Garbage in the 0th element of a char* array

A

Amrit Kohli

Hello.

I have the following code, to do a simple operation by copying the elements
of a vector of strings into an array of char pointers. However, when I run
this code, the first element in the char array strarr[0] holds garbage
characters. For some reason, every time the strcpy function is called to
copy the string in temp to the array, it fills the 0th element in the strarr
array with garbage characters. Can someone try to compile this code and
debug it and see if they get the same results that I'm getting? Any insight
into why this might be happening? I'm really stumpped. The code works for
every other element in strarr except for the 0th element. I've even tried
directly setting the strarr[0]th element to the pointer of temp with this
statement:

strarr = temp; // where i = 0

and the pointer value gets set, but the characters are still garbage. When
I look at the value of strarr[0] in the debugger, no matter how I assign the
temp string to the array index, it always comes out as garbage, even when
the pointer is equal to the value of temp. The temp variable holds the
correct string, but for some reason, it won't properly copy into the 0th
element of the array.

Any help on this would be greatly appreciated.

Thanks -

#include <iostream>
#include <string>
#include <vector>
#include <bitset>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main()
{
vector<string> str;
string s;
while(cin >> s)
str.push_back(s);
const size_t array_size = 100;
char *strarr[array_size];
int i = 0;
for(vector<string>::iterator iter = str.begin();iter !=
str.end();++iter, ++i)
{
size_t len = (*iter).length() + 1;
strarr = new char[len];
char *temp = new char[len];
strcpy(temp, (*iter).c_str());
strcpy(strarr, temp);
}
return 0;
}
 
J

jmd

I created a C++ Win32 project in Visual Studio 2005 containing your code.
Compile & link without error.
Run also without error !
. starr was set correctly with the addresses of the first char of each
C-style string.
. each memory at the corresponding address contains the correct entered
characters.

Good luck.
jean-marie
 
T

Tom Widmer [VC++ MVP]

Amrit said:
Hello.

I have the following code, to do a simple operation by copying the elements
of a vector of strings into an array of char pointers. However, when I run
this code, the first element in the char array strarr[0] holds garbage
characters. For some reason, every time the strcpy function is called to
copy the string in temp to the array, it fills the 0th element in the strarr
array with garbage characters. Can someone try to compile this code and
debug it and see if they get the same results that I'm getting? Any insight
into why this might be happening? I'm really stumpped. The code works for
every other element in strarr except for the 0th element. I've even tried
directly setting the strarr[0]th element to the pointer of temp with this
statement:

strarr = temp; // where i = 0

and the pointer value gets set, but the characters are still garbage. When
I look at the value of strarr[0] in the debugger, no matter how I assign the
temp string to the array index, it always comes out as garbage, even when
the pointer is equal to the value of temp. The temp variable holds the
correct string, but for some reason, it won't properly copy into the 0th
element of the array.


This suggests to me that you are using the debugger incorrectly. If two
pointers have the same value, then they point to the same characters! If
you're seeing something strange in the debugger, the best option is to
put some output statements in the code to see what's going on.

The code looks ok as far as it goes, apart from memory leaks.
for(vector<string>::iterator iter = str.begin();iter !=
str.end();++iter, ++i)
{
size_t len = (*iter).length() + 1;
strarr = new char[len];
char *temp = new char[len];
strcpy(temp, (*iter).c_str());
strcpy(strarr, temp);


You've got a memory leak here - the dynamic array you assigned to temp
is no longer accessible, and therefore leaks. You should instead have
something like:
size_t len = iter->length() + 1;
strarr = new char[len];
strcpy(strarr, iter->c_str());

You also need to clean up strarr at the end.

Tom
 

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

Top