#include <stdio.h>
#include "stdlib.h"
int main(int argc, const char * argv[])
{
int i,j;
char takimlar[4][8]= { "TAKIM A ","TAKIM B","TAKIM C","TAKIM D"};
char *var = malloc(sizeof(char)*1);
int sonuclar[4][4] = {
{0,3,4,2},
{1,2,3,1},
{2,3,2,3},
{0,2,1,4}
};
for (i=0; i<4; i++) {
if (sonuclar[i][2]>sonuclar[i][3]) {
var = realloc(var,8);
for (j=0; j<8; j++) {
var += takimlar[i][0];
}
}
else if(sonuclar[i][2]<sonuclar[i][3]){
var = realloc(var,8);
for (j=0; j<8; j++) {
var += takimlar[i][1];
}
}
}
printf("%s",var);
return 0;
}
我想動態分配內存中的變量,然後我想使用realloc函數分配更多的內存。當我運行這個程序,我得到這個錯誤:正在重新分配的指針未被分配
error for object 0x100100c17: pointer being realloc'd was not allocated. set a breakpoint in malloc_error_break to debug
當我在malloc.c
文件一看,我看到這幾行:
zone = find_registered_zone(old_ptr, &old_size);
if (!zone) {
malloc_printf("error for object %p: pointer being realloc'd was not allocated.\n"
"set a breakpoint in malloc_error_break to debug\n", old_ptr);
malloc_error_break();
我究竟做錯了什麼?
我們可以看到您的整個源代碼嗎?您顯示的代碼是正確的。 – md5
malloc最初是否會返回有效的內存塊? realloc應該工作不管,但我似乎記得這是一個實施依賴。 – Joe
你不可能給我們相關的代碼。製作一個小型,獨立完整的示例。 –