### 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!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113`` ``````#include #include #include 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
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.

 ``123`` ``````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')

 ``12`` ``````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...

 ``12`` `````` values = nullptr; weights = nullptr;``````
memory leak.

 ``1234`` `````` 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 .
 ``123456789101112`` ``````namespace nih{ class vector{ std::vector 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
 ``12345`` ``````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
`new_array' only holds 2 integers, when you do `new_array[0] = *weights;` you are assigning only the first element of the `weights' array.
 ``12345`` ``````int parse_input(int &num_items, int &pouch_size) { //... return *new_array; }``````