Segmentation Fault Help

I know this is a longshot, but I'm brand new to C++, and my code keeps giving me a segmentation fault. I don't know much about using pointers so I feel like that could be the issue. It also could be me trying to initialize 2D arrays with the wrong syntax. Should be an easy find for the experts. Thanks guys!

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
#include <sstream>

int parse_input(int &num_items, int &pouch_size);

int calculate(int &num_items, int &pouch_size, int values[], int weights[]);


int main()
{
	int num_items;
	int pouch_size;
	int my_array[2] = { 0 };
	*my_array = parse_input(num_items, pouch_size);
	calculate(num_items, pouch_size, &my_array[0], &my_array[1]);

	return 0;
}

int calculate(int &num_items, int &pouch_size, int values[], int weights[])
{
	int **M = new int *[((num_items + 1)*(pouch_size + 1))];
	int **B = new int *[((num_items + 1)*(pouch_size + 1))];

	for (int i = 1; i < num_items; i++)
	{
		for (int j = 0; j < pouch_size; j++)
		{
			if (weights[i] <= j)
			{		
				if (M[i-1][j-weights[i]] + values[i] > M[i-1][j])
				{
					M[i][j] = M[i-1][j-weights[i]] + values[i];
					B[i][j] = i;
				}

				else
				{
					M[i][j] = M[i-1][j];
					B[i][j] = 0;
				}
			}

			else
			{
				B[i][j] = M[i-1][j];
				B[i][j] = 0;	
			}	
		}
	}
	int r = num_items;
	int max_value;
	int s = pouch_size;
	int *output = new int(num_items + 1);
	int out_index = 1;
	while (r > 0 && s > 0)
	{
		if (B[r][s] == 0)
		{
			r--;
		}

		else
		{
			output[out_index] = B[r][s];
			out_index++;
			s = s - weights[r];
			r--;
		}

	}

	max_value = M[num_items][pouch_size];
	delete[] M;
	delete[] B;
	return max_value;
}

int parse_input(int &num_items, int &pouch_size)
{
	std::string x;
	std::string y;
	std::getline(std::cin, x);
	std::getline(std::cin, y);
	num_items = std::stoi(x);
	pouch_size= std::stoi(y);
	std::cout << num_items << ' ' << pouch_size << std::endl;
	int *values = new int((num_items + 1) * (pouch_size + 1));
	int *weights = new int((num_items + 1) * (pouch_size + 1));
	int i = 1;
	int j = 1;
	for (std::string line; std::getline(std::cin, line); )
	{
		std::stringstream info(line);
		std::string A[100] = { NULL };
		for (std::string key; std::getline(info, key, ','); )
		{
			A[i] = key;
			i++;
		}
		weights[j] = std::stoi(A[1]);
		values[j] = std::stoi(A[2]);
		j++;
	}
	std::cout << values << std::endl;	
	int *new_array = new int[2];
	new_array[0] = *weights;
	new_array[1] = *values;
	values = nullptr;
	weights = nullptr;
	return *new_array;
}	





[/code]
Last edited on
Can you please edit your post and format your code. It's almost impossible to read.
See this->code tags..
http://www.cplusplus.com/articles/jEywvCM9/

Good luck.
Sorry guys, it's formatted now! Also the code isn't quite finished which can explain if it doesn't make sense
Last edited on
http://www.cplusplus.com/reference/vector/vector/


http://www.cplusplus.com/forum/general/112111/
provide example input, some comments wouldn't hurt either.


1
2
3
int **M = new int *[((num_items + 1)*(pouch_size + 1))];
//later
M[i-1][j-weights[i]]

you have reserved a lot of pointers, but no integers.
`M[i-1]' is unitialized, you can't dereference it
(same for `B')


1
2
int *output = new int(num_items + 1); //note: parenthesis()
output[out_index] = B[r][s];

You have reserved just one integer, and initialized with `num_items+1' as value
Last edited on
it's for a school project and we're not permitted to use vectors .

However the input is as follows

<integer>
<integer>
<string>,<integer>,<integer>
<string>,<integer>,<integer>
<string>,<integer>,<integer>
<string>,<integer>,<integer>
...

Last edited on
> std::string A[100] = { NULL };
http://www.cplusplus.com/reference/string/string/string/
Copies the null-terminated character sequence (C-string) pointed by s.
you are trying to dereference null pointers.
If you want empty strings then simply std::string A[100];


> However the input is as follows
> <string><integer><integer>
> for (std::string key; std::getline(info, key, ','); )
your code is looking for ',' as a field separator, but your input is nothing like that...


1
2
	values = nullptr;
	weights = nullptr;
memory leak.

1
2
3
4
	int *new_array = new int[2];
	new_array[0] = *weights;
	new_array[1] = *values;
	return *new_array;
¿all that work for just two numbers?


> we're not permitted to use vectors .
1
2
3
4
5
6
7
8
9
10
11
12
namespace nih{
   class vector{
       std::vector<int> data;
   public:
      int &operator[](int index);
      int operator[](int index) const;
      vector(int size);
      vector(const vector&);
      vector& operator=(const vector&);
      ~vector();
   };
}
use nih::vector, the methods would be simply forwarding to `data' (the destructor would be empty).
after you get a working code, change data to int *data; and code the member functions properly.

given that you need a 2d matrix you may do
1
2
3
4
5
namespace nih{
   class matrix2d{
      nih::vector data;
   };
}
changing the implementation of nih::vector would not affect nih::matrix2d.

Only if you want.
weights and values are arrays
¿your point?
`new_array' only holds 2 integers, when you do new_array[0] = *weights; you are assigning only the first element of the `weights' array.

And then
1
2
3
4
5
int parse_input(int &num_items, int &pouch_size)
{
	//...
	return *new_array;
}
you only return the first element of `new_array'
Last edited on
Topic archived. No new replies allowed.