指针和结构问题

时间:2014-08-29 19:39:39

标签: c pointers struct

我无法理解为什么这段代码无法正常工作。

#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;
}

此功能通过python&#39; 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)) 

我要做的是以下内容:

  • 传入一个指向名为returnPointer

  • 的指针的void指针
  • 实例化指向ReturnStruct

  • 的指针
  • 实例化指向类型为Test

  • 的大小为3的数组的指针
  • 实例化Test个结构的数据成员xu

  • returnPointer指向完全实例化的ReturnStruct

当我运行所列出的代码时,当我尝试为new_returnStruct->vars[0].u[0]分配空间时,它会在第二个内部for循环的第一次迭代中发生段错误。如果我完全消除了double *u[10]及其实例的decleartion,我会在我设置returnPointer以指向ReturnStruct*returnStruct = new_return;的行上出现段错误。老实说,我不知道到底发生了什么。任何帮助都会很棒。

PS:我使用double *x[11]; double *u[10];而不是double **x; double **u;,因为结构Test不是我将使用的真正结构。真正的结构在库中的其他地方定义,并且具有这两个变量以及大约20个其他double *变量。我想最终用该结构替换Test,但我希望首先使用Test来使用此代码。仅供参考,真正的结构Test将替换为:

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;

2 个答案:

答案 0 :(得分:2)

您的代码中存在一些错误:

这一行应该是ReturnStruct而不是new_returnStruct作为sizeof的参数:

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

您确定sizeof(Test) == sizeof(Pee)*3或事实上,为什么您使用Pee代替Test

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

答案 1 :(得分:1)

在您的代码中,您为ReturnStruct *指针而不是实际的struct分配内存 而不是

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

试试这个

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