ryan
ryan

Reputation: 153

Pointer and Struct Issues

I am having trouble understanding why this piece of code is not working properly.

#include <stdio.h>
#include <stdlib.h>
#include "runSolver.h"

typedef struct testStruct {
  double *x[11];
  double *u[10];
} Test;

typedef struct returnStruct_t {
  Test* vars;
} ReturnStruct;

void initalize_returnStruct(void** returnStruct){
  ReturnStruct* new_returnStruct = (ReturnStruct *)malloc(sizeof(new_ReturnStruct));
  Test* varsStruct = (Test*)malloc(sizeof(Test)*3);

  int dataSize = 5;
  int i;
  for(i = 0; i < 3; i++){
    int x;
    for(x = 0; x < 11; x++){ 
      fprintf(stderr, "i:%d and x:%d for x\n", i, x);
      varsStruct[i].x[x] = (double *)malloc(sizeof(double)*5);    
    }
    for(x = 0; x < 10; x++){
      fprintf(stderr, "i:%d and x:%d for u\n", i, x);      
      varsStruct[i].u[x] = (double *)malloc(sizeof(double)*5);    
    }
  }

  new_returnStruct->vars = varsStruct;
  *returnStruct = new_returnStruct;
}

This function is called via python's ctypes:

#!/usr/bin/python

import ctypes as ct

class returnStruct_t(ct.Structure):
    _fields_ = [("vars", Test_t*3)]

runSolver = ct.CDLL('./runSolverParallel.so')

varsd = ct.c_void_p()
runSolver.initalize_returnStruct(ct.byref(varsd)) 

What I am trying to do is the following:

When I run the code as listed it segfaults on the first iteration of the second inner for loop when I am trying to allocate space for new_returnStruct->vars[0].u[0]. If I totally eliminate the decleartion of double *u[10] and its instatiation I get a segfault on the line where I set returnPointer to point to ReturnStruct : *returnStruct = new_return;. Honestly I am out of ideas as to what the hell is going on. Any help would be great.

PS: I am using double *x[11]; double *u[10]; rather then, double **x; double **u;, because the struct Test is not the real struct I will using. The real struct is defined elsewhere in a library and has those two variables as well as about 20 some other double * variables. I want to eventually replace Test with that struct but I want to get this code working with Test first. Just for reference the real struct Test will be replaced with is:

typedef struct Vars_t {
  double *u_0; /* 1 rows. */
  double *u_1; /* 1 rows. */
  double *u_2; /* 1 rows. */
  double *u_3; /* 1 rows. */
  double *u_4; /* 1 rows. */
  double *u_5; /* 1 rows. */
  double *u_6; /* 1 rows. */
  double *u_7; /* 1 rows. */
  double *u_8; /* 1 rows. */
  double *u_9; /* 1 rows. */
  double *x_1; /* 5 rows. */
  double *x_2; /* 5 rows. */
  double *x_3; /* 5 rows. */
  double *x_4; /* 5 rows. */
  double *x_5; /* 5 rows. */
  double *x_6; /* 5 rows. */
  double *x_7; /* 5 rows. */
  double *x_8; /* 5 rows. */
  double *x_9; /* 5 rows. */
  double *t_01; /* 1 rows. */
  double *t_02; /* 1 rows. */
  double *t_03; /* 1 rows. */
  double *t_04; /* 1 rows. */
  double *t_05; /* 1 rows. */
  double *t_06; /* 1 rows. */
  double *t_07; /* 1 rows. */
  double *t_08; /* 1 rows. */
  double *t_09; /* 1 rows. */
  double *t_10; /* 1 rows. */
  double *x_10; /* 5 rows. */
  double *u[10];
  double *x[11];
} Vars;

Upvotes: 0

Views: 117

Answers (2)

NoDataFound
NoDataFound

Reputation: 11949

There are some errors in your code:

This line hould be ReturnStruct and not new_returnStruct as parameter of sizeof:

ReturnStruct* new_returnStruct = (ReturnStruct *)malloc(sizeof(new_returnStruct));

Are you sure that sizeof(Test) == sizeof(Pee)*3 or in fact, why you're using Pee instead of Test ?

Test* varsStruct = (Test*)malloc(sizeof(Pee)*3);

Upvotes: 2

siu
siu

Reputation: 312

In your code you are allocating memory for ReturnStruct* pointer not the actual struct Instead of

   ReturnStruct* new_returnStruct = (ReturnStruct *)malloc(sizeof(new_returnStruct));

try this

   ReturnStruct* new_returnStruct = (ReturnStruct *)malloc(sizeof(ReturnStruct));

Upvotes: 1

Related Questions