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:
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");
}

This won't compile either.

--
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
Ian Collins
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 wrote:
Quote:
hi,
Following is problematic Code

Indeed it is, it won't compile.


Quote:
main()
{
char *ptr[1]={NULL};
ftn(ptr);


}
void ftn(**data)
{
*data=malloc(4);
memset(*data,0,4);
strcpy(*data,"HAI");
}



--
Ian Collins.
Back to top
Martin Ambuhl
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 wrote:
Quote:
hi,
Following is problematic Code

Yes, it is.

Start by paying attention to the following changes. Then decide what I
have left undone.

/* You need these headers */
#include <stdlib.h>
#include <string.h>

/* Without the following, ftn (in main) is explicitly declared to
return an int, which conflicts with its later redefinition */
void ftn(char *data[]);

/* main returns an int. You should say so. Not doing so is an error
for the C standard from 1999 on, and a bad idea for the earlier
standard. */
int main(void)
{
char *ptr[1] = { NULL };
ftn(ptr);

/* You allocated space for ptr[0] to point at. free it */
free(*ptr);

/* main returns an int. You should do so. Not doing so is an error
for the C standard up to 1999, and a bad idea for the later
standard. */
return 0;

}

/* You have specified no type for the argument to ftn, do so: */
void ftn(char *data[])
{
*data = malloc(4);
/* you should always check that malloc succeeds */
if (!*data)
return;
#if 0
/* The following memset is completely worthless. It has been
commented out. */
memset(*data, 0, 4);
#endif
strcpy(*data, "HAI");
}
Back to top
Barry Schwarz
Guest





PostPosted: Sun May 13, 2007 5:03 am    Post subject: Re: Array of Pointers and Freeing problem Reply with quote

On Sat, 12 May 2007 18:07:04 -0400, CBFalconer <cbfalconer (AT) yahoo (DOT) com>
wrote:

Quote:
Richard Heathfield wrote:
anand1603 (AT) yahoo (DOT) com said:

Following is problematic Code

main()
{
char *ptr[1]={NULL};
ftn(ptr);
}

void ftn(**data)
{
*data=malloc(4);
memset(*data,0,4);
strcpy(*data,"HAI");
}

This won't compile either.

However, this will (untested). (To OP) Study the difference.

#include <stdlib.h
#include <string.h

void ftn(**data) {

Doesn't the argument need a type? I think you meant

void ftn(char **data){

Quote:
*data = malloc(4);

memset(*data, 0, 4);
strcpy(*data, "HAI");
}

int main(void) {
char *ptr[1] = {NULL};
ftn(ptr);
return 0;
}



Remove del for email
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.