473,414 Members | 1,628 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,414 software developers and data experts.

Garbage in the 0th element of a char* array

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[i] = 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[i] = new char[len];
char *temp = new char[len];
strcpy(temp, (*iter).c_str());
strcpy(strarr[i], temp);
}
return 0;
}
Nov 17 '05 #1
2 2022
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
"Amrit Kohli" <am***@wayne.edu> wrote in message
news:ei**************@TK2MSFTNGP14.phx.gbl...
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[i] = 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[i] = new char[len];
char *temp = new char[len];
strcpy(temp, (*iter).c_str());
strcpy(strarr[i], temp);
}
return 0;
}

Nov 17 '05 #2
Amrit Kohli wrote:
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[i] = 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[i] = new char[len];
char *temp = new char[len];
strcpy(temp, (*iter).c_str());
strcpy(strarr[i], 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[i] = new char[len];
strcpy(strarr[i], iter->c_str());

You also need to clean up strarr at the end.

Tom
Nov 17 '05 #3

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

Similar topics

10
by: Noah Spitzer-Williams | last post by:
Hello guys, I'm itinerating through my array using pointers in this fashion: image is unsigned char image do { cout << "image byte is: " << *image << endl;
3
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database...
3
by: Travis L Spencer | last post by:
Hey, I am trying to insert a bunch of Token objects into a collection called tokens of type deque<Token>. The problem is that the output of Tokens::print is just junk. For instance, the call at...
12
by: leonard.guillaume | last post by:
Hi guys, I use dynamic char arrays and I'm trying to get rid of the garbage in it. Let me show you the code and then I'll explain more in details. ...
142
by: jacob navia | last post by:
Abstract -------- Garbage collection is a method of managing memory by using a "collector" library. Periodically, or triggered by an allocation request, the collector looks for unused memory...
5
by: junky_fellow | last post by:
Hi, I discussed about this earlier as well but I never got any satisfactory answer. So, I am initiating this again. Page 84, WG14/N869 "If both the pointer operand and the result point to...
28
by: onkar | last post by:
This idea might be vey crazy. But I hope to get answers to this .. from comp.lang.c If a compiler is designed such that it automatically adds a free() matching every malloc() then is it not a...
21
by: arnuld | last post by:
int main() { const char* arr = {"bjarne", "stroustrup", "c++"}; char* parr = &arr; } this gives an error: $ g++ test.cpp test.cpp: In function 'int main()': test.cpp:4: error: cannot...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.