Tag: malloc

返回函数前释放已分配的内存

我试图在函数中使用malloc返回一个数组: char* queueBulkDequeue(queueADT queue, unsigned int size) { unsigned int i; char* pElements=(char*)malloc(size * sizeof(char)); for (i=0; i<size; i++) { *(pElements+i) = queueDequeue(queue); } return pElements; } 问题是我需要释放它,因为我的MCU堆大小有限。 但是我想要退回它,所以我不能在function中释放它,对吧? 我可以在函数外部释放已分配的内存(我称之为函数)。 这有什么最好的做法吗? 先感谢您!

在使用malloc分配新内存后,是否必须调用memset

#include “stdlib.h” #include “stdio.h” #include “string.h” int main(int argc, char* argv[]) { int *test = malloc(15 * sizeof(int)); for(int i = 0;i < 15 ;i ++ ) printf("test is %i\n",test[i]); memset(test,0,sizeof(int) * 15); for(int i = 0 ; i < 15; i ++ ) printf("test after memset is %i\n",test[i]); return 0; } 我得到的输出非常奇怪: test is […]

使用指针/ malloc创建2D数组,然后将其打印出来

我正在尝试编写2个函数,一个用于读取矩阵(二维数组),另一个用于打印出来。 到目前为止我有: /* Read a matrix: allocate space, read elements, return pointer. The number of rows and columns are given by the two arguments. */ double **read_matrix(int rows, int cols){ double **mat = (double **) malloc(sizeof(double *)*rows); int i=0; for(i=0; i<rows; i++){ /* Allocate array, store pointer */ mat[i] = (double *) malloc(sizeof(double)*cols); //what to […]

操作内存时是否需要乘以sizeof(char)?

当使用malloc并进行类似的内存操作时,我可以依赖sizeof(char)始终为1吗? 例如,我需要为char类型的N个元素分配内存。 是否需要乘以sizeof( char ) : char* buffer = malloc( N * sizeof( char ) ); 或者我可以依赖sizeof(char)始终为1并且只是跳过乘法 char* buffer = malloc( N ); 我完全理解在编译期间评估sizeof然后编译器甚至可以编译出乘法,因此性能损失将是最小的并且很可能为零。 我主要询问代码清晰度和可移植性。 char类型是否需要这种乘法?

具有不同大小结构的struct数组的malloc()

如果每个struct包含一个大小不一的字符串数组,那么malloc如何正确地构造一个结构数组? 因此,每个结构可能具有不同的大小,并且无法实现 realloc(numberOfStructs * sizeof(structName)) 后 malloc(initialSize * sizeof(structName) 如何为此分配内存并跟踪发生的情况?

Malloc和Void指针

我正在研究这个malloc函数,我可以使用一些帮助: static void *malloc(int size) { void *p; if (size = free_mem_end_ptr) error(“Out of memory”); malloc_count++; return p; } 我知道malloc func为任何类型分配内存空间,如果有足够的内存,但我不理解的行是: p = (void *)malloc_ptr; malloc_ptr += size; 它如何指向任何类似的数据类型? 我只是无法理解void指针或其位置。 注意:malloc_ptr是unsigned long

malloc()如何导致SIGSEGV?

我的程序中有一个奇怪的错误,在我看来malloc()正在引起一个SIGSEGV,据我所知,这没有任何意义。 我正在使用一个名为simclist的库来创建动态列表。 这是一个稍后引用的结构: typedef struct { int msgid; int status; void* udata; list_t queue; } msg_t; 这是代码: msg_t* msg = (msg_t*) malloc( sizeof( msg_t ) ); msg->msgid = msgid; msg->status = MSG_STAT_NEW; msg->udata = udata; list_init( &msg->queue ); list_init是程序失败的地方,这里是list_init的代码: /* list initialization */ int list_init(list_t *restrict l) { if (l == NULL) return -1; srandom((unsigned […]

包装malloc – C.

我是C的初学者。在阅读git的源代码时,我发现了围绕malloc这个包装函数。 void *xmalloc(size_t size) { void *ret = malloc(size); if (!ret && !size) ret = malloc(1); if (!ret) { release_pack_memory(size, -1); ret = malloc(size); if (!ret && !size) ret = malloc(1); if (!ret) die(“Out of memory, malloc failed”); } #ifdef XMALLOC_POISON memset(ret, 0xA5, size); #endif return ret; } 问题 我不明白为什么他们使用malloc(1) ? release_pack_memory做了什么,我在整个源代码中找不到这个函数实现。 #ifdef XMALLOC_POISON memset(ret, […]

使用malloc分配char数组

嗨,最近我在网上看到了很多代码(也是关于SO;),如: char *p = malloc( sizeof(char) * ( len + 1 ) ); 为什么sizeof(char)? 这不是必要的,不是吗? 或者只是风格问题? 它有什么优势?

C标准malloc’ing字符的潜在问题

当我在这里回答对我的另一个答案的评论时,我发现我认为可能是C标准中的一个漏洞(c1x,我没有检查过早期的那些,是的,我知道我不可能单独在所有星球中居民在标准中发现了一个错误)。 信息如下: 第6.5.3.4节(“sizeof运算符”)第2节规定”The sizeof operator yields the size (in bytes) of its operand” 。 该部分的第3段指出: “When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1” 。 第7.20.3.3节描述了void *malloc(size_t sz)但它说的是”The malloc function allocates space for an object whose size is specified by size […]