Sujet : Re: Joy of this, Joy of that
De : invalid (at) *nospam* invalid.invalid (Richard Kettlewell)
Groupes : comp.os.linux.miscDate : 27. Nov 2024, 19:32:25
Autres entêtes
Organisation : terraraq NNTP server
Message-ID : <wwvmshkle86.fsf@LkoBDZeT.terraraq.uk>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
The Natural Philosopher <
tnp@invalid.invalid> writes:
On 27/11/2024 01:48, Rich wrote:
The only two guarantees given in that paragraph are:
1) if ptr was returned by one of the malloc's, the space referenced
by
ptr is deallocated (albeit only for the first call of free(ptr), per
the second sentence);
and
2) if ptr is NULL, nothing occurs.
>
Exactly. The result of anything else is implementation dependent on
the library.
>
One might write a free that says 'look at our list of allocated
blocks, compare with the pointer and if no match do nothing'
>
Or one that says 'look at our list of allocated block compare with
the pointer and if no match, segfault'
>
I am not au fait with the internal of 'free()' so I cannot comment as
to why the first is not always the case.
You can absolutely write an allocator that does that if you want. You’ll
pay for it in performance and memory usage, but it might well be
worthwhile in a given application.
However, it doesn’t fully solve the problem. Suppose the sequence of
operations (with lots going on between) happens to look like this:
int *p = malloc(xxx);
// ...
free(p);
// ...
int *q = malloc(yyy);
// ...
free(p); // double free of p
// ...
*q = 1;
If p!=q then a tracking allocator like you envisage would spot the
error. But if it happens that p=q (which is entirely possible) then the
tracking allocator won’t notice the problem. The program will start to
misbehave seriously when the it access the (now freed) memory pointed to
be q.
Nulling out pointers after freeing them, as suggested elsewhere helps,
but that depends on perfect play by human programmers, not something you
want to rely on.
To go further you need to track not just the status of each allocation
but the provenance of the pointers to. valgrind does that (and a lot
more) but the performance penalty means it’s only practical to use for
testing and debugging.
-- https://www.greenend.org.uk/rjk/
Date | Sujet | # | | Auteur |
19 Nov 24 | Joy of this, Joy of that | 806 | | root |
20 Nov 24 |  Re: Joy of this, Joy of that | 4 | | Lawrence D'Oliveiro |
20 Nov 24 |   Re: Joy of this, Joy of that | 3 | | root |
20 Nov 24 |    Re: Joy of this, Joy of that | 1 | | Lawrence D'Oliveiro |
20 Nov 24 |    Re: Joy of this, Joy of that | 1 | | rbowman |
20 Nov 24 |  Re: Joy of this, Joy of that | 801 | | 186282@ud0s4.net |
20 Nov 24 |   Re: Joy of this, Joy of that | 193 | | Rich |
20 Nov 24 |    Re: Joy of this, Joy of that | 1 | | The Natural Philosopher |
21 Nov 24 |    Re: Joy of this, Joy of that | 191 | | 186282@ud0s4.net |
21 Nov 24 |     Re: Joy of this, Joy of that | 2 | | Lawrence D'Oliveiro |
14 Jan 25 |      Re: Joy of this, Joy of that | 1 | | Bozo User |
21 Nov 24 |     Re: Joy of this, Joy of that | 187 | | The Natural Philosopher |
22 Nov 24 |      Re: Joy of this, Joy of that | 186 | | Don_from_AZ |
22 Nov 24 |       Re: Joy of this, Joy of that | 185 | | Lawrence D'Oliveiro |
23 Nov 24 |        Re: Joy of this, Joy of that | 184 | | 186282@ud0s4.net |
23 Nov 24 |         Re: Joy of this, Joy of that | 5 | | Lawrence D'Oliveiro |
24 Nov 24 |          Re: Joy of this, Joy of that | 4 | | 186282@ud0s4.net |
24 Nov 24 |           Re: Joy of this, Joy of that | 2 | | Lawrence D'Oliveiro |
24 Nov 24 |            Re: Joy of this, Joy of that | 1 | | 186282@ud0s4.net |
24 Nov 24 |           Re: Joy of this, Joy of that | 1 | | rbowman |
23 Nov 24 |         Re: Joy of this, Joy of that | 178 | | The Natural Philosopher |
23 Nov 24 |          Re: Joy of this, Joy of that | 1 | | rbowman |
23 Nov 24 |          Re: Joy of this, Joy of that | 1 | | Lawrence D'Oliveiro |
24 Nov 24 |          Re: Joy of this, Joy of that | 174 | | 186282@ud0s4.net |
24 Nov 24 |           Re: Joy of this, Joy of that | 1 | | rbowman |
24 Nov 24 |           Re: Joy of this, Joy of that | 172 | | Rich |
24 Nov 24 |            Re: Joy of this, Joy of that | 168 | | The Natural Philosopher |
24 Nov 24 |             Re: Joy of this, Joy of that | 159 | | Rich |
24 Nov 24 |              Re: Joy of this, Joy of that | 158 | | D |
25 Nov 24 |               Re: Joy of this, Joy of that | 157 | | rbowman |
25 Nov 24 |                Re: Joy of this, Joy of that | 156 | | D |
25 Nov 24 |                 Re: Joy of this, Joy of that | 155 | | The Natural Philosopher |
25 Nov 24 |                  Re: Joy of this, Joy of that | 15 | | D |
26 Nov 24 |                   Re: Joy of this, Joy of that | 14 | | rbowman |
26 Nov 24 |                    Re: Joy of this, Joy of that | 1 | | D |
26 Nov 24 |                    Re: Joy of this, Joy of that | 12 | | Pancho |
26 Nov 24 |                     Re: Joy of this, Joy of that | 11 | | Rich |
26 Nov 24 |                      Re: Joy of this, Joy of that | 2 | | Chris Ahlstrom |
27 Nov 24 |                       Re: Joy of this, Joy of that | 1 | | Pancho |
26 Nov 24 |                      Re: Joy of this, Joy of that | 8 | | rbowman |
26 Nov 24 |                       Re: Joy of this, Joy of that | 4 | | D |
27 Nov 24 |                        Re: Joy of this, Joy of that | 1 | | Rich |
27 Nov 24 |                        Re: Joy of this, Joy of that | 2 | | rbowman |
27 Nov 24 |                         Re: Joy of this, Joy of that | 1 | | D |
27 Nov 24 |                       Re: Joy of this, Joy of that | 3 | | The Natural Philosopher |
27 Nov 24 |                        Re: Joy of this, Joy of that | 2 | | rbowman |
28 Nov 24 |                         Re: Joy of this, Joy of that | 1 | | The Natural Philosopher |
25 Nov 24 |                  Re: Joy of this, Joy of that | 139 | | Lawrence D'Oliveiro |
27 Nov 24 |                   Re: Joy of this, Joy of that | 138 | | 186282@ud0s4.net |
27 Nov 24 |                    Re: Joy of this, Joy of that | 137 | | Lawrence D'Oliveiro |
28 Nov 24 |                     Re: Joy of this, Joy of that | 136 | | 186282@ud0s4.net |
28 Nov 24 |                      Re: Joy of this, Joy of that | 133 | | rbowman |
28 Nov 24 |                       Re: Joy of this, Joy of that | 132 | | 186282@ud0s4.net |
28 Nov 24 |                        Re: Joy of this, Joy of that | 3 | | rbowman |
29 Nov 24 |                         Re: Joy of this, Joy of that | 2 | | 186282@ud0s4.net |
29 Nov 24 |                          Re: Joy of this, Joy of that | 1 | | rbowman |
28 Nov 24 |                        Re: Joy of this, Joy of that | 128 | | Lawrence D'Oliveiro |
29 Nov 24 |                         Re: Joy of this, Joy of that | 127 | | 186282@ud0s4.net |
29 Nov 24 |                          Re: Joy of this, Joy of that | 116 | | rbowman |
30 Nov 24 |                           Re: Joy of this, Joy of that | 115 | | 186282@ud0s4.net |
30 Nov 24 |                            Re: Joy of this, Joy of that | 1 | | Lawrence D'Oliveiro |
30 Nov 24 |                            Re: Joy of this, Joy of that | 113 | | rbowman |
30 Nov 24 |                             Re: Joy of this, Joy of that | 2 | | The Natural Philosopher |
1 Dec 24 |                              Re: Joy of this, Joy of that | 1 | | Lawrence D'Oliveiro |
1 Dec 24 |                             Re: Joy of this, Joy of that | 110 | | 186282@ud0s4.net |
1 Dec 24 |                              Re: Joy of this, Joy of that | 5 | | The Natural Philosopher |
1 Dec 24 |                               Re: Joy of this, Joy of that | 2 | | Rich |
3 Dec 24 |                                Re: Joy of this, Joy of that | 1 | | 186282@ud0s4.net |
1 Dec 24 |                               Re: Joy of this, Joy of that | 1 | | D |
2 Dec 24 |                               Re: Joy of this, Joy of that | 1 | | 186282@ud0s4.net |
1 Dec 24 |                              Re: Joy of this, Joy of that | 2 | | Lawrence D'Oliveiro |
3 Dec 24 |                               Re: Joy of this, Joy of that | 1 | | 186282@ud0s4.net |
1 Dec 24 |                              Re: Joy of this, Joy of that | 102 | | rbowman |
3 Dec 24 |                               Re: Joy of this, Joy of that | 101 | | 186282@ud0s4.net |
3 Dec 24 |                                Re: Joy of this, Joy of that | 2 | | The Natural Philosopher |
3 Dec 24 |                                 Re: Joy of this, Joy of that | 1 | | rbowman |
3 Dec 24 |                                Re: Joy of this, Joy of that | 98 | | rbowman |
5 Dec 24 |                                 Re: Joy of this, Joy of that | 97 | | 186282@ud0s4.net |
5 Dec 24 |                                  Re: Joy of this, Joy of that | 92 | | D |
6 Dec 24 |                                   Re: Joy of this, Joy of that | 91 | | 186282@ud0s4.net |
6 Dec 24 |                                    Re: Joy of this, Joy of that | 37 | | rbowman |
6 Dec 24 |                                     Re: Joy of this, Joy of that | 34 | | D |
7 Dec 24 |                                      Re: Joy of this, Joy of that | 2 | | 186282@ud0s4.net |
7 Dec 24 |                                       Re: Joy of this, Joy of that | 1 | | D |
7 Dec 24 |                                      Re: Joy of this, Joy of that | 17 | | 186282@ud0s4.net |
7 Dec 24 |                                       Re: Joy of this, Joy of that | 16 | | D |
7 Dec 24 |                                        Re: Joy of this, Joy of that | 14 | | Rich |
7 Dec 24 |                                         Re: Joy of this, Joy of that | 13 | | D |
7 Dec 24 |                                          Re: Joy of this, Joy of that | 12 | | Rich |
7 Dec 24 |                                           Re: Joy of this, Joy of that | 10 | | D |
8 Dec 24 |                                            Re: Joy of this, Joy of that | 7 | | Rich |
8 Dec 24 |                                             Re: Joy of this, Joy of that | 5 | | rbowman |
8 Dec 24 |                                              Re: Joy of this, Joy of that | 4 | | Rich |
8 Dec 24 |                                               Re: Joy of this, Joy of that | 2 | | The Natural Philosopher |
9 Dec 24 |                                                Re: Joy of this, Joy of that | 1 | | The Natural Philosopher |
8 Dec 24 |                                               Re: Joy of this, Joy of that | 1 | | rbowman |
8 Dec 24 |                                             Re: Joy of this, Joy of that | 1 | | D |
8 Dec 24 |                                            Re: Joy of this, Joy of that | 2 | | Robert Riches |
8 Dec 24 |                                             Re: Joy of this, Joy of that | 1 | | D |
7 Dec 24 |                                           Re: Joy of this, Joy of that | 1 | | D |
8 Dec 24 |                                        Re: Joy of this, Joy of that | 1 | | 186282@ud0s4.net |
7 Dec 24 |                                      Re: Joy of this, Joy of that | 14 | | D |
7 Dec 24 |                                     Re: Joy of this, Joy of that | 2 | | 186282@ud0s4.net |
6 Dec 24 |                                    Re: Joy of this, Joy of that | 53 | | D |
5 Dec 24 |                                  Re: Joy of this, Joy of that | 4 | | Lawrence D'Oliveiro |
29 Nov 24 |                          Re: Joy of this, Joy of that | 10 | | Lawrence D'Oliveiro |
28 Nov 24 |                      Re: Joy of this, Joy of that | 2 | | Lawrence D'Oliveiro |
25 Nov 24 |             Re: Joy of this, Joy of that | 8 | | vallor |
24 Nov 24 |            Re: Joy of this, Joy of that | 3 | | D |
24 Nov 24 |          Re: Joy of this, Joy of that | 1 | | Rich |
21 Nov 24 |     Re: Joy of this, Joy of that | 1 | | Rich |
20 Nov 24 |   Re: Joy of this, Joy of that | 606 | | John Ames |
20 Nov 24 |   Re: Joy of this, Joy of that | 1 | | rbowman |