gpt4 book ai didi

c - GCC 中的动态分配指针数组不匹配

转载 作者:太空宇宙 更新时间:2023-11-04 13:03:01 25 4
gpt4 key购买 nike

Minimally Verifiable Source Code - I think.我正在尝试在函数中使用指针数组的动态分配来创建双双指针数组。当我在结构引用之外使用代码时,它会起作用。当我尝试通过指针引用它时,它没有。我究竟做错了什么?

此代码适用于 GCC 4.9.2

gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.9.2-10ubuntu13' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
int main(int argc, char **argv) 
{
double ** array;
double row[6] = { 1.2, 2.3, 3.0, 4, 5, 6 };
int i;
int j;
array = (double **) malloc( 50* sizeof(double * ) );
for ( i = 0; i < 50; i++)
{
array[i] = (double *) malloc( 6 * sizeof(double ) );
}
for (j =0; j < 50; j++)
for ( i = 0; i < 6; i++)
{
array[j][i] = row[i];
}
for (j =0; j < 50; j++)
{
for (i = 0; i < 6; i++ )
{
printf("%f ", ( array[0][i]) );
}
printf("\n ");
}
exit(0); // Use exit() to exit a program, do not use 'return' from main()
}

但是这段代码没有:

double ** create_array( unsigned int length,  unsigned int row)
{
double ** array;
int i;

array = (double **) malloc( length * sizeof(double * ) );
for ( i = 0; i < length; i++)
{
array[i] = (double *) malloc( row * sizeof(double ) );
}
return array;
}

我把双指针数组放在一个结构中

typedef struct
{
// the data area
double ** array;
// the number of rows in the entire entry
unsigned int length;
// the size of the double entity of array size
unsigned int rowsize;
// the current starting point
int start;
// the current ending point
int end;
// the amount of the last write action
// assumes the number of write in order
unsigned int last_write[100];
// the number of writes
unsigned int write_count;
} DRingBuffer;

然后通过如下函数调用访问:

int DRingBuffer_printrow(DRingBuffer *buffer,  unsigned int row  )

当我调用该函数然后尝试访问它的成员时出现错误。

        printf("%f ", (buffer->array[3][2]));

这是很多代码,但有人要求它。

 DRingBuffer *DRingBuffer_create(unsigned int length,  unsigned int row  )
{
int i =0;
DRingBuffer *buffer = malloc( sizeof(DRingBuffer) );
buffer->length = length;
buffer->rowsize = row;
buffer->start = 0;
buffer->end = 0;
buffer->array = create_array( length, row);
if ( buffer->array <= 0 )
{
printf("ERROR: allocating arrays");
exit( -1);
}
return buffer;
}

最佳答案

您在 create_array() 中的代码应该没问题。它对我有用。您没有在使用它的地方显示代码,因此我们无法确定您是如何滥用它的,但看起来您确实在滥用它。

这是对您的代码的改编:

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

static
double **create_array(unsigned int length, unsigned int row)
{
double **array;

array = (double **) malloc(length * sizeof(double *));
for (unsigned int i = 0; i < length; i++)
{
array[i] = (double *) malloc(row * sizeof(double));
}
return array;
}

int main(void)
{
double **array;
double row[6] = { 1.2, 2.3, 3.0, 4, 5, 6 };

array = (double **) malloc(50 * sizeof(double *));
for (int i = 0; i < 50; i++)
{
array[i] = (double *) malloc(6 * sizeof(double));
}

for (int j = 0; j < 50; j++)
{
for (int i = 0; i < 6; i++)
{
array[j][i] = row[i] + (i + 1) * (j + 1);
}
}
for (int j = 0; j < 50; j++)
{
for (int i = 0; i < 6; i++)
{
printf("%6.1f ", (array[j][i]));
}
putchar('\n');
}

for (int j = 0; j < 50; j++)
free(array[j]);
free(array);

double **array2 = create_array(50, 6);

for (int j = 0; j < 50; j++)
{
for (int i = 0; i < 6; i++)
{
array2[j][i] = row[i] + (i + 1) * (j + 1);
}
}
for (int j = 0; j < 50; j++)
{
for (int i = 0; i < 6; i++)
{
printf("%6.1f ", (array2[j][i]));
}
putchar('\n');
}

for (int j = 0; j < 50; j++)
free(array2[j]);
free(array2);

return(0);
}

在 Mac OS X 10.11.1 El Capitan 上测试,它产生:

   2.2    4.3    6.0    8.0   10.0   12.0 
