I believe this wouldn't be allowed in C since I have to allocate all memory in advance so by allocating the argv to be 10 elements, I can't then go and define each one individually.. or can I?

I know the + modifier doesn't work in C, but I'm unsure what function I would need to replicate it (I realise I can use strcat to add to an existing defined array, but didn't think this would work with a newly formed array)

4 Answers
4

General malloc

man malloc may be helpful. malloc takes the minimum number of bytes you need of memory (i.e. malloc can choose to provide you more). So if you need exactly 10 elements in your char array, it may be best to simply allocate char* argv[10] as you have already done. However, this creates a container for exactly 10 char* which are not yet defined. Thus, for each char*, argv[0]...argv[9] you can define exactly what goes in there. For instance, if you want to malloc a string of size 200 for argv[0], you would use a statement as follows (notice that 200 can be held in either a constant or a variable):

argv[0] = malloc(200 * sizeof(char));

Generally, sizeof(char) == 1 byte, so this value is probably going to try to get 200 bytes. However, at this point you can modify argv[0] in any way you need to (i.e. strncpy, strncat, etc.).

Now, if you do not know how many arguments you may have, you can allocate your container on the fly. So instead of char* argv[10], you can try to allocate a char** argv. To do this, you would execute the following statement:

Often times the sizeof(char*) == 4 bytes on a 32-bit system (size of a typical pointer). Now you can use this chunk of memory, argv, in a similar way that has been done before. For ease of thinking about this, using malloc in this way allowed you to perform a relatively equivalent operation of char* argv[WITH_SOME_DYNAMIC_NUMBER]. Thus, you can manipulate this new container in a similar way as I have described above.

Remember though, when you are done with memory created by malloc, you must call free or else it will not be deallocated until the program terminates.

Your Problem

If I understand your question correctly, you have a flattened string which you want to turn into a string array for execve. I will work out a simple example trying to explain one of the many ways this can be done.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void someMethod()
{
char* argv[10];
char* path = getMyPath();
// Notice - this is a little messy and can/should be encapsulated away in another
// method for ease of use - this is to explicitly show, however, how this can work.
argv[9] = malloc((strlen(path) + strlen("--path=") + 1) * sizeof(char));
strncpy(argv[9], "--path=", strlen("--path="));
argv[9][strlen("--path=")] = '\0'; // NULL-terminate since strncpy doesn't
strncat(argv[9], path, strlen(path));
// Do stuff with array
printf("%s\n", argv[9]);
// Technically, you should never get here if execve succeeds since it will blow this
// entire program away (unless you are fork()'ing first)
free(argv[9]);
}

Can you give me a sample on how to malloc the memory that it'll use? I think my biggest issue is that its a array of an array of chars here, probably just confusing myself.
–
John MitchellApr 6 '13 at 17:25