Why does this program segfault?

I have this project for a class and the program is to print out a randomized sequence of the numbers from 0 to 9, Using a really dumb algorithm. I need to find the the problems and not necessarily fix it. The problem I found so far is on line 18 "num_left++;" as a uninitialized local variable, but I don't know what that means.


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
35
36
#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

int main()
 {
	string *str[10];
	int num_left;
	int pick;
	string *sptr;

	// Fill array with freshly-constructed strings
	for (int i = 0; i < 10; i++) 
	{
		str[i] = new string(1, static_cast<char>('0' + i));
		num_left++;
	}

	// Now, scramble by picking a slot at random, making sure it's not
	// already been processed, then printing, deleting and nulling out
	while (num_left > 0)
	{
		pick = rand() % 10;
		sptr = str[pick];
		if (sptr = NULL) 
		{  // NULL indicates we already processed this;
			continue;       // skip and continue looping
		}
		cout << *str[pick] << endl;
		delete str[pick];
		str[pick] = NULL;
		--num_left;
	}
}
Last edited on
1
2
3
4
		if (sptr = NULL) 
		{  // NULL indicates we already processed this;
			continue;       // skip and continue looping
		}

you're doing assignment (=), not testing for equality (==)...

Why are you combining arrays of std::strings with dynamic allocation? Why not just have an array of string objects instead of dynamically-allocated pointers? If it's just for practice, I guess that's fine, but it seems like you're just unnecessarily complicating things.

Also consider std::vectors
http://en.cppreference.com/w/cpp/container/vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
 
int main()
{
    // Create a vector containing integers
    std::vector<std::string> v = {"begin"};
 
    // Add two more integers to vector
    v.push_back("hello");
    v.push_back("world");
 
    // Iterate and print values of vector
    for (const std::string& s : v) {
        std::cout << s << '\n';
    }
}
Last edited on
Well is an assignment and I'm trying to figure out what's wrong with this program. I still can't pass from
1
2
str[i] = new string(1, static_cast<char>('0' + i));
		num_left++;


On line 27, you are assigning NULL to sptr. I do not believe that this is what you intend.

Your other problem, that I should have noticed before, is that you never set num_left to an initial value before incrementing it.
This is what an "uninitialized local variable" is.

The fix:
Instead of int num_left;, do int num_left = 0;
Last edited on
So far, lines 15-19, it looks like you're trying to make an array of strings, for some reason using the Fill constructor with size 1.

["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];

Are you sure you didnt want to use a single string , also known as an array of characters?

This makes the next randomization part a bit confusing. You also need to be very careful about deleting while still working with that string.

Think about what you're really trying to accomplish here. It appears you're going through 10 times, and I think you're trying to assign a string to NULL -- I believe you can't do this and you could instead try assigning a string to empty string (""). If it was a character, you could assign it to the null terminator ('\0') character. Also, do you need to show the string changing over time (printing it out with each iteration of loop)? Think about the logic before diving into semantics.

As for num_left, I think it doesn't like you atttemping to increment something that doesn't have an initial value. Change line10 to say int num_left=0;
Topic archived. No new replies allowed.