Arrays 在C结构响应中动态分配数组不正确?

Arrays 在C结构响应中动态分配数组不正确?,arrays,c,memory,dynamic,malloc,Arrays,C,Memory,Dynamic,Malloc,我试图在C结构中创建两个动态分配的数组 typedef struct { int count1; int count2; int *array1; int *array2; int check; } __attribute__ ((packed)) test_T; int main() { int data[8] = { 2, 3, 20, 21, 30, 31, 32, 100 }; test_T *s_t = malloc(size

我试图在C结构中创建两个动态分配的数组

typedef struct {
    int count1;
    int count2;
    int *array1;
    int *array2;
    int check;
} __attribute__ ((packed)) test_T;

int main() {
    int data[8] = { 2, 3, 20, 21, 30, 31, 32, 100 };

    test_T *s_t = malloc(sizeof(s_t));
    s_t->array1 = malloc(sizeof(int) * s_t->count1);
    s_t->array2 = malloc(sizeof(int) * s_t->count2);

    s_t = (test_T *)data;
 
    printf("%d\n", s_t->check);

    return 0;
}
array1
array2
大小都未知,分别取决于
count1
count2

我的结构数据在
main
函数中定义,并将数据放入我的结构中。 问题是结构中的
check
成员总是
32
,但我希望该值是
100

非常感谢您的帮助。

您的代码中有许多问题:

  • \uuuuuuuuuuu属性((打包))
    是一种特定于系统的黑客行为。除非你知道你在做什么,并且认为这是绝对必要的,否则你不应该使用这种不可携带的东西

  • 第一个
    malloc()
    的大小不正确:
    test\u T*s\u T=malloc(sizeof(s\u T))应该是

      test_T *s_t = malloc(sizeof(*s_t));
    
  • s\u t->count1
    s\u t->count2
    必须在使用前进行初始化。
    malloc()
    返回的对象未初始化,因此所有结构成员都未初始化。目前尚不清楚这些成员的初始值应该是多少,8似乎是合理的

  • s\u t->check
    未初始化。如果这是预期值,则应将其初始化为
    100

  • s_t=(test_t*)数据
    是假的:如果需要的话,应该将
    数据中的值复制到数组中

以下是修改后的版本:

#包括
#包括
类型定义结构测试{
int count1;
int count2;
int*阵列1;
int*array2;
整数检查;
}测试;
int main(){
#定义数据计数8
int data[data_COUNT]={2,3,20,21,30,31,32,100};
test_T*s_T=malloc(sizeof(*s_T));
统计->计数1=数据计数;
统计->计数2=数据计数;
s_t->array1=malloc(sizeof(int)*s_t->count1);
s_t->array2=malloc(sizeof(int)*s_t->count2);
s_t->检查=100;
对于(int i=0;iarray2[i]=s_t->array1[i]=data[i];
}
printf(“%d\n”,s\u t->check);
返回0;
} 

您的代码中有许多问题:

  • \uuuuuuuuuuu属性((打包))
    是一种特定于系统的黑客行为。除非你知道你在做什么,并且认为这是绝对必要的,否则你不应该使用这种不可携带的东西

  • 第一个
    malloc()
    的大小不正确:
    test\u T*s\u T=malloc(sizeof(s\u T))应该是

      test_T *s_t = malloc(sizeof(*s_t));
    
  • s\u t->count1
    s\u t->count2
    必须在使用前进行初始化。
    malloc()
    返回的对象未初始化,因此所有结构成员都未初始化。目前尚不清楚这些成员的初始值应该是多少,8似乎是合理的

  • s\u t->check
    未初始化。如果这是预期值,则应将其初始化为
    100

  • s_t=(test_t*)数据
    是假的:如果需要的话,应该将
    数据中的值复制到数组中

以下是修改后的版本:

#包括
#包括
类型定义结构测试{
int count1;
int count2;
int*阵列1;
int*array2;
整数检查;
}测试;
int main(){
#定义数据计数8
int data[data_COUNT]={2,3,20,21,30,31,32,100};
test_T*s_T=malloc(sizeof(*s_T));
统计->计数1=数据计数;
统计->计数2=数据计数;
s_t->array1=malloc(sizeof(int)*s_t->count1);
s_t->array2=malloc(sizeof(int)*s_t->count2);
s_t->检查=100;
对于(int i=0;iarray2[i]=s_t->array1[i]=data[i];
}
printf(“%d\n”,s\u t->check);
返回0;
} 

您的代码中有多个错误,但在这个答案中,只有一个对其中一个错误的响应:指针与数组

我想你误解了指针和结构的工作原理。
array1
是一个指针。在AMD64上,指针始终有8个字节。这并不取决于他们指向哪里。在AMD64上,当编译器没有添加一些填充时,您的
测试将使用28个字节。
当您分配内存并将指针存储在
array1
中时,意味着
array1
现在指向您用
malloc()保留的内存区域。
array1
array2
是数组,它们是指针。也许你为他们选择了一个不那么容易引起误解的名字

阵列1和阵列2的大小都未知

这是错误的。它们是指针,它们的大小是已知的。不言而喻的是所指向的内存区域的大小。此内存区域可以用作数组,
array1
不是数组

如果要在结构中使用变量数组,则必须将数组作为最后一个元素放置,而不包含大小信息,并且在为结构保留内存时,必须在末尾为数组和结构添加空间。一个结构中只能放置一个变量数组。 所有其他的都需要添加指针

像这样:

#include <stdlib.h>

struct Test_T
{
    int someOtherData;
    size_t count;
    int variableArray[];
};

int main(void)
{
    size_t count=5;
    struct Test_T *s_t=malloc(sizeof(*s_t)+sizeof(int)*count);
    //skipped malloc error handling, but you should do that in real programs 
    s_t->count=count;
    // .. some more code should be placed here ...
    free(s_t); //do not forget to free your allocated memory
}
#包括
结构测试
{
int其他数据;
大小/数量;
整数变量数组[];
};
内部主(空)
{
尺寸/计数=5;
结构测试*s\u T=malloc(sizeof(*s\u T)+sizeof(int)*计数);
//跳过了malloc错误处理,但您应该在实际程序中这样做
统计->计数=计数;
//…应该在这里放置更多的代码。。。
free(s_t);//不要忘记释放分配的内存
}

您的代码中有多个错误,但在这个答案中,只有一个对其中一个错误的响应:指针与数组

我想你误解了指针和结构的工作原理。
array1
是一个指针。在AMD64上,指针始终有8个字节。这并不取决于他们指向哪里。在AMD64上,当编译器没有添加一些填充时,您的
测试将使用28个字节。
当您分配内存并将指针存储在
array1中时<