3.2 6.3 9.0 12.0 15.0 18.0
4.2 8.3 12.0 16.0 20.0 24.0
5.2 10.3 15.0 20.0 25.0 30.0
6.2 12.3 18.0 24.0 30.0 36.0
7.2 14.3 21.0 28.0 35.0 42.0
8.2 16.3 24.0 32.0 40.0 48.0
9.2 18.3 27.0 36.0 45.0 54.0
10.2 20.3 30.0 40.0 50.0 60.0
11.2 22.3 33.0 44.0 55.0 66.0
12.2 24.3 36.0 48.0 60.0 72.0
13.2 26.3 39.0 52.0 65.0 78.0
14.2 28.3 42.0 56.0 70.0 84.0
15.2 30.3 45.0 60.0 75.0 90.0
16.2 32.3 48.0 64.0 80.0 96.0
17.2 34.3 51.0 68.0 85.0 102.0
18.2 36.3 54.0 72.0 90.0 108.0
19.2 38.3 57.0 76.0 95.0 114.0
20.2 40.3 60.0 80.0 100.0 120.0
21.2 42.3 63.0 84.0 105.0 126.0
22.2 44.3 66.0 88.0 110.0 132.0
23.2 46.3 69.0 92.0 115.0 138.0
24.2 48.3 72.0 96.0 120.0 144.0
25.2 50.3 75.0 100.0 125.0 150.0
26.2 52.3 78.0 104.0 130.0 156.0
27.2 54.3 81.0 108.0 135.0 162.0
28.2 56.3 84.0 112.0 140.0 168.0
29.2 58.3 87.0 116.0 145.0 174.0
30.2 60.3 90.0 120.0 150.0 180.0
31.2 62.3 93.0 124.0 155.0 186.0
32.2 64.3 96.0 128.0 160.0 192.0
33.2 66.3 99.0 132.0 165.0 198.0
34.2 68.3 102.0 136.0 170.0 204.0
35.2 70.3 105.0 140.0 175.0 210.0
36.2 72.3 108.0 144.0 180.0 216.0
37.2 74.3 111.0 148.0 185.0 222.0
38.2 76.3 114.0 152.0 190.0 228.0
39.2 78.3 117.0 156.0 195.0 234.0
40.2 80.3 120.0 160.0 200.0 240.0
41.2 82.3 123.0 164.0 205.0 246.0
42.2 84.3 126.0 168.0 210.0 252.0
43.2 86.3 129.0 172.0 215.0 258.0
44.2 88.3 132.0 176.0 220.0 264.0
45.2 90.3 135.0 180.0 225.0 270.0
46.2 92.3 138.0 184.0 230.0 276.0
47.2 94.3 141.0 188.0 235.0 282.0
48.2 96.3 144.0 192.0 240.0 288.0
49.2 98.3 147.0 196.0 245.0 294.0
50.2 100.3 150.0 200.0 250.0 300.0
51.2 102.3 153.0 204.0 255.0 306.0
2.2 4.3 6.0 8.0 10.0 12.0
3.2 6.3 9.0 12.0 15.0 18.0
4.2 8.3 12.0 16.0 20.0 24.0
5.2 10.3 15.0 20.0 25.0 30.0
6.2 12.3 18.0 24.0 30.0 36.0
7.2 14.3 21.0 28.0 35.0 42.0
8.2 16.3 24.0 32.0 40.0 48.0
9.2 18.3 27.0 36.0 45.0 54.0
10.2 20.3 30.0 40.0 50.0 60.0
11.2 22.3 33.0 44.0 55.0 66.0
12.2 24.3 36.0 48.0 60.0 72.0
13.2 26.3 39.0 52.0 65.0 78.0
14.2 28.3 42.0 56.0 70.0 84.0
15.2 30.3 45.0 60.0 75.0 90.0
16.2 32.3 48.0 64.0 80.0 96.0
17.2 34.3 51.0 68.0 85.0 102.0
18.2 36.3 54.0 72.0 90.0 108.0
19.2 38.3 57.0 76.0 95.0 114.0
20.2 40.3 60.0 80.0 100.0 120.0
21.2 42.3 63.0 84.0 105.0 126.0
22.2 44.3 66.0 88.0 110.0 132.0
23.2 46.3 69.0 92.0 115.0 138.0
24.2 48.3 72.0 96.0 120.0 144.0
25.2 50.3 75.0 100.0 125.0 150.0
26.2 52.3 78.0 104.0 130.0 156.0
27.2 54.3 81.0 108.0 135.0 162.0
28.2 56.3 84.0 112.0 140.0 168.0
29.2 58.3 87.0 116.0 145.0 174.0
30.2 60.3 90.0 120.0 150.0 180.0
31.2 62.3 93.0 124.0 155.0 186.0
32.2 64.3 96.0 128.0 160.0 192.0
33.2 66.3 99.0 132.0 165.0 198.0
34.2 68.3 102.0 136.0 170.0 204.0
35.2 70.3 105.0 140.0 175.0 210.0
36.2 72.3 108.0 144.0 180.0 216.0
37.2 74.3 111.0 148.0 185.0 222.0
38.2 76.3 114.0 152.0 190.0 228.0
39.2 78.3 117.0 156.0 195.0 234.0
40.2 80.3 120.0 160.0 200.0 240.0
41.2 82.3 123.0 164.0 205.0 246.0
42.2 84.3 126.0 168.0 210.0 252.0
43.2 86.3 129.0 172.0 215.0 258.0
44.2 88.3 132.0 176.0 220.0 264.0
45.2 90.3 135.0 180.0 225.0 270.0
46.2 92.3 138.0 184.0 230.0 276.0
47.2 94.3 141.0 188.0 235.0 282.0
48.2 96.3 144.0 192.0 240.0 288.0
49.2 98.3 147.0 196.0 245.0 294.0
50.2 100.3 150.0 200.0 250.0 300.0
51.2 102.3 153.0 204.0 255.0 306.0

valgrind 下运行时,它也不会泄漏任何内存或显示任何内存滥用.或者,至少,在 El Capitan 上,内存泄漏全部来自系统代码,完全不受像你我这样的凡人的控制。

有些人会责备您(可能还有我)强制转换 malloc() 的结果。了解他们的敏感度,谨慎行事。如果你像我一样使用严格的编译警告进行编译,你就不会遇到问题:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
> -Wold-style-definition -Werror ddda.c -o ddda
$

关于c - GCC 中的动态分配指针数组不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33452671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com