我对malloc有一个奇怪的问题.我有这个typedef:
typedef struct buffer {
int D;
int T;
unsigned int current_size;
unsigned int max_size;
pthread_mutex_t mutex;
pthread_cond_t non_pieno;
pthread_cond_t non_vuoto;
msg_t** messages;
struct buffer* (*buffer_init)(unsigned int);
void (*buffer_destroy)(struct buffer*);
} buffer_t;
Run Code Online (Sandbox Code Playgroud)
这是buffer_init和buffer_destroy函数:
buffer_t* buffer_init(unsigned int maxsize)
{
buffer_t* new_buffer = (buffer_t*)malloc(sizeof(buffer_t));
msg_t** new_messages = (msg_t**)calloc(maxsize, sizeof(msg_t**));
pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t np = PTHREAD_COND_INITIALIZER;
pthread_cond_t nv = PTHREAD_COND_INITIALIZER;
new_buffer->T = 0;
new_buffer->D = 0;
new_buffer->current_size = 0;
new_buffer->max_size = maxsize;
new_buffer->messages = new_messages;
new_buffer->mutex = mx;
new_buffer->non_pieno = np; …Run Code Online (Sandbox Code Playgroud) 我有C程序的问题:
char *str = (char *) malloc(20);
strcpy_s(str, 10, "abcdefghij");
//here I change one byte before str and one byte after
*((int*)str-1) = 10;
*((int*)(str+20)) = 10;
//and it stops on the..
free(str);
Run Code Online (Sandbox Code Playgroud)
在调试过程中出错了什么?
覆盖未分配内存的部分是任务的一部分.我知道通常它不正确,但在这种情况下它是任务的一部分.
当我使用Instruments分析我的代码时,它显示了来自此函数的Malloc 16字节泄漏(下面),但我从未在此函数中使用malloc.在这个函数中是否有一个地方我应该释放一些资源?
它可能看起来像很多代码,但我认为实际上只有变量count和count2作为可能的违规者.
+ (int) trimArray: (NSMutableArray*) source above: (short) max andMin: (short) min
{
int counts[6][7];
int counts2[6][7];
for (int i=0;i<=5;i++)
{
for (int ii=0;ii<7;ii++)
{
counts[i][ii] = 0;
counts2[i][ii] = 0;
}
}
int capacity = (int)[source count]/max;
if (capacity <2)
capacity = 2;
NSMutableArray *itemsToRemove = [[NSMutableArray alloc] initWithCapacity:capacity];
int week,dow,count1,count2;
EntryTimeItem *item;
NSEnumerator *e;
e = [source objectEnumerator];
while (item = [e nextObject])
{
week = item.week_number;
dow = item.day_of_the_week;
if (week >=0 && week <6 …Run Code Online (Sandbox Code Playgroud) 我试图编写一个简单的函数来释放动态分配的内存
typedef struct list{
int data;
struct list * link;
} list;
list * head = NULL;
void release(list * head_new){
list * dummy = NULL;
while(head_new != NULL){
dummy = head_new->link;
printf("before freeing %p, %d", head_new->link, head_new->data);
free(head_new);
printf("free returns %p, %d", head_new->link, head_new->data);
head_new = dummy
}
}
Run Code Online (Sandbox Code Playgroud)
使用主函数值被赋予列表,并且即使在释放head_new节点之后,在该特定函数中也会打印一些值
1
12
1
123
1 12 1 123 before freeing 00622A40, 1
free returns 006200C4, 6433408
before freeing 00622A60, 12
free returns 006200C4, 6434048
before freeing 00622A70, 1
free …Run Code Online (Sandbox Code Playgroud) 当我输入数据时,curr->data我首先通过执行来分配内存curr = (NODE*) malloc (sizeof(NODE));.然后我构建列表,最后在最后打印它.在程序的某个地方我必须释放我用过的内存,但我不知道该怎么做.构建列表后,或打印列表后?或者我可以这样做吗?
printf("How many elements do you want to make? ");
scanf("%d", &NumElem);
head = NULL;
for (i = 0; i < NumElem; i++)
{
//memory allocate
curr = (NODE*)malloc(sizeof(NODE));
printf("Number %d: ", i+1);
scanf("%d", &curr->num);
FLUSH;
if (head == NULL)/*beginning of the list*/
{
curr->next = head;
head = curr;
*tail = curr;
}
else /*adding on the list*/
{
curr->next = head;
head = curr;
}
free (curr);
}//for
Run Code Online (Sandbox Code Playgroud)
将数据放入当前节点后,每次都可以释放吗?
我有一个没有内存的功能.
我想在调用此函数后断言该函数释放了给定的内存.
我无法改变这个功能中的任何东西.
对于单一测试我需要它.我想测试我的函数,我想检查一下我的函数在调用之后真的释放了内存
问题出在代码中
void func(char *mem)
{
// Some where in the function there is a free of the memory
// The free could be into if condition so there is a risk to not be executed
}
int main()
{
char *mem = malloc(20);
func(mem);
// how to assert here that the memory is freed?
}
Run Code Online (Sandbox Code Playgroud) 我正在调试我已经工作了一段时间的项目,并且遇到了一些涉及的疯狂错误free.我无法上传代码,因为没有办法确切地说出问题所在(大约2500行代码分成22个文件),但我会解释我所知道的.
首先,gdb正在用于整个调试过程.这个错误似乎从呼叫上升到了free.gdb程序退出后,我收到以下错误消息SIGABRT:
*** Error in `application': free(): invalid next size (normal): 0x08052008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x767e2)[0xb7e467e2]
/lib/i386-linux-gnu/libc.so.6(+0x77530)[0xb7e47530]
application[0x8049aef]
application[0x804a8aa]
application[0x8048bee]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0xb7de9935]
application[0x8048a51]
======= Memory map: ========
08048000-08050000 r-xp 00000000 00:16 1571817 application
08050000-08051000 r--p 00007000 00:16 1571817 application
08051000-08052000 rw-p 00008000 00:16 1571817 application
08052000-08073000 rw-p 00000000 00:00 0 [heap]
b7d9c000-b7db7000 r-xp 00000000 08:01 1309022 /lib/i386-linux-gnu/libgcc_s.so.1
b7db7000-b7db8000 r--p 0001a000 08:01 1309022 /lib/i386-linux-gnu/libgcc_s.so.1
b7db8000-b7db9000 rw-p 0001b000 08:01 1309022 /lib/i386-linux-gnu/libgcc_s.so.1
b7dce000-b7dd0000 …Run Code Online (Sandbox Code Playgroud) row = n + 1;
col = n + 1;
//used n+1 and i=-1 to avoid segmentation faults
board = malloc(row*sizeof(char *));
for(i=-1;i<row;i++)
{
board[i] = malloc(col*sizeof(char));
if(board[i] == NULL)
{
printf("Out of memory");
exit(EXIT_FAILURE);
}
}
for(i=-1; i < n+1; ++i)
{
free(board [i]);
}
free(board);
Run Code Online (Sandbox Code Playgroud)
当我尝试在运行时释放这个数组时,我的编译器变得狂暴,请解释一下,谢谢.
当调用memset时,我对内存中实际发生的事情感到困惑,而当你调用free时会发生什么.
例如,我有一个指向一个char*数组的指针A.
char** A = (char**)calloc(5, sizeof(char*));
int i;
for(i=0;i<5;i++)
{
//filling
A[i] = (char*)calloc(30, sizeof(char));
scanf("%s", &A[i]);
}
Run Code Online (Sandbox Code Playgroud)
现在我想重置它我的char**指针和它指向的所有元素都是完全空的
memset(A, 0, 5);
Run Code Online (Sandbox Code Playgroud)
要么
free(A);
Run Code Online (Sandbox Code Playgroud)
有什么不同?
我对C有点新意,所以请以外行的话说话,谢谢
我只是很想知道,如果在释放新运算符分配的内存之前会发生什么,会发生异常?是否发生了内存泄漏问题?
#include <iostream>
#include<new>
using namespace std;
void func()
{
try
{
int *p = new int[10];
/*
Number of lines code here
.
.
.
.
.
Suppose here I got exception then What heppens????
.
.
.
.
*/
delete []p;
}
catch(const std::exception& e)
{
cout<<"Exception occured"<<endl;
}
}
int main() {
func();
return 0;
}
Run Code Online (Sandbox Code Playgroud) free ×10
c ×9
malloc ×4
c++ ×2
arrays ×1
assert ×1
debugging ×1
linked-list ×1
memory ×1
memory-leaks ×1
memset ×1
new-operator ×1
objective-c ×1
output ×1
pointers ×1