S
Snis Pilbor
First, let me announce that this is very possibly off-topic because
malloc is a specific third party accessory to c, etc. I spent about an
hour trying to find a more appropriate newsgroup and failed. If anyone
could point one out, I would be much obliged. I'm using MSVC .NET, but
the only .NET specific newsgroups I could find were vbasic newsgroups
and you'll agree a C malloc question is less off-topic here than there,
at least.
I seem to be suffering the exact opposite of fragmentation: I have no
problem whatsoever allocation a large array of structures, but if I try
to allocate room for just 1 single structure, the program immediately
throws an exception. Debugging indicates the exception is indeed
thrown by malloc itself and not by some later line of code.
I temporarily solved the problem as follows:
original code:
void myfunction( void )
{
struct mystructure *m;
m = (struct mystructure *) malloc( sizeof(struct mystructure) );
...
}
band-aid solution code:
#define MAX_MYSTRUCTURES 1000
void myfunction( void )
{
static int x = 0;
static struct mystructure *buf = NULL;
struct mystructure *m;
if ( !buf )
buf = (struct mystructure *) malloc( MAX_MYSTRUCTURES *
sizeof(struct mystructure) );
m = &buf[x++];
...
}
Amazingly the original code throws an exception when it reaches the
malloc line, while the band-aid fix code, which allocates MUCH MORE
memory, does not.
(the structure in question is pretty small-- 4 pointers, 4 ints and 3
chars)
If anyone could go above and beyond and offer me some insight even
though this isn't pure unblemished by-the-books C, I would be much
indebted to you.
Snis
malloc is a specific third party accessory to c, etc. I spent about an
hour trying to find a more appropriate newsgroup and failed. If anyone
could point one out, I would be much obliged. I'm using MSVC .NET, but
the only .NET specific newsgroups I could find were vbasic newsgroups
and you'll agree a C malloc question is less off-topic here than there,
at least.
I seem to be suffering the exact opposite of fragmentation: I have no
problem whatsoever allocation a large array of structures, but if I try
to allocate room for just 1 single structure, the program immediately
throws an exception. Debugging indicates the exception is indeed
thrown by malloc itself and not by some later line of code.
I temporarily solved the problem as follows:
original code:
void myfunction( void )
{
struct mystructure *m;
m = (struct mystructure *) malloc( sizeof(struct mystructure) );
...
}
band-aid solution code:
#define MAX_MYSTRUCTURES 1000
void myfunction( void )
{
static int x = 0;
static struct mystructure *buf = NULL;
struct mystructure *m;
if ( !buf )
buf = (struct mystructure *) malloc( MAX_MYSTRUCTURES *
sizeof(struct mystructure) );
m = &buf[x++];
...
}
Amazingly the original code throws an exception when it reaches the
malloc line, while the band-aid fix code, which allocates MUCH MORE
memory, does not.
(the structure in question is pretty small-- 4 pointers, 4 ints and 3
chars)
If anyone could go above and beyond and offer me some insight even
though this isn't pure unblemished by-the-books C, I would be much
indebted to you.
Snis