在 RHEL6 上,我遇到了 realloc() 的一个奇怪问题。在程序的某个时刻,realloc() 返回 NULL(旧指针有一个地址并且有大量可用内存)。分配的是 200 个结构元素(结构如下)。出于某种原因,当我改为执行 realloc() 时,它可以工作,但是我必须将旧指针分配给新指针。下面是我的代码的简化版本。
这可能是服务器调优问题,而不是编程问题。你有什么意见?
谢谢。
//hearder file
typedef struct { /* Variable Node Detail Record */
long next;
long mask;
char *value;
// more stuff...
} NODETEST;
extern NODETEST *oldNodes;
extern NODETEST *newNodes;
//program
#define MAXSIZE 200
// do some stuff with oldNodes....
int alloc_nodes (void)
{
// Allocate or grow the table
oldNodes = (NODETEST *) malloc(MAXSIZE * sizeof(NODETEST));
if( oldNodes == NULL ) {
//handle exception...
exit(1);
}
//oldNodes = (NODETEST *) realloc(oldNodes,MAXSIZE * sizeof(NODETEST)); // *** FAILS
newNodes = (NODETEST *) realloc(oldNodes,MAXSIZE * sizeof(NODETEST)); // *** WORKS
if( newNodes == NULL ){
printf("errno=%d\n", errno );
}else{
oldNodes = newNodes; }
}