Why this prints Garbage?

I'm going over an exercise from a book, which basically says the following:

Write a program that defines a vector of pointers to strings. Read the vector, printing each string and its corresponding size.

I have the following solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>

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

int main() {
	vector<char*> p_str;
	int i = 0;
	string line;
	cout << "Enter a phrase: ";
        //outer while
	while(getline(cin, line) && (i < 3) ) {
		char *ptr = new char[line.size() + 1];
		int index = 0;
		while(index < line.size()) {
			*(ptr + index) = line[index];
			++index;
		}
		p_str.push_back(ptr);
		++i;
		cout << "Enter a phrase: " ;
	}
	
	for(vector<char*>::iterator iter = p_str.begin(); iter != p_str.end(); ++iter) {
		cout << *iter << " " << sizeof(*iter) - 1;
		delete *iter;
	}	
	//delete [] ptr;
	return 0;
}


but I don't know why it's printing garbage. the outer while is also wrong because it goes beyond the third iteration, and I can't find the reason.

I hope somebody can help me out, I'm lost here.

Best Regards,

Jose.

PD. is this the correct way to print the size ?
Last edited on
You want to check i < 3 first; as it stands, it will do a getline() regardless of whether i < 3 or not. Also, IIRC getline() doesn't return a Boolean condition you can loop on. Inside the assignment loop (basically your version of strcpy(), it looks like) you aren't null-terminating the string. Finally, to get the size of a C-string, use strlen().

That's all I see right now.
Just a quick glance (there may be other mistakes) reveals:

1. use strlen() instead of sizeof() for char*
2. Line 30, if you new [], you must delete [] each of your strings
3. to avoid the mess of the inner loop, consider p_str.push( strdup( line.c_str() )); but you must call free() in this case. Or alternatively, just use vector<string> p_str and stuff the strings right in. STL is copy-by-value so it'll do a lot of the work for you.
Last edited on
Helo guys, I've changed the code following your suggestions, and now it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
vector<char*> p_str;
	
	int i = 0;
	string line;
	cout << "Enter a phrase: " ;
	while( i < 3 ) {
		getline(cin, line); 
		char *ptr = new char[line.size() + 1];
		int index = 0;
		while(index < line.size()) {
			*(ptr + index) = line[index];
			++index;
		}
		*(ptr + index) = '\0';
		p_str.push_back(ptr);
		cout << "Enter a phrase: ";
		++i;
	}
	
	for(vector<char*>::iterator iter = p_str.begin(); iter != p_str.end(); ++iter) {
		cout << *iter << " " << strlen(*iter) << " ";
                //delete statement
		delete *iter;
	}


However I have one more doubt.. Is the delete statement delete *iter; wrong ?
if it is, how should I change it and why ?

Thank you guys, magnificent lessons.

Jose
However I have one more doubt.. Is the delete statement delete *iter; wrong ?


1
2
3
4
5
vector<char*> p_str;
...
char *ptr = new char[line.size() + 1];
...
p_str.push_back(ptr);


You new char[] so therefore when you free should be delete [] something.

delete [] (*iter);
Topic archived. No new replies allowed.