C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Array of Pointers and Freeing problem

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Sat May 12, 2007 9:11 am    Post subject: Array of Pointers and Freeing problem Reply with 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");
}
Back to top
Richard Heathfield
Guest





PostPosted: Sat May 12, 2007 9:11 am    Post subject: Re: Array of Pointers and Freeing problem Reply with quote



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





PostPosted: Sat May 12, 2007 9:11 am    Post subject: Re: Array of Pointers and Freeing problem Reply with quote



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






PostPosted: Sat May 12, 2007 9:11 am    Post subject: Re: Array of Pointers and Freeing problem Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.