Tag: malloc

C中的malloc()没有按预期工作

我是C的新手。对不起,如果已经回答了,我找不到答案,所以我们走了.. 我试图了解malloc()在C中是如何工作的。我有这样的代码: #define MAXLINE 100 void readInput(char **s) { char temp[MAXLINE]; printf(“Please enter a string: “); scanf(“%s”, temp); *s = (char *)malloc((strlen(temp)+1)*sizeof(char)); // works as expected //*s = (char *)malloc(2*sizeof(char)); // also works even when entering 10 chars, why? strcpy ((char *)*s, temp); } int main() { char *str; readInput(&str); printf(“Your string is %s\n”, str); […]

分配指向malloc保留的块的指针

根据这个答案: https : //stackoverflow.com/a/19765782/1606345 #include typedef struct { int *arr1; int *arr2; } myStruct; myStruct *allocMyStruct(int num) { myStruct *p; if ((p = malloc(sizeof *p + 10 * sizeof *p->arr1 + 10 * num * sizeof *p->arr2)) != NULL) { p->arr1 = (int *)(p + 1); p->arr2 = p->arr1 + 10; } return p; } […]

多个realloc比巨大的malloc更昂贵吗?

我使用动态数组来表示最小堆。 有一个循环可以删除最小值,并将随机元素添加到最小堆中,直到出现某种情况。 虽然我不知道堆的长度在运行时会如何变化(有很多随机性),但我知道上限,即1000万。 我有两个选择: 1)使用malloc声明一个小数组,然后当堆中的元素数量超过长度时调用realloc。 2)使用malloc声明一个1000万条目数组。 这避免了调用realloc。 题 选项2比选项1更有效吗? 我用我的代码测试了这个,并且使用2似乎有显着的(20%)运行时间减少。这是因为代码中的随机性而估计的。 使用malloc预先声明一个大型的10-50万个入口arrays有什么缺点吗?

使用最少数量的malloc调用为2D数组分配内存

我使用下面的代码片段,使用最少数量的malloc()调用为2D数组分配内存。 我想使用下标p [i] [j]访问数组。 #define ROW 3 #define COL 2 int main() { void **ptr = malloc( ROW*COL* sizeof(int) ); int (*p)[COL] = ptr; int i, j; for( i = 0; i < ROW; ++i ) for( j = 0; j < COL; ++j ) scanf("%d", &ptr[i][j]); for( i = 0; i < ROW; ++i […]

在C中分配2-Darrays

我想在运行时在C中分配一个二维数组。 现在这可以通过这样的传统方式实现: int *matrix[rows] for (row = 0; row < rows; ++row) { matrix[row] = (int *)malloc(ncol*sizeof(int)); } 但我找到了另一种方法,它做了同样的事情: int (*p)[rows]; p=(int (*)[rows])malloc(rows*cols*sizeof(int)); 谁能解释第二次宣言是如何运作的? 具体来说, (int (*)[rows])malloc是什么意思? 据我所知, malloc的使用类似于(int *)malloc(ncol*sizeof(int))或(char *)malloc(ncol*sizeof(char)) 。

将free()参数转换为void * neccessary?

是否有必要将传递给free()的值转换为此代码段中的void指针? free((void *) np->defn); np是链表中的struct , defn是char * 。

基本的Malloc /免费

如果我对我的程序有这样的嗤之以鼻: struct Node *node; while(…){ node = malloc(100); //do stuff with node } 这意味着每次循环while循环时,我都会重新分配节点指针所指向的100个字节吗? 如果这是真的,那么如果我只有指针指向发生的最后一个malloc,如何释放我用所有循环所做的所有内存? 谢谢!

将指针返回到数组的函数

我设法在C使用可变长度数组,我现在有以下内容: #include #include int (*foo(size_t row, size_t col))[3]; int main(void){ size_t row, col; printf(“Give the ROW: “); if ( scanf(“%zu”,&row) != 1){ printf(“Error, scanf ROW\n”); exit(1); } printf(“Give the COL: “); if ( scanf(“%zu”,&col) != 1){ printf(“Error, scanf COL\n”); exit(2); } int (*arr)[col] = foo(row, col); for ( size_t i = 0; i < row; […]

通过将指针传递给c中的函数来创建2D数组

所以我读了几十个将2D数组指针传递给函数的例子,以便在函数中获取/更改该数组的值。 但是有可能在函数内部创建(分配内存)。 像这样的东西: #include void createArr(int** arrPtr, int x, int y); int main() { int x, y; //Dimension int i, j; //Loop indexes int** arr; //2D array pointer arr = NULL; x=3; y=4; createArr(arr, x, y); for (i = 0; i < x; ++i) { for (j = 0; j < y; ++j) { printf("%d\n", […]

为什么我在初始化结构时会遇到段错误?

搜索了一个小时左右。 我想我最好在这里发布这个问题。 我简化了代码。 segfault位于函数initMyStruct 。 #include “stdlib.h” typedef struct { int * arr1; int * arr2; } myStruct; void allocMyStruct (myStruct * a, int num) { a = malloc(sizeof(myStruct)); a->arr1 = malloc(10*sizeof(int)); a->arr2 = malloc(10*num*sizeof(int)); } void initMyStruct (myStruct * a, int num) { int i; for (i = 0; i arr1[i] = 0; for […]