Issues with jdeReAllocEx

BOster

BOster

Legendary Poster
I have used jdeReAllocEx in the past w/o any issues (although sparingly, I often find myself using a link list when I have a dynamically growing array). I have been banging my head against the wall all morning with it though. It has intermittently been failing by not allocating memory and causing jde to crash when making the first call to do the initial allocation.

In other words the following fails:

ZCHAR *line=(ZCHAR *)NULL;

if(size == 0)
size = len + 1;
else
size += len;

line = jdeReAllocEx(line, size);


The following doesn't seem to have any issues:

if(size == 0)
{
size = len + 1;
line = jdeAlloc(COMMON_POOL, size, MEM_ZEROINIT);
}
else
{
size += len;
line = jdeReAllocEx(line, size);
}


Are you allways supposed to call jdeAlloc for the initial malloc? I have never done this in the past either in jde or out and if jdeReAllocEx is supposed to work the same way as realloc then, based on the documentation, I can just use jdeReAllocEx. Obviously I could have some sort of buffer overrun someplace else, but its weird that by making my initiall allocation call to jdeAlloc I don't see any issues.
 
In standard C, and also in JDE, you must call malloc (or jdeAlloc) to get memory, and realloc (or jdeReAllocEx ) to realloc. This is why your second call works.

If it worked in the past, it is surprising, at best it is undefined behavior in C.
 
From MSDN

[ QUOTE ]

void *realloc(
void *memblock,
size_t size
);


Parameters

memblock
Pointer to previously allocated memory block.

size
New size in bytes.

<snip>

The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes.


[/ QUOTE ]

I have all ways used it this way in the past (pass null on first call) and there are tons of examples in pristine code that does the same thing.

Of course there is no telling what Oracle is doing inside of jdeReAllocEx (jdeReallocInternal). I just always assumed it was a light wrapper to realloc.
 
Back
Top