473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

malloc & free

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
Nov 13 '05 #1
4 2372
Cadderly <iv*******@yahoo.com> wrote:
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
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #2
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 :)

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;


Nov 13 '05 #3
Cadderly <iv*******@yahoo.com> wrote:
> 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;

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
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #4
On Sun, 24 Aug 2003 13:24:09 +0200, "Cadderly" <iv*******@yahoo.com>
wrote:
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>>
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: spasmous | last post by:
Yo, I'm running a subroutine that mallocs a 10 or so Mb, then frees it on exit. Then when it's called again it mallocs/frees again, and so on for maybe 100 calls. Sometimes the code fails...
13
by: Steve Zimmerman | last post by:
Esteemed contributors to clc: Thank you for all the responses. Experiments 2 and 3 below are identical, except that experiment 2 does not call free(), while experiment 3 does. With such a...
34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
27
by: Chess Saurus | last post by:
I'm getting a little bit tired of writing if (a = malloc(...) == NULL) { // error code } I mean, is it really possible that a malloc call could fail, except in the case of running out of...
1
by: Kevin | last post by:
Hi all, I clearly have an issue with some pointers, structures, and memory allocation. Its probably pritty basic, but I'm a little stuck. Any help would be greatly appreciated. I'd like...
3
by: Petr Pavlu | last post by:
Hello, I have two questions how the functions should be written. I read the FAQ but didn't find any answer. If there is any please point me out. I. Cleanup code Consider I have to open file1,...
7
by: somenath | last post by:
Hi All , As a process of my C language learning I was trying to learn how malloc can be implemented from K&R2 .But I am not able to understand few points . It would be very much helpful if...
9
by: anon856525 | last post by:
Hi I recently downloaded Turbo C v2.01 from the borland web site where its being given out for free. Has anyone else tried this? Ive been using it for some months now and hav'nt noted any...
25
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.