ptr2 could be different from ptr1 after realloc(), in that case, should I free(ptr1)?Code:char *ptr1, *ptr2; ptr1 = (char *)malloc(m); ptr2 = ptr1; ptr2 = (char *)realloc(ptr1, n); // n > m
ptr2 could be different from ptr1 after realloc(), in that case, should I free(ptr1)?Code:char *ptr1, *ptr2; ptr1 = (char *)malloc(m); ptr2 = ptr1; ptr2 = (char *)realloc(ptr1, n); // n > m
Yes. No. Read the FAQ on why you shouldn't typecast functions that return void pointers.
Quzah.
Hope is the first step on the road to disappointment.
-> Yes. No.
Is Yes said that ptr2 != ptr1 maybe true, and No said that I need not free(ptr1) if it's no longer used?
> ptr2 could be different from ptr1 after realloc(), in that case, should I free(ptr1)?
Only if ptr2 is NULL.
If ptr2 isn't NULL, ptr1 has already been freed and the memory copied to where ptr2 now points.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
thanks.
BTW, is there any function can figure out that if a pointer is allocated any memory?
Do you mean to check if allocation was successfull? the malloc
set of functions return NULL if they fail, if its not equal to NULL,
you can take it that allocation was successfull and the pointer
points to some form of memory.
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
No, what I mean is, giving you a pointer ptr, then can you know that if the memory pointed by ptr was once allocated by using malloc(), realloc(), etc. ? For example,
My question is, is there any function can tell me that ptr1 just points to a single char 'a', while ptr2 points to a block of memory?Code:char *ptr1 = &'a'; char *ptr2 = malloc(20);
Well i asked google and came up with this. Naturally i can't stand
over this because it's not my code/idea but it might help. Maybe
one of the more experienced members here can shed more light
[edit]
The MSDN link at the bottom of that page recommends a function
in malloc.h - this is outdated by about 20 years so there's
probably a more modern way.
[/edit]
Last edited by Richie T; 05-10-2006 at 07:11 AM.
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
> then can you know that if the memory pointed by ptr was once allocated by using malloc(), realloc(), etc. ?
No, there is no portable way to know this.
Nor is there any portable way to know how much was allocated simply by looking at the pointer.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Your code won't compile. You cannot get the address of a character constant. It would be like getting the address of a number. You just can't do it.Originally Posted by TalosChen
Quzah.
Hope is the first step on the road to disappointment.
:-) I know that. It's just an example to explain my question.