linux - Why does realloc() fail where malloc() succeds in C? -



linux - Why does realloc() fail where malloc() succeds in C? -

on rhel6, i'm facing unusual problem realloc(). @ point in program, realloc() returns null (the old pointer has address , there's plently of memory available). what's beingness allocated 200 construction elements (structure below). reason, when realloc() instead, works, have assign old pointer new one. below simplified version of code.

this perhaps server tuning issue more programming one. opinion? thanks.

//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 // stuff oldnodes.... int alloc_nodes (void) { // allocate or grow 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; } }

your first phone call malloc size s , realloc same size s. wrong: have pass realloc new wanted size (independently of current size - not increment). here, there big chance realloc returns same pointer received. btw not clear why want malloc followed realloc. gives more detail.

if want dynamic table size auto-adjusts, need allocate initial size storing size in variable (e.g. alloc_size) , maintain current number of occupied elements in variable (e.g. n_elem) . when add together element increment number. when table total reallocate it. here sketch

nodetest *newnodes = null; int allocated_elem = 0; int n_elem = 0; #define alloc_incr 200

then @ each addition:

if (n_elem >= alloc_size) { // first time realloc malloc since nodes == null alloc_size += alloc_incr; nodes = (nodetest *) realloc(nodes, alloc_size * sizeof(nodetest)); if (nodes == null) { //handle exception... exit(1); } } // add together element @ nodes[n_elem] n_elem++;

recall realloc acts malloc when received pointer null (case of first call). allocates initial table. subsequent calls reallocate adjusting size constant increment (here 200). other schemes possible enlargement of table, instance can multiply size factor (e.g. 2) starting 32:

if (n_elem >= alloc_size) { // first time realloc malloc since nodes == null alloc_size = (alloc_size == 0) ? 32 : alloc_size * 2;

regarind fail , works comments: clear if assign oldnodes (in fail code) newnodes not assigned , keeps initial value 0 (null) since declared global variable , not initialized (well suppose, extern here). test if (newnodes == null) fail.

c linux malloc realloc rhel6

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -