tags:

views:

88

answers:

6

for some reason I cannot explain, every single item in the character array...is equal to the last item added to it...for example progArgs[0] through progArgs[size] contains the value of the last item.

I cannot figure out what I'm doing wrong for the life of me. Any suggestions?

int count = 0;
char *progArgs[commandList.size()]

    for(list<string>::iterator t=commandList.begin(); t!=commandList.end(); t++)
    {
        char item[strlen((*t).c_str())]; //create character string
        strcpy(item, (*t).c_str()); //convert from const char to char
        progArgs[count] = item;
        count++;
    }

edit:

Thanks for all the quick responses everyone...I see what you are talking about

+3  A: 

progArgs is an array of pointers to char.

You set each of these pointers to point to item. item is a local variable in the loop, so as soon as the loop exits, item no longer exists and the pointers are no longer valid[*]. However, in your C++ implementation, they all still point to the bit of memory which used to be the array item on the stack. This memory contains whatever it was last used for, which is the sequence of characters from the last string in the list.

If you want to copy a list of strings to an array, it would be better if possible to use an array of strings:

std::string progArgs[commandList.size()] // if your compiler has C99 VLAs as an extension
int count = 0;

for(std::list<std::string>::iterator t=commandList.begin(); t != commandList.end(); ++t) {
    progArgs[count] = *t;
    ++count;
}

Or even better, use a vector instead of an array:

std::vector<std::string> progArgs(commandList.begin(), commandList.end());

[*] to be more precise, the scope of item is a single repeat of the loop, it's nominally "created" and "destroyed" each time around. But this doesn't do any work - on your C++ implementation the same region of memory is re-used each time, and there's no work needed to create or destroy an array of char on the stack.

Steve Jessop
Copying into an array of char* can be useful for calling C APIs that expect it, such as UNIX/Linux execv(...), execvp().
Tony
@Tony: yes, although that doesn't rule out using a `vector<char>` to store the copied string data. That said, if the array of `char*` is only used for a call to exec and then immediately discarded, I don't think it's necessary to copy the strings at all. The `exec` functions don't actually modify the string data, they just take non-const for historical reasons, so for this use you could `const_cast` the result of `c_str()`. A bit tricksy just to avoid copying a few strings, I suppose.
Steve Jessop
And I guess in the case of `exec`, you're not going to need to free the memory since the process is about to be replaced, so good practices with respect to RAII are irrelevant...
Steve Jessop
@Steve: I see you've covered the same ground here I covered in comments on another answer :-).
Tony
+2  A: 

You're assigning the same pointer (the address of the first element of the stack array item) to each element of progArgs, then repeatedly overwriting that memory. You can do:

progArgs[count] = strdup(t->c_str());

and get rid of the first two lines of the for body.

strdup allocates memory, so you will have to free each element with free later. Also, you were not allocating a character for the NUL-terminator. You need would strlen + 1. However, this is not an issue with strdup, since it allocates for you.

Matthew Flaschen
perfect thanks.
Ryan
This ignores the possibility that commandList will be in scope and unchanged at least until progArgs has been used, in which case `progArgs[count] = t->c_str();` suffices and no free()ing is required.
Tony
@Tony: `progArgs[count] = t->c_str()` is illegal, since `progArgs` is an array of `char*`, not `const char*`. If this is for `exec` and friends as you guessed, then Ryan is a casualty of its dodgy but backward-compatible API.
Steve Jessop
@Steve: excellent points, though const_cast<char*>(t->c_str()) would be a more typical, direct and fast - if dodginess propagating - workaround than strdup() :-). All good anyway.
Tony
A: 

You're setting progArgs[count] to the same item every time!

int count = 0;
char *progArgs[commandList.size()]

for(list<string>::iterator t=commandList.begin(); t!=commandList.end(); t++)
{
    char * item = new char[strlen((*t).c_str()) + 1]; //create new character string
    strcpy(item, (*t).c_str()); //convert from const char to char
    progArgs[count] = item;
    count++;
}

Then remember to call delete[] for each element in progArgs.

Personally, I'd create an array of string and convert to char * on the fly as needed.

egrunin
You have the same off-by-one (missing room for NUL) as the original.
Matthew Flaschen
@Matthew Flaschen: fixed, thanks.
egrunin
A: 

The char array item is local variable and is available only within the for loop.

Instead allocate it dynamically.

codaddict
A: 

You're right:

for(list<string>::iterator t=commandList.begin(); t!=commandList.end(); t++)
{
    char item[strlen((*t).c_str())]; //create character string

...this does create an array, but it creates it on the stack, so when you reach the end of its scope, it gets destroyed.

    strcpy(item, (*t).c_str()); //convert from const char to char
    progArgs[count] = item;
    count++;
}

...and this closing brace marks the end of its scope. That means you're creating a string, putting it into your array, then destroying it before you add the next one. Thanks to the fact that you're creating them all on the stack, each new one you create is being created in exactly the same place as the previous one, so it's no surprise that at the end of it all, you have a bunch of identical strings.

Perhaps it would be better if you told us what you're trying to accomplish here, so we can help you do that.

Jerry Coffin
+1  A: 

item has scope local to the loop. The propArgs array therefore contains a bunch of stack-based pointers, likely all the same. You can examine how this works in the debugger, just step thru the loop twice and it should be clear what's going on.

By the time you exit the loop, the buffer addressed by the common pointer contains the most recently-copied c_str().

You could fix this by doing

char* item = new char[strlen((*t).c_str()) + 1];

but then you'd have to delete[] all the propArgs array entries when you exit the loop.

This code shows a fundamental lack of understanding of memory management such that further reading might be useful before restructuring the code. If the code used here was in only slightly more complex context than this example, it might just crash since any access of propArgs outside the loop would rely on an invalid (no longer in scope) item.

Steve Townsend
Your suggested fix has an off-by-one, like the original.
Matthew Flaschen
@Matthew - thanks, fixed
Steve Townsend