0

I am completely stuck on this problem... The code structure that is given is as follows:

typedef struct _myvar{
   uint32_t someInt;
   uint32_t *ptr;
} myvar;
...
myvar **var;
..
var = new myvar*[x]; // where x is an int
for(int i = 0; i < y; i++){ // y is usually 2 or 4
   var[i] = new myvar[z]; //create dynamic 2d array
   for(int j = 0; j < z; j++){
      var[i][j].ptr = new int[b]; //where b is another int
   }
}

What happens is I want to create a 2d structure that has one part of it being 3d i.e. the ptr is just a pointer to an array of ints.

My code compiles but I get seg faults in trying to allocate memory so ptr can point to it. I tried working on this for about an hour and figure it was time for some help.

Thanks!

EDIT1: Fixed code issue in regards to comment on code not compiling.. Secondly... I cannot use vectors as much as I would like to... The data structure that I have there is what I have to use.

EDIT2: b is dynamic and set at the command line. For testing purposes use 16.

9
  • Use std::vector wherever possible because it maintains copies of items and manages dynamic memory allocation so you don't have to. Commented Jan 30, 2015 at 0:38
  • That code won't compile. var = new myvar[z] will not work on myvar **var. Commented Jan 30, 2015 at 0:38
  • and have you tried (var[i])[j].ptr? Commented Jan 30, 2015 at 0:45
  • just did and I still get a seg fault when trying to access it... so i.e. cout << myvar[i][j].ptr[k]. where i, j, k should be within the array bounds that were previously used to declare it. Commented Jan 30, 2015 at 0:47
  • so whats you problem now - you say 'fixed the code issu' Commented Jan 30, 2015 at 0:49

1 Answer 1

1

I think you are getting confused with your indices. In your first loop you use var[i], so i must stop at x, not at y. For the columns I have used j and y. Not sure what z is. Then, as pointed out by others, you shouldn't mix int and uint32_t. Try this:

#include <iostream>
using namespace std;

typedef struct _myvar{
   uint32_t someInt;
   uint32_t *ptr;
} myvar;

int main() {

    myvar **var;

    int x = 3;
    int y = 4;

    var = new myvar*[x]; // where x is an int
    for(int i = 0; i < x; i++) { // i stops at x
        var[i] = new myvar[y]; //create dynamic 2d array
        for(int j = 0; j < y; j++){
            var[i][j].ptr = new uint32_t[16]; // used 16 for testing
        }
    }


    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Okay so I have exactly what you have here and I still cannot access any part of ptr. i.e. if I want to set var[0][0].ptr[0] = 0; (numerical 0 not pntr) I get a seg fault. This is were I am confused...
@Sharki Could you please post your precise code? I have tried this: var[0][0].ptr[0] = 0; cout << "var[0][0].ptr[0] is " << var[0][0].ptr[0] << endl; var[2][3].ptr[15] = 15; cout << "var[2][3].ptr[15] is " << var[2][3].ptr[15] << endl; and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.