Will it cause problems if I call free() on a char* that I never called malloc() for?
Will it cause problems if I call free() on a char* that I never called malloc() for?
Probably. And what do you hope to achieve with that?
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
It was a timing issue I was worried about. There was a chance that the way I had structured my main loop, that I might be freeing a variable that might not have been malloced during the iteration.
I wondered if I'd need to set a flag to make sure.
Another way to check this is to set those variable to NULL when they are declared or freed:
Pointers are never set to NULL (either when initialized or freed) unless you do it, but if they are NULL then you know who did it and why (hopefully: they are already freed or unallocated*). This is easier than using a flag.Code:char *ptr=NULL; [various things may or may not happen] if (ptr) { free(ptr); ptr=NULL; }
*remember that setting an allocated pointer to NULL does not free it, so would be a memory leak...
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I seem to remember seeing somewhere that you cannot allocate memory for a pointer that is already set to NULL. Is this true?
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Instead of NULL, would setting the prt to 0 work?
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
It is not as obscure as you think: 0 is a null pointer constant.Originally Posted by MK27
Personal choice to avoid the use of a macro.Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
NULL and 0 are essentially the same thing - serves the same purpose and behaves the same. Any particular reason you prefer to use 0?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
But then if you have a long function with something like this:
That's why I use NULL. It makes is clear that the variable it's being assigned to is a pointer.Code:char* blah = 0; // You can clearly see it's a pointer since you see the declaration. ... ... blah = 0; // Not so obvious 50 or more lines down since you can't see how blah is declared. Is it a pointer or an int?
And yes, since NULL is 0 you could assign NULL to an int, but that would just be dumb.
Although, usually the name of the variable itself gives you a pretty good idea about it's type since a lot of people prefix pointers with 'p'...
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
Hence a function should do one thing and do it wellOriginally Posted by cpjust
Unless a mistake is made for the same reason as you cite NULL being useful. The reader would then be misinformed unless he/she checked, but if he/she checked then the use of NULL would no longer be useful.Originally Posted by cpjust
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The closest I can say that's remotely connected to is reallocBladactania:
I seem to remember seeing somewhere that you cannot allocate memory for a pointer that is already set to NULL. Is this true