我正在尝试实现malloc
和free
C,我不知道如何重用内存.我目前struct
看起来像这样:
typedef struct _mem_dictionary {
void *addr;
size_t size;
int freed;
} mem_dictionary;
Run Code Online (Sandbox Code Playgroud)
我malloc
看起来像这样:
void *malloc(size_t size) {
void *return_ptr = sbrk(size);
if (dictionary == NULL)
dictionary = sbrk(1024 * sizeof(mem_dictionary));
dictionary[dictionary_ct].addr = return_ptr;
dictionary[dictionary_ct].size = size;
dictionary[dictionary_ct].freed = 1;
dictionary_ct++;
return return_ptr;
}
Run Code Online (Sandbox Code Playgroud)
当我释放内存时,我只会将地址标记为0
(表示它是免费的).在我看来malloc
,我会使用for循环来查找数组中的任何值0
,然后将内存分配给该地址.我有点困惑如何实现这一点.
让我们考虑一下这段很短的代码片段:
#include <stdlib.h>
int main()
{
char* a = malloc(20000);
char* b = realloc(a, 5);
free(b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在阅读了realloc的手册页后,我不完全确定第二行会导致释放19995个额外字节.引用手册页:The realloc() function changes the size of the memory block pointed to by ptr to size bytes.
但是根据该定义,我可以确定其余的将被释放吗?
我的意思是,指向的块b
肯定包含5个空闲字节,所以对于懒惰的顺从分配器是否足以让realloc行不做任何事情?
注意:我使用的分配器似乎释放了19 995个额外字节,如valgrind在注释掉free(b)
行时所示:
==4457== HEAP SUMMARY:
==4457== in use at exit: 5 bytes in 1 blocks
==4457== total heap usage: 2 allocs, 1 frees, 20,005 bytes allocated
Run Code Online (Sandbox Code Playgroud) 我希望我的类有一个静态指针指向动态分配的内存区域.我理解如何初始化它 - 在我的情况下,我将在第一个对象需要它时初始化它.但是,我不知道在代码中何时/何处释放它.我想在程序终止时释放它.
我可能能够在我的对象的析构函数中释放指针,但是我必须维护一个对象计数,以查看当对象是最后使用的对象时是否可以安全释放.
有没有更优雅的方式来做到这一点?
请告诉我.
谢谢,jbu
运行ac程序时出现以下错误:
*** glibc detected *** ./a.out: double free or corruption (!prev): 0x080b8008 ***
Run Code Online (Sandbox Code Playgroud)
我相信这是因为在程序结束时调用了free(),但我无法弄清楚malloc内存在此之前被释放的位置.这是代码:
#include <stdio.h>
#include <stdlib.h> //malloc
#include <math.h> //sine
#define TIME 255
#define HARM 32
int main (void) {
double sineRads;
double sine;
int tcount = 0;
int hcount = 0;
/* allocate some heap memory for the large array of waveform data */
double *ptr = malloc(sizeof(double *) * TIME);
if (NULL == ptr) {
printf("ERROR: couldn't allocate waveform memory!\n");
} else {
/*evaluate and add …
Run Code Online (Sandbox Code Playgroud) 为了理解C编程语言中free的用法,我尝试在Ubuntu上运行此代码,但是在运行EXE文件时,我收到了SIGABRT错误.为什么程序没有正常退出?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int ret;
int *ptr;
ptr = (int *)malloc(sizeof(int)*10);
free(ptr);
ptr = &ret;
free(ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在C和C++中,释放空指针将导致无法完成任务.
不过,我看到人们说如果你"两次释放内存"会导致内存损坏.
这是真的?当你释放记忆两次时,引擎盖下发生了什么?
有人可以告诉我之间的区别:
int *p;
p=(int*)malloc(10*sizeof(int));
free(p);
Run Code Online (Sandbox Code Playgroud)
要么
int *p;
p=(int*)malloc(10*sizeof(int));
p=NULL;
Run Code Online (Sandbox Code Playgroud) 我想知道为什么下面的代码不起作用
int main(int argc, char **argv)
{
char *test = (char*) malloc(12*sizeof(char));
test = "testingonly";
free(test);
}
Run Code Online (Sandbox Code Playgroud)
在考虑之后,我的假设是首先在内存中为12个字符分配空间,但是下一行中的赋值在堆栈上创建一个char数组,并将其内存地址传递给test.所以free()尝试释放堆栈上不允许的空间.那是对的吗?
那么在堆上保存字符串的正确方法是什么?以下是一种常见的方式吗?
int main(int argc, char **argv)
{
char *test = (char*) malloc(12*sizeof(char));
strcpy(test, "testingonly");
free(test);
}
Run Code Online (Sandbox Code Playgroud)