not able to free up a string...

M

mmonty11

hi

i am trying to write a short program. i have this function, when i try
to free up line, i got a Segmentation
fault
if i remove, free(line); the program work fine

void readFile(int fd,char *directory, char *filename[], int sizedir,
int sizefile[])
{
FILE *fp;
char *line[2];
char *tmp;
int i;
int size[2];

if ( ( tmp = malloc( sizedir + sizefile[0] + 1) ) == NULL )
exit( EXIT_FAILURE );

strcpy(tmp, directory);
strcat(tmp, "/");
strcat(tmp, filename[0]);

fp = fopen(tmp, "r");
if(fp != NULL)
{

if ( ( line[0] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE );

fgets(line[0], LINE_MAX, fp);
fclose(fp);
free(tmp);

if ( ( line[1] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE );

if ( ( tmp = malloc( sizedir + sizefile[1] + 1) ) == NULL )
exit( EXIT_FAILURE );

strcpy(tmp, directory);
strcat(tmp, "/");
strcat(tmp, filename[1]);

fp = fopen(tmp, "r");
if(fp != NULL)
{
fgets(line[1], LINE_MAX, fp);
size[0]= strlen(line[0]);
size[1]= strlen(line[1]);

analyzeFilename(fd, filename, line, size);
fclose(fp);
}
else
fprintf(stderr, "%s - Not able to open the file: %s\n",
strerror(errno),filename[1] );
}
else
fprintf(stderr, "%s - 1 Not able to open the file: %s\n",
strerror(errno),filename[0]);

free(tmp);
free(line);

}

any idea?

thanks
 
W

Walter Roberson

i am trying to write a short program. i have this function, when i try
to free up line, i got a Segmentation
fault
if i remove, free(line); the program work fine
void readFile(int fd,char *directory, char *filename[], int sizedir,
int sizefile[])
{
FILE *fp;
char *line[2];
if ( ( line[0] = malloc( LINE_MAX + 1) ) == NULL )
if ( ( line[1] = malloc( LINE_MAX + 1) ) == NULL )
free(line);

You didn't malloc() a pointer that is stored as line. You malloc()'d
a pointer stored into line[0] and another pointer stored in line[1].
You can't just pass the array of pointers to free() and expect that
C will run through each element of the array and free what's there.

In char *line[2], line is not a pointer: line is an array of two
elements, and the value of line is invariant (for any one invocation
of the function.) Essentially line is a constant there. You can't
free() a constant.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


hi

i am trying to write a short program. i have this function, when i try
to free up line, i got a Segmentation
fault
if i remove, free(line); the program work fine

void readFile(int fd,char *directory, char *filename[], int sizedir,
int sizefile[])
{
FILE *fp;
char *line[2];

"line" is an array of two pointers to char.

[snip]
if ( ( line[0] = malloc( LINE_MAX + 1) ) == NULL )

line[0] is populated with a pointer to char

[snip]
if ( ( line[1] = malloc( LINE_MAX + 1) ) == NULL )

line[1] is populated with a pointer to char

[snip]
free(line);

Here, you neither free(line[0]) nor free(line[1])

Instead, you attempt to free(line), which has not been malloc()ed. line
is an automatic variable, not a pointer to malloc()ed data. Don't do
that. It annoys the system and causes unpredictable behaviour (such as
your Segmentation Fault).

What you /need/ to do is
free(line[0]);
and
free(line[1]);
at the appropriate places in your logic. line[0] /is/ a pointer that
you have populated from malloc(), as is line[1].
}

any idea?

thanks

You're welcome

HTH
- --
Lew Pitcher


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFEzllAagVFX4UWr64RAqtaAJ9LOtZ9FspWEHrsXOLi717QId32tgCgzTkS
GtGCjOlJi4ewVwfinRn796I=
=kRAO
-----END PGP SIGNATURE-----
 
D

Dave Thompson

In addition to the other answers already given:

void readFile(int fd,char *directory, char *filename[], int sizedir,
int sizefile[])
{
char *tmp;
if ( ( tmp = malloc( sizedir + sizefile[0] + 1) ) == NULL )
exit( EXIT_FAILURE );

strcpy(tmp, directory);
strcat(tmp, "/");
strcat(tmp, filename[0]);
And later similarly for sizefile[1] and filename[1].

Assuming sizedir is the length of the string pointed to by directory
and sizefile is the length of the string pointed to by filename,
this allocates too little space -- you need one more byte for the null
terminator that the str* routines will copy -- and the fopen, snipped,
will need.

Requiring the caller to provide those lengths is an easy source of
bugs. You're probably better off passing only the strings (pointers)
and letting this routine call strlen() to determine their lengths.
Even if this routine is called enough zillions of times for the cost
of the strlen() calls to be nonnegligible, the cost of the fopen(),
fgets(), and fclose() for each file will almost certainly dominate.
if ( ( line[0] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE );

fgets(line[0], LINE_MAX, fp);
And similarly for [1].

Here, oppositely, you allocate more space than needed. fgets ( , N,
fp) reads at most N-1 characters and adds a null terminator for a
total at most N bytes (except if EOF or I/O error).


- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top