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;
}
ç”案 0 :(得分:0)
ä¸æ˜¯å°†æŒ‡é’ˆæ•°ç»„分é…给其他数组,而是实现2D数组,您åªéœ€åˆ›å»ºä¸€ä¸ªåˆ†é…,然åŽåƒarray[x + y*WIDTH]
ä¸€æ ·è®¿é—®å®ƒã€‚è¿™æ ·æ›´æœ‰æ•ˆï¼Œæ›´ä¸å®¹æ˜“出错ç‰ã€‚
您的代ç 会å˜å¾—更简å•ï¼š
double * create_array( unsigned int length, unsigned int row)
{
return malloc( length * row * sizeof(double) );
}
å’Œat()函数å¯èƒ½å¦‚下所示:
// return element at row column
double at(double * array, unsigned int row, unsigned int col, unsigned int width)
{
return array[ col + row * width ];
}
ç”案 1 :(得分:0)
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
$
ç”案 2 :(得分:0)
在ubuntu上è¿è¡Œddda.c输出:
tmpdata
它适用于那些CFLAGSé›†ã€‚æ‰€ä»¥æ˜¯çš„ï¼Œä½ çš„è°ƒæ•´ä»£ç 确实有效。我现在ä¸çŸ¥é“å¦‚ä½•ï¼Œå”¯ä¸€çš„åŒºåˆ«æ˜¯æ ‡å¿—è®¾ç½®ã€‚