- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
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/
我刚接触 C 语言几周,所以对它还很陌生。 我见过这样的事情 * (variable-name) = -* (variable-name) 在讲义中,但它到底会做什么?它会否定所指向的值吗? 最佳答案
我有一个指向内存地址的void 指针。然后,我做 int 指针 = void 指针 float 指针 = void 指针 然后,取消引用它们以获取值。 { int x = 25; vo
我正在与计算机控制的泵进行一些串行端口通信,我用来通信的 createfile 函数需要将 com 端口名称解析为 wchar_t 指针。 我也在使用 QT 创建一个表单并获取 com 端口名称作为
#include "stdio.h" #include "malloc.h" int main() { char*x=(char*)malloc(1024); *(x+2)=3; --
#include #include main() { int an_int; void *void_pointer = &an_int; double *double_ptr = void
对于每个时间步长,我都有一个二维矩阵 a[ix][iz],ix 从 0 到 nx-1 和 iz 从 0 到 nz-1。 为了组装所有时间步长的矩阵,我定义了一个长度为 nx*nz*nt 的 3D 指针
我有一个函数,它接受一个指向 char ** 的指针并用字符串填充它(我猜是一个字符串数组)。 *list_of_strings* 在函数内部分配内存。 char * *list_of_strings
我试图了解当涉及到字符和字符串时,内存分配是如何工作的。 我知道声明的数组的名称就像指向数组第一个元素的指针,但该数组将驻留在内存的堆栈中。 另一方面,当我们想要使用内存堆时,我们使用 malloc,
我有一个 C 语言的 .DLL 文件。该 DLL 中所有函数所需的主要结构具有以下形式。 typedef struct { char *snsAccessID; char *
指针, C语言的精髓 莫队先咕几天, 容我先讲完树剖 (因为后面树上的东西好多都要用树剖求 LCA). 什么是指针 保存变量地址的变量叫做指针. 这是大概的定义, 但是Defad认为
我得到了以下数组: let arr = [ { children: [ { children: [], current: tru
#include int main(void) { int i; int *ptr = (int *) malloc(5 * sizeof(int)); for (i=0;
我正在编写一个程序,它接受一个三位数整数并将其分成两个整数。 224 将变为 220 和 4。 114 将变为 110 和 4。 基本上,您可以使用模数来完成。我写了我认为应该工作的东西,编译器一直说
好吧,我对 C++ 很陌生,我确定这个问题已经在某个地方得到了回答,而且也很简单,但我似乎找不到答案.... 我有一个自定义数组类,我将其用作练习来尝试了解其工作原理,其定义如下: 标题: class
1) this 指针与其他指针有何不同?据我了解,指针指向堆中的内存。如果有指向它们的指针,这是否意味着对象总是在堆中构造? 2)我们可以在 move 构造函数或 move 赋值中窃取this指针吗?
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: C : pointer to struct in the struct definition 在我的初学者类
我有两个指向指针的结构指针 typedef struct Square { ... ... }Square; Square **s1; //Representing 2D array of say,
变量在内存中是如何定位的?我有这个代码 int w=1; int x=1; int y=1; int z=1; int main(int argc, char** argv) { printf
#include #include main() { char *q[]={"black","white","red"}; printf("%s",*q+3); getch()
我在“C”类中有以下函数 class C { template void Func1(int x); template void Func2(int x); }; template void
我是一名优秀的程序员,十分优秀!