Tag: 分配

内存如何分配给不同数据类型的变量?

我写了以下代码。 #include int main() { int x = 1 ; int *j = &x ; int y = 2 ; int *t = &y ; printf(“%p\n” , (void *)j); printf(“%p” , (void *)t); } 输出为0028FF14 0028FF10 。 我要说的是,地址之间的差异是“4”。 而在这种情况下 #include int main() { char x = ‘t’ ; char *j = &x ; char y […]

在函数内动态分配2D数组(使用指针返回已分配对象的地址)

我想知道如何使用函数参数将指针传递给动态分配的数组。 该函数应该分配数组10×10(为简单起见,跳过检查)。 这可能吗? 我究竟做错了什么? 提前致谢。 int array_allocate2DArray ( int **array, unsigned int size_x, unsigned int size_y) { array = malloc (size_x * sizeof(int *)); for (int i = 0; i < size_x; i++) array[i] = malloc(size_y * sizeof(int)); return 0; } int main() { int **array; array_allocate2DArray (*&array, 10, 10); }

malloc(0)实际上有效吗?

可能重复: malloc(0)有什么意义? 为什么malloc(0)实际上返回一个有效的写指针? char *str = NULL; str = (char*)malloc(0); // allocate 0 bytes ? printf(“Pointer of str: %p\n”, str); strcpy(str, “A very long string ……………….”); printf(“Value of str: %s”, str); free(str); // Causes crash if str is too long 输出: Pointer of str: 0xa9d010 Aborted Value of str: A very long string ………………. 当str更短时,它就可以正常工作。 […]

malloc(sizeof(int))vs malloc(sizeof(int *))vs(int *)malloc(sizeof(int))

我承认所有这三个都有不同的含义。 但是,我不明白每种情况适用于哪些特定情况。 任何人都可以分享这些例子吗? 谢谢。 malloc(sizeof(int)) malloc(sizeof(int *)) (int *)malloc(sizeof(int))

在C中的每次错误检查后如何避免长链的免费(或删除)?

假设我非常防御地编写代码,并且总是检查我调用的所有函数的返回类型。 所以我喜欢: char* function() { char* mem = get_memory(100); // first allocation if (!mem) return NULL; struct binder* b = get_binder(‘regular binder’); // second allocation if (!b) { free(mem); return NULL; } struct file* f = mk_file(); // third allocation if (!f) { free(mem); free_binder(b); return NULL; } // … } 注意free()事物失控的速度有多快。 如果某些function失败,我必须先释放每一个分配。 代码很快变得丑陋,我所做的就是将所有内容复制粘贴。 我成为了一个复制/粘贴程序员,更糟糕的是,如果有人在其间添加一个声明,他必须修改下面的所有代码来调用free()来添加它。 […]

为什么编译器在堆栈中分配的数量超过了需要?

我有一个简单的C程序。 比方说,我有一个长度为20的int和一个char数组。我总共需要24个字节。 int main() { char buffer[20]; int x = 0; buffer[0] = ‘a’; buffer[19] = ‘a’; } 堆栈需要与16字节边界对齐,因此我假设编译器将保留32个字节。 但是当我使用gcc x86-64编译这样的程序并读取输出程序集时,编译器会保留64个字节。 ..\gcc -S -o main.s main.c 给我: .file “main.c” .def __main; .scl 2; .type 32; .endef .text .globl main .def main; .scl 2; .type 32; .endef .seh_proc main main: pushq %rbp # RBP is pushed, […]

我应该检查malloc()是否成功?

如果成功,应该在每个malloc()之后检查吗? malloc()是否有可能失败? 那么会发生什么? 在学校我们被告知我们应该检查,即: arr = (int) malloc(sizeof(int)*x*y); if(arr==NULL){ printf(“Error. Allocation was unsuccessful. \n”); return 1; } 有什么做法? 我可以这样做吗: if(!(arr = (int) malloc(sizeof(int)*x*y))

动态分配C中的数组数组

我并不真正理解C中的一些基本内容,比如动态分配数组数组。 我知道你可以这样做: int **m; 为了声明一个二维数组(随后将使用一些* alloc函数分配)。 也可以通过*(*(m + line) + column) “轻松”访问它。 但是我应该如何为该数组中的元素赋值? 使用gcc以下语句m[line][column] = 12; 因故障而失败。 任何文章/文档将不胜感激。 🙂

是否可以在函数内部分配数组并使用引用返回它?

我尝试过使用tripple指针,但它仍然失败。 码: #include #include int set(int *** list) { int count, i; printf(“Enter number:\n”); scanf(“%d”, &count); (*list) = (int **) malloc ( sizeof (int) * count); for ( i = 0; i<count;i++ ) { (**list)[count] = 123; } return count; } int main ( int argc, char ** argv ) { int ** list; int […]