【问题标题】:Malloc array of structmalloc 结构体数组
【发布时间】:2015-07-15 09:25:49
【问题描述】:

过去几天我一直在四处寻找如何为结构数组分配内存。我在网上尝试了大多数示例都没有成功,通读了《C 编程语言》一书,但没有成功。

typedef struct {
    int testMallocOne;
    int testMallocTwo;
} test;   

这是我设置结构的地方。

然后我使用 -

声明了我的结构
struct test _testData[5]; 

这就是我卡住的地方——这是我最近一次失败的尝试

_testData = malloc (sizeof(struct test);

谁能指出我哪里出错了,从我的研究来看,我似乎在做与我见过的大多数例子一样的事情。这些都是在 main 之外声明的。

【问题讨论】:

  • 首先,你需要一个右括号malloc :)

标签: c arrays malloc heap-memory


【解决方案1】:

改变

struct test _testData[5];  // You already declared it an array 

动态分配数组

struct test *_testData = malloc(5 * sizeof(struct test));

【讨论】:

    【解决方案2】:
    struct test _testData[5]; 
    

    您已经为堆栈上的 5 个struct test 类型的对象分配了内存。

    如果要动态分配内存

    那就做吧

    struct test * ptr = malloc(sizeof(struct test) * 5);
    

    在这种情况下分配的内存在堆上。

    【讨论】:

      【解决方案3】:

      你可以简单地使用类似的东西:

      test *_testData = malloc (5 * sizeof(*_testData));
      

      由于您创建了 typedef,因此无需指定完整的结构名称,并且乘以 5 会自动为您提供所需大小的数组。

      您会注意到我还使用 *_testData 作为 sizeof 的参数 - 这绝不是必要的,但如果您将来将类型更改为其他类型,它可以最大限度地减少所需的更改。

      【讨论】:

        【解决方案4】:

        这需要更多上下文,但当你这样做时

        struct test _testData[5];
        

        你在堆栈上分配内存,所以你不需要 malloc。如果你使用 malloc,你应该将你的变量定义为一个指针,请求一块内存来分配你的整个数组并将指针分配给它:

        struct test *_testData;  
        _testData = malloc(5 * sizeof(struct test));
        

        【讨论】:

          【解决方案5】:

          使用malloc 动态创建指向堆中5 个struct test 项的数组的指针:

          struct test *ptr = malloc(sizeof(struct test) * 5);
          

          并且您必须在使用后释放指针以防止内存泄漏

          而你的代码声明:

          struct test _testData[5];
          

          将固定内存分配给堆栈中的struct test 项数组。这将在使用后由编译器自动释放。

          【讨论】:

            【解决方案6】:
            • 使用struct test _testData[5];

              struct test _testData[5];
              // to malloc
              for(int i = 0; i < sizeof(_testData)/sizeof(_testData[0]); i++)
                  _testData[i] = malloc (sizeof(struct test));
              
              // do something to _testData[0],_testData[1]... 
              // to free
              for(int i = 0; i < sizeof(_testData)/sizeof(_testData[0]); i++)
                  if(_testData[i]) {
                      free (_testData[i])
                  };
              
            • 使用struct test *p;

              struct test *p;
              int num = 5;
              // to malloc
              p = malloc (sizeof(struct test)*num);
              // do something to p[0],p[1]...
              // to free
              if(p){
                 free(p);
              } 
              

            【讨论】:

              【解决方案7】:
              /* Hope this small prog will help you to understand */
              #include<stdio.h>
              #include<stdlib.h>
              
              struct test
              {
                      int testMallocOne;
                      int testMallocTwo;
              } test; 
              
              typedef struct test node;
              
              int main ( void )
              {
                      int i=0;
                      node *ptr[5];
              
                      for ( i=0; i<5 ; i++ )
                      {
                              ptr[i] = malloc (sizeof (node ));
                      }
                      for ( i=0; i<5; i++ )
                      {
                              printf ("\n enter the value for testMallocOne " );
                              scanf ( "%d", &(ptr[i]->testMallocOne) );
              
                              printf ("\n enter the value for testMallocTwo " );
                              scanf ( "%d", &(ptr[i]->testMallocTwo) );
              
                      }
              
                      printf ( "\n here is the data \n");
              
                      for ( i=0; i<5; i++ )
                      {
                              printf ( "\n value of i = %d and testMallocOne is  %d",i, ptr[i]->testMallocOne );
                              printf ( "\n value of i = %d and testMallocTwo is  %d",i, ptr[i]->testMallocTwo );
                      }
              
                      return ( 0 );
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2018-06-21
                • 1970-01-01
                • 2021-05-13
                • 2012-06-04
                • 2015-07-30
                • 2015-02-12
                • 1970-01-01
                相关资源
                最近更新 更多