 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Sat May 12, 2007 9:11 am Post subject: Array of Pointers and Freeing problem |
|
|
hi,
Following is problematic Code
main()
{
char *ptr[1]={NULL};
ftn(ptr);
}
void ftn(**data)
{
*data=malloc(4);
memset(*data,0,4);
strcpy(*data,"HAI");
} |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Sat May 12, 2007 9:11 am Post subject: Re: Array of Pointers and Freeing problem |
|
|
anand1603 (AT) yahoo (DOT) com said:
| Quote: | On May 12, 12:40 am, anand1...@yahoo.com wrote:
hi,
Following is problematic Code
main()
{
char *ptr[1]={NULL};
ftn(ptr);
printf("String is %s\n",*ptr)
/*It PRints "HAI" -No Problem*/
if(*ptr !=NULL)
free(*ptr);
/*It Says Invalid Pointer*/
}
void ftn(**data)
{
*data=malloc(4);
memset(*data,0,4);
strcpy(*data,"HAI");
}-
hi,
Please let me why the free is throwing "Inavild Pointer" Error.
|
foo.c:2: warning: return-type defaults to `int'
foo.c:2: warning: function declaration isn't a prototype
foo.c: In function `main':
foo.c:3: `NULL' undeclared (first use in this function)
foo.c:3: (Each undeclared identifier is reported only once
foo.c:3: for each function it appears in.)
foo.c:3: initializer element is not computable at load time
foo.c:3: (near initialization for `ptr[0]')
foo.c:4: warning: implicit declaration of function `ftn'
foo.c:5: warning: implicit declaration of function `printf'
foo.c:7: parse error before `if'
foo.c:10: warning: control reaches end of non-void function
foo.c: At top level:
foo.c:12: parse error before `*'
foo.c:13: warning: function declaration isn't a prototype
foo.c:13: warning: type mismatch with previous implicit declaration
foo.c:4: warning: previous implicit declaration of `ftn'
foo.c:13: warning: `ftn' was previously implicitly declared to return
`int'
foo.c: In function `ftn':
foo.c:14: `data' undeclared (first use in this function)
foo.c:14: warning: implicit declaration of function `malloc'
make: *** [foo.o] Error 1
Sort those out, and come on back if you're still having trouble.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www. |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Sat May 12, 2007 9:11 am Post subject: Re: Array of Pointers and Freeing problem |
|
|
anand1603 (AT) yahoo (DOT) com said:
| Quote: | hi,
Following is problematic Code
main()
{
char *ptr[1]={NULL};
ftn(ptr);
}
void ftn(**data)
{
*data=malloc(4);
memset(*data,0,4);
strcpy(*data,"HAI");
}
|
You're right. The code is problematic. For one thing, it won't compile.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www. |
|
| Back to top |
|
 |
Guest
|
Posted: Sat May 12, 2007 9:11 am Post subject: Re: Array of Pointers and Freeing problem |
|
|
On May 12, 12:40 am, anand1...@yahoo.com wrote:
| Quote: | hi,
Following is problematic Code
main()
{
char *ptr[1]={NULL};
ftn(ptr);
printf("String is %s\n",*ptr)
/*It PRints "HAI" -No Problem*/ |
if(*ptr !=NULL)
free(*ptr);
/*It Says Invalid Pointer*/
| Quote: | }
void ftn(**data)
{
*data=malloc(4);
memset(*data,0,4);
strcpy(*data,"HAI");
}-
|
hi,
Please let me why the free is throwing "Inavild Pointer" Error.
Your help is highly aprreciated.
Regards
Anand |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|