Dumb question concerning malloc/free
Author |
Message |
W. Kierna #1 / 6
|
 Dumb question concerning malloc/free
Hello newsgroup! In the past I've used malloc to allocate space for a string and, following the rule "better safe than sorry", if I was planning to mess around with the pointer to the string I always copied the pointer, messed with the copy, and saved the original pointer to send to free later. ... p = (char)malloc(10); strcpy(p, "Hello"); p1 = p + 1; /* cut the first character off the string */ printf("%s\n", p1); free(p); but I was just glancing at the back of K&R and it says void free(void *p) free deallocates the space pointed to by P; it does nothing if p is NULL. p must be a pointer to space previously allocated by calloc, malloc or realloc. Have I been too cautious? If instead of the example above, I did ... p = (char)malloc(10); strcpy(p, "Hello"); p++; /* cut the first character off the string */ printf("%s\n", p); free(p); would that be a bad thing to do, as I suspect? p still points to the space of memory allocated by malloc, just not the beginning of that space. The book is not entirely unambiguous about it, so I thought maybe some of you folks here, who know how malloc is actually implemented, might be able to tell me if I have wasted the effort (and storage for one pointer) to save the original value of p that was returned from malloc. Also, this is another dumb question, has anybody ever copied K&R to a text file? My copy of K&R is actually larger in physical size than my favorite PC to hack around on (an HP 200). Thanks in advance for your answers.
|
Thu, 05 Oct 2000 03:00:00 GMT |
|
 |
Will Ro #2 / 6
|
 Dumb question concerning malloc/free
: Hello newsgroup! : In the past I've used malloc to allocate space for a string and, : following the rule "better safe than sorry", if I was planning to mess : around with the pointer to the string I always copied the pointer, : messed with the copy, and saved the original pointer to send to free : later. : ... : p = (char)malloc(10); : strcpy(p, "Hello"); : p1 = p + 1; /* cut the first character : off the string */ : printf("%s\n", p1); : free(p); Correct. : but I was just glancing at the back of K&R and it says : void free(void *p) : free deallocates the space pointed to by P; it does nothing if : p is NULL. p must be a pointer to space previously allocated : by calloc, malloc or realloc. : Have I been too cautious? If instead of the example above, I did : ... : p = (char)malloc(10); : strcpy(p, "Hello"); : p++; /* cut the first character : off the string */ : printf("%s\n", p); : free(p); Incorrect. : would that be a bad thing to do, as I suspect? p still points to the : space of memory allocated by malloc, just not the beginning of that : space. P must be a pointer to space previously allocated by malloc... that is, it must be a value returned from malloc(), not just a pointer somewhere into a memory area allocated by malloc(). Malloc's allowed to do magic when allocating memory: in fact it probably has to, to let free() know how much memory to de-allocate. For instance, there may be other data structures stored just before the malloc'd data which only malloc() and free() understand, and which tell free() how much memory to release. Unless you pass back the exact value you got from malloc(), free() won't find this data and bad things will happen. Will
|
Thu, 05 Oct 2000 03:00:00 GMT |
|
 |
Jack Klei #3 / 6
|
 Dumb question concerning malloc/free
Quote: > Hello newsgroup! > In the past I've used malloc to allocate space for a string and, > following the rule "better safe than sorry", if I was planning to mess > around with the pointer to the string I always copied the pointer, > messed with the copy, and saved the original pointer to send to free > later. > ... > p = (char)malloc(10);
^^^^^^^^^^^^^^^^^^^^^^^^ I hope this is a typo, and that in your programs you actually use: p = (char *)malloc(10); <Jack> Will Rose already answered the question you asked, and correctly. I am sure this is a typographical error you made when typing your message, and you do it correctly in your code, but this is a perfect example of why most experienced C programmers say you shouldn't cast the return value of malloc() and its cousins. We/they usually say the cast can hide mistakes if you have forgotten to include stdlib.h, but in this case it could hide a mistake even if you DO include the proper header. C allows malloc's return type (a pointer to void) to be assigned to any pointer type without a cast. </Jack>
|
Thu, 05 Oct 2000 03:00:00 GMT |
|
 |
