Tag: malloc

我使用malloc()将多维变量数组传递给C中的函数时遇到问题

这段代码应该在main中创建一个数组然后打印它,但每次运行它时我只得到一个全0的数组 #include #include void print(float **A, int w, int h){ int i, j; A = (float*) malloc(w*h*sizeof(float)); for (i=0;i<h;i++){ A[i] = (float*) malloc(w*sizeof(float)); } for (i=0; i<h; i++){ for(j=0; j<w; j++){ printf("%f ", A[i][j]); } printf("\n"); } } int main(void) { int i; int x_dimension=3; int y_dimension=2; float arr [3][2]={}; arr[0][0]=16.2; print(arr,x_dimension,y_dimension); return 0; }

malloc灾难性地失败了

我正在尝试在C中实现Queue。来自Java和其他托管语言,我真的在努力进行内存管理。 这是enqueue()函数: int enqueue(Queue q, int value) { Node newNode = malloc(sizeof(Node)); /*newNode->value = value; if (q->size == 0) q->head = newNode; else q->head->next = &newNode; q->size++;*/ } 我收到此错误: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) – 1) * 2])) – __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) […]

裸机环境下的stdlib式库? (内存管理和希望pthread支持)

是否存在用于裸机编程的类似stdlib的库? 我正在尝试为裸机环境构建一个程序(应该是在linux上构建)。 该程序依赖于stdlib和posix lib(malloc,calloc,realloc,free和pthread用法)。 无论如何我会修改单线程。 我正在阅读https://www.ibm.com/developerworks/aix/tutorials/au-memorymanager/ ,也许我将实现自己的内存管理。 但在我的情况下,该程序具有各种大小的malloc / realloc / free。 如果有任何程序(开源)支持内存管理(并希望也是pthread)。请给我一个建议。 语言是C.

使用malloc为字符串分配内存(c ++源代码)

我正在尝试创建一个结构数组,我已经完成了,我必须从用户那里收到一个输入。 第一条数据是描述(字符串)。 我也必须为它分配内存。 我不是因为它要进行检查所以要检查的字符串有多大,但我认为我没有正确设置它。 任何人都可以给我一个提示或一个我可以看到的页面来弄明白吗? 非常感激。 以下是代码的重要内容: struct myExpenses { char *description; float cost; }; int main (void) { struct myExpenses *pData = NULL; struct myExpenses expenses[60]; int exit=0; int i = 0; char buffer[81] = “”; printf(“Please enter all your descriptions:\n”); for (i=0;i < 60; i++) { fgets(buffer,stdin); expenses[i].description=(char *)malloc sizeof(buffer); }

修改malloc的二维数组到C中的3d数组的function

我是C的新手,这是我写的第一个程序。 我的教授给了我们一个为2d数组分配内存的函数,叫做malloc2d。 我应该修改它来为一个3d数组分配内存,但对于CI这么新,我不知道如何去做。 我已经尝试过查看3D数组的其他malloc函数,但它们看起来与我给出的类似。 同样,我们有一个free2d函数,也需要针对3d数组进行修改。 以下是要修改的function: void** malloc2D(size_t rows, size_t cols, size_t sizeOfType){ void* block = malloc(sizeOfType * rows * cols); void** matrix = malloc(sizeof(void*) * rows); for (int row = 0; row < rows; ++row) { matrix[row] = block + cols * row * sizeOfType; }//for return matrix; }//malloc2D void free2D(void*** matrix){ free((*matrix)[0]); free((*matrix)); […]

为连续的2Darrays分配内存

我正在尝试创建一个generics函数,当为维度数组调用分配的连续内存时。 目标是实现类似下面的目标 所以要实现它 – 我正在使用的方程式 Type **pArray; int total_elements = ((rows * cols) + rows); pArray = (Type **) malloc(total_elements * sizeof(Type)); 我也对访问元素部分感到困惑。 我发现很难想象下面的代码如何填充上面数组的元素 for (row = 0; row < dim0; ++row) { for (col = 0; col < dim1; ++col) { /* For this to work Type must be a 1D array type. */ […]

为什么读取结构指针字段无效?

在Valgrind中运行该程序,它表示在结构的转换指针处存在“无效读取大小为8”。 它与calloc有关吗? 如果按原样读取它是(零)。 有一个结构(称为trie),它使用如下: #include #include #include #include const int MAX_SIZE = 20; struct _trie { int maxNode; int nextNode; int** transition; char* fin; }; typedef struct _trie * Trie; Trie createTrie (int maxNode){ Trie trie; trie = (Trie) malloc(sizeof(Trie)); printf(“size of trie: %lu, size of the struct: %lu, size of _trie: %lu\n”,sizeof(trie),sizeof(Trie), sizeof(struct _trie)); […]

函数中的指针不返回正确的地址

我尝试创建一个在C中分配数组的函数,但我没有得到正确的地址。 这是代码: #include #include void allocate_array(int* arr, int size); void print_array(int* arr, int size); void main(){ int size; int* arr; printf(“Please enter array size: “); scanf(“%d”, &size); allocate_array(arr, size); print_array(arr, size); } void allocate_array(int* arr, int size){ int i; arr = (int*)malloc(size*sizeof(int)); for(i=0; i<size; i++){ printf("arr[%d] = ",i); scanf("%d", arr+i); } } void print_array(int* arr, […]

malloc和free动态变化的结构

我在动态变化的结构中移动指针时遇到了麻烦。 我已经创建了我的代码,你可以在malloc中获得更多的内存,这看起来很有效。 我遇到的问题是如何添加到结构,如何释放内存以及如何从结构移动到结构并打印所有项目。 我正在尝试测试添加和打印(删除function,似乎没有工作,segfaults) 当我添加到结构然后打印结构时,我从我添加的值中获得了一个段错误。 我不知道我是否正确地从第一个结构转移到下一个结构。 #include #include #include “pointer.h” /******************************************** Creates more memory for size (strut * rec+1) *********************************************/ employee *create(int record){ employee *new_employee = malloc(sizeof(employee) * (record+1)); return new_employee; } /******************************************** Copies the data from one structure to a new structure with size “structure” multipled by rec+1 ***********************************************/ employee *copy(employee *data, int record){ […]

我可以用新的内存分配一块内存吗?

所以考虑到这个c结构: typedef struct { int* arr1; int* arr2; } myStruct; 这个答案描述了使用单个malloc同时分配myStruct和它的数组: myStruct* p = malloc(sizeof(*p) + 10 * sizeof(*p->arr1) + 10 * num * sizeof(*p->arr2); if(p != NULL) { p->arr1 = (int*)(p + 1); p->arr2 = p->arr1 + 10; } 我想知道有没有类似的方法来做到这一点? 显然,我希望能够分配到我在运行时收到的大小,就像使用C示例一样。