简体   繁体   English

带指针和数组的typedef结构

[英]typedef structs with pointers and arrays

 #include <stdio.h>
 #define DEFAULT_CAPACITY 5

Here's my typedef'ed struct - I am not allowed to change this. 这是我的typedef定义的结构-不允许更改。

typedef struct Vector
{
   int items[DEFAULT_CAPACITY];
   int size;   
}*VectorP;

*VectorP is already a pointer which confuses me a lot. * VectorP已经是一个使我感到困惑的指针。

Next I'm initializing the vector, calling malloc 接下来,我初始化向量,调用malloc

void initVector(VectorP vector)
{
      vector = (VectorP)malloc(DEFAULT_CAPACITY * sizeof(VectorP));

      if(vector == NULL)
       {
           fprintf(stderr, "Memory allocation failed!\n");
           exit(1);
       } 

   (*vector).size = 0;        //Does this change the size on the main function?
   vector->items[0] = 1;      // And this one too?
 }

And Here's my main function: 这是我的主要功能:

int main()
{
  // Create a new vector ... check size
    struct Vector vector;
    VectorP v1 = &vector;
    initVector(v1);
    fprintf(stderr, "\nThe size is (0) %i\n", v1->size);

    printf("items[0] = %i ", v1->items[0]);        
}

The problem is that function initVector didn't change the size or the item. 问题是initVector函数没有更改大小或项目。 How would i make it to change the size in the vector that is created in main? 我将如何更改main中创建的向量的大小?

In the function initVector , you allocated memory for vector , this overwrites its previous value(the argument that's passed in), so it doesn't modify vector in `main. 在函数initVector ,您为vector分配了内存,这将覆盖其先前的值(传入的参数),因此它不会在`main中修改vector This also means there's memory leak here. 这也意味着这里存在内存泄漏。

To fix it, just remove the memory allocation lines in initVector . 要解决此问题,只需删除initVector中的内存分配行。

void initVector(VectorP vector)
{
     vector->size = 0;       //the same as (*vector).size
     vector->items[0] = 1;   
}

I think your problem is you are re-allocating the structure after you create it with struct Vector vector; 我认为您的问题是使用struct Vector vector;创建结构后,您将对其进行重新分配struct Vector vector; c knows enough to create the simple things in structures like basic ints and chars along with int arrays which have a predefined length like int items[DEFAULT_CAPACITY]; c有足够的知识来在结构中创建简单的东西,例如基本的int和chars以及具有预定义长度的int数组(例如int items[DEFAULT_CAPACITY]; .

So what is happening is you are creating a new structure in your init function after you instantiated it in main. 因此,正在发生的是在main中实例化init函数后正在创建一个新结构。 Like Yu Hao suggests just remove the memory allocation in your init function and it should work. 就像Yu Hao所建议的那样,只需删除init函数中的内存分配,它就可以工作。

When you define a vector in your main like this: 当您在主对象中定义矢量时,如下所示:

struct Vector vector;

That mean you are already allocating space in the program stack for this struct. 这意味着您已经在程序堆栈中为此结构分配了空间。 So allocating memory again within the function initvector is not needed at all. 因此,根本不需要在函数initvector再次分配内存。 Thats what is causing the problem. 那就是导致问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM