1

Hello I am struggling to find a solution Ive defined the following 3D array of structures.

typedef struct{
  float x;
  float y;
  float z;
} Point;

Point ***Qw;

Qw =  malloc(num_bezier * sizeof(Point **));
for(i=0; i<num_bezier; i++){
    Qw[i] =  malloc((m+1) * sizeof(Point *));

    for(j=0; j<=m;j++)
      Qw[i][j] = malloc((p+1) * sizeof(Point));
  }

I can loop through the array to print its contents but at some point of the program after modifying some of the elements, Im no longer able to access some of the structs in the array and i get a segfault. Any help appreciated, thanks.

PD: Ive just noticed i had defined incorrectly my struct...

typedef struct{
 double x;
 double y;
 float z;
} Point;

As soon as i exchanged the double for float type it fixed the segfault... still trying to figure out why it was segfaulting

4
  • 2
    Did you try running your program in valgrind? Maybe you are messing up your indices. Commented Oct 13, 2015 at 13:47
  • 2
    The shown code looks correct. The problem has to be somewhere else. Commented Oct 13, 2015 at 13:47
  • 1
    Please post a minimal reproducible example Commented Oct 13, 2015 at 14:00
  • As soon as i exchanged the double for float type it fixed the segfault. It's not fixed. There is still a problem hidden in your code which could surface elsewhere again. Commented Oct 13, 2015 at 22:37

1 Answer 1

1

Consider allocating a single buffer (with pow( num_bezier, 3) elements) instead of allocating rows, columns and cells separately. That way you can avoid excess allocation but also memory fragmentation.

struct Point { float x, y, z; }

size_t bufferLength;
size_t bufferSquareLength;
struct Point* buffer; // pointer to a dynamically-allocated cubic buffer of Point values, stored in row x, column y, and depth z order.

void allocateBuffer(size_t squareLength) {
    bufferSquareLength = squareLength;
    bufferLength = squareLength * squareLength * squareLength;
    buffer = calloc( bufferLength, sizeof(struct Point) );
}

struct Point* getElement(size_t x, size_t y, size_t z) {

    Point* p = buffer + ( x * bufferSquareLength * bufferSquareLength ) + ( y * bufferSquareLength ) + z;
    return p;
}

Much simpler and easier to follow.

To iterate over each value:

void forEachPoint( void(*callback(size_t x, size_t y, size_t z, struct Point* value) ) {
    for(size_t x = 0; x < bufferSquareLength; x++) {
        for(size_t y = 0; y < bufferSquareLength; y++) {
            for(size_t z = 0; z < bufferSquareLength; z++) {
                callback( x, y, z, getElement( x, y, z ) );
            }
        }
    }
}

Simply call forEachPoint with a custom callbakc function to iterate over each struct Point value.

Sign up to request clarification or add additional context in comments.

Comments

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.