#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 函数分配更多内存。当我运行这个程序时,我得到了这个错误:
对象 0x100100c17 的错误:未分配重新分配的指针。在 malloc_error_break 中设置断点进行调试
当我查看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();
我究竟做错了什么?