I created a simple shell program that
_displays a prompt
_receives user input using readline()
_splits the input into words based on where spaces are found in the input string
_the words are then put in an array (which is MALLOC'ed);
_fork() is used to create a child process
_i then pass this array to call a program from the child process using execve()
execve(myarray[0], myarray, env);
and the current process waits for it.
MY QUESTION IS:
Do i need to free the MALLOC'ed memory of myarray
?
Here is a part of the code
child_pid = fork();
42 if (child_pid == -1)
43 {
44 perror(argv[0]);
45 free(linebuffer);
46 return (2);
47 }
48
49 if (child_pid == 0)
50 {
51 /*strsplit() returns MALLOC'd array*/
52 splitted_str = _strsplit(linebuffer, ' ');
53
54 if (execve(splitted_str[0], splitted_str, env) == -1)
55 {
56 perror(argv[0]);
57
/*Function that frees a NULL terminated array*/
free_array(splitted_str);
58 free(linebuffer);
59
60 return (2);
61 }
62 }
63 else
64 {
65 wait(NULL);
66 }