malloc & free

C

Cadderly

Hi all,

I have the following pseudo-c-code which parse a command line, and puts the
command and its arguments in an array, and I can't figure out where/how I
should use free:

main(void):
char ** command_arr;
parser(command_arr);

parser(char ** command_arr):
command_arr = malloc ( argument_number * sizeof(char *);
while ( there is an argument arg ) {
*(command_arr + command_arr_index) = malloc ( strlen(arg) + 1 )
strcpy (*(command_arr + command_arr_index), arg)
command_arr_index++;
}
*(command_arr + command_arr_index) = NULL;

well, what i wonder is if, in the main fonction, free(command_arr)
deallocates 'malloced' spaces in the parser (with strlen), or should I write
something like that?
while ( *(command_arr + command_arr_index) != NULL ) {
free( *(command_arr + command_arr_index) );
command_arr_index++;
}
free(command_arr);


I know the code is not rigorous but the question is mainly on the usage of
malloc/free..
Thanks
 
J

Jens.Toerring

Cadderly said:
I have the following pseudo-c-code which parse a command line, and puts the
command and its arguments in an array, and I can't figure out where/how I
should use free:
main(void):
char ** command_arr;
parser(command_arr);
parser(char ** command_arr):
command_arr = malloc ( argument_number * sizeof(char *);
while ( there is an argument arg ) {
*(command_arr + command_arr_index) = malloc ( strlen(arg) + 1 )
strcpy (*(command_arr + command_arr_index), arg)
command_arr_index++;
}
*(command_arr + command_arr_index) = NULL;
well, what i wonder is if, in the main fonction, free(command_arr)
deallocates 'malloced' spaces in the parser (with strlen), or should I write
something like that?
while ( *(command_arr + command_arr_index) != NULL ) {
free( *(command_arr + command_arr_index) );
command_arr_index++;
}
free(command_arr);

Only the second alternative will deallocate all memory, the first
one would only deallocate the memory used for the pointers but not
what they are pointing to, thus leading to a memory leak. BTW, you
must allocate one more pointer than the maximum number of arguments
(for the NULL pointer) or you write past the end of the array of
pointers.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
C

Cadderly

thanks,
I've got another problem with the same structure
in parser, printf("%s\n", command_arr) gives me the correct result, but in
main, after parser, the same printf gives me a NULL, my opinion is that's
related to the pass-by-value property, I'm changing a copy of command_arr
although it's a pointer, am-i correct? how can I solve this problem without
having to declare parser with ***command_arr, because it seems to me very
ugly :)
 
J

Jens.Toerring

I've got another problem with the same structure
in parser, printf("%s\n", command_arr) gives me the correct result, but in

I guess you mean "printf("%s\n", *command_arr)" (to print the first string)
because comamand_arr is a pointer to a pointer to char, so you can't
print it using "%s"...
main, after parser, the same printf gives me a NULL, my opinion is that's
related to the pass-by-value property, I'm changing a copy of command_arr
although it's a pointer, am-i correct? how can I solve this problem without

Yes. I did overlook this is my previous post.
having to declare parser with ***command_arr, because it seems to me very
ugly :)

A solution that might appeal more to your sense of beauty would be
to return command_arr from your parser() function, i.e. uses some-
thing like

int main( void ) {
char **command_arr = parser( );
...
}

char **parser( void )
char **command_arr = malloc( ( argument_number + 1 )
* sizeof *command_arr );
....
return command_arr;
}
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
B

Barry Schwarz

Hi all,

I have the following pseudo-c-code which parse a command line, and puts the
command and its arguments in an array, and I can't figure out where/how I
should use free:

main(void):
char ** command_arr;
parser(command_arr);

parser(char ** command_arr):
command_arr = malloc ( argument_number * sizeof(char *);
while ( there is an argument arg ) {
*(command_arr + command_arr_index) = malloc ( strlen(arg) + 1 )
strcpy (*(command_arr + command_arr_index), arg)
command_arr_index++;
}
*(command_arr + command_arr_index) = NULL;

While this is not incorrect, I believe most people would find the
equivalent subscript notation
command_arr[command_arr_index] = NULL;
a lot easier on the eyes and brain.
well, what i wonder is if, in the main fonction, free(command_arr)
deallocates 'malloced' spaces in the parser (with strlen), or should I write
something like that?
while ( *(command_arr + command_arr_index) != NULL ) {
free( *(command_arr + command_arr_index) );
command_arr_index++;
}
free(command_arr);

The value of command_arr in main is never changed by the execution of
parser. The argument was passed by value to parser (think of it as
being copied to a local variable of the same name). You promptly
throw this value away and replace it, in parser, with the value
returned by malloc. This new value is used throughout parser and then
discarded when parser returns.

What you probably want is something like

main(void):
char ** command_arr;
parser();

char ** parser(void):
char ** command_arr;
snip function code
return command_arr;


<<Remove the del for email>>
 

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

Similar Threads


Members online

Forum statistics

Threads
474,077
Messages
2,570,567
Members
47,204
Latest member
abhinav72673

Latest Threads

Top