Will Ro #4 / 6
|
 Dumb question concerning malloc/free
: : Hello newsgroup! : : In the past I've used malloc to allocate space for a string and, : : following the rule "better safe than sorry", if I was planning to mess : : around with the pointer to the string I always copied the pointer, : : messed with the copy, and saved the original pointer to send to free : : later. : : ... : : p = (char)malloc(10); : : strcpy(p, "Hello"); : : p1 = p + 1; /* cut the first character : : off the string */ : : printf("%s\n", p1); : : free(p); : Correct. Actually, incorrect - as Jack Klien says in another post, malloc's been cast to char not char *. I deliberately ignored the cast, and didn't notice that it was the wrong cast. Will
|
Thu, 05 Oct 2000 03:00:00 GMT |
|
 |
John Kugelma #5 / 6
|
 Dumb question concerning malloc/free
Quote:
> Hello newsgroup! > In the past I've used malloc to allocate space for a string and, > following the rule "better safe than sorry", if I was planning to mess > around with the pointer to the string I always copied the pointer, > messed with the copy, and saved the original pointer to send to free > later. > ... > p = (char)malloc(10);
Oops, that cast should be (char *). However, even better would be to not use the cast at all, since it hides the error of not prototyping malloc. Quote: > strcpy(p, "Hello"); > p1 = p + 1; /* cut the first character > off the string */ > printf("%s\n", p1); > free(p); > but I was just glancing at the back of K&R and it says > void free(void *p) > free deallocates the space pointed to by P; it does nothing if > p is NULL. p must be a pointer to space previously allocated > by calloc, malloc or realloc. > Have I been too cautious? If instead of the example above, I did > ... > p = (char)malloc(10); > strcpy(p, "Hello"); > p++; /* cut the first character > off the string */ > printf("%s\n", p); > free(p); > would that be a bad thing to do, as I suspect? p still points to the > space of memory allocated by malloc, just not the beginning of that > space. The book is not entirely unambiguous about it, so I thought > maybe some of you folks here, who know how malloc is actually > implemented, might be able to tell me if I have wasted the effort (and > storage for one pointer) to save the original value of p that was > returned from malloc.
You were right to be cautious. free expects a pointer to the location malloc returned, not any portion of that block. Some implementations of malloc/free store the amount of memory allocated in a few bytes before the location it gives you, so when you give free a pointer, it looks back a few bytes and sees how long your memory block is. If you pass it a pointer somewhere in the middle, it would look back a few bytes into your data and would find an incorrect number. I suppose it might be nice not to have to save the pointer in some cases, but it's only a few lines of typing. The overhead of free having to search a large table of addresses to see which block your pointer is inside would be unacceptable overhead to most everyone. (Dynamic allocation is slow enough as is.) --
I believe we can change anything. I believe in my dream. - Joe Satriani
|
Thu, 05 Oct 2000 03:00:00 GMT |
|
 |
Syd #6 / 6
|
 Dumb question concerning malloc/free
Quote:
>> Hello newsgroup! >> In the past I've used malloc to allocate space for a string and, >> following the rule "better safe than sorry", if I was planning to >mess >> around with the pointer to the string I always copied the pointer, >> messed with the copy, and saved the original pointer to send to free >> later. >> ... >> p = (char)malloc(10); >^^^^^^^^^^^^^^^^^^^^^^^^ >I hope this is a typo, and that in your programs you actually use: >p = (char *)malloc(10);
Also, please check the return value from malloc and make sure it isn't null. If it is, this program breaks. -- Please disregard the e-mail address in the header of this message. I may be addressed as "syd" at the commercial internet service provider "Netroplex."
|
Thu, 05 Oct 2000 03:00:00 GMT |
|
|
|