473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem with malloc / calloc. Size not LARGE enough!

I have had this problem for months now and it has been nagging me.

I have a large project that has several C++ DLL's, one of them uses malloc /
calloc / free for the other DLL's.
I process very large datafiles (100MB to 300 MB) that have more than 524,000
lines with 5 columns of numbers in text. I allocate 8 arrays with 524,000 or
more doubles , and 10 arrays of 32,768 doubles.

Is there a min size for malloc / calloc required to succeed?

I found that the allocation fails on the smaller 32K arrays. If I make them
larger, the allocation succeeds. It doesn't seem like a pointer porblem in my
code could cause this failure, since I can make the buffer bigger and it is
OK.

Here's the code that I use in the DLL:

int _cdecl mmDBase_dataAlloc
( OUT void **ppdData
, IN int nNumPts
, IN int nElementSize)
{
// \todo: The allocator doesn't work well if there is less than 2^19
bytes
const int knMinBytes = 524288;
if ((nNumPts * nElementSize) < knMinBytes) {
/* DEBUG -> */ nNumPts = (int)(((double)knMinBytes / nElementSize) +
0.5);
}
(*ppdData) = calloc((size_t)nNumPts, (size_t)nElementSize);
if (NULL == *ppdData) {
char acMsg[120];
_snprintf(acMsg, sizeof(acMsg), MM_THISFUNC ": Memory allocation
failed: Points:%d, SizeOf:%d, Pointer:0x%p"
, nNumPts, nElementSize, ppdData);
return mmVisa_errUpdateErrorMsg(VI_ERROR_ALLOC, acMsg);
}
return 0;
} // mmDBase_dataAlloc()

I set a breakpoint at /* DEBUG -> */ and try the allocation with the desired
num bytes and it always fails. If I set the num bytes to the min size (512K
bytes) it always succeeds.

I just don't get it? Any sugestions?
Nov 17 '05 #1
7 2144
mef526 wrote:
I have had this problem for months now and it has been nagging me.

I have a large project that has several C++ DLL's, one of them uses malloc /
calloc / free for the other DLL's.
What do you mean, "for the other DLLs"? Do you mean that this one DLL
handles memory for the others?
I process very large datafiles (100MB to 300 MB) that have more than 524,000
lines with 5 columns of numbers in text. I allocate 8 arrays with 524,000 or
more doubles , and 10 arrays of 32,768 doubles.

Is there a min size for malloc / calloc required to succeed?
0 is the minimum, but malloc/calloc are never required to succeed. A
conforming (but useless) implementation might always return NULL.
I found that the allocation fails on the smaller 32K arrays.
The first one? Or do some succeed first?

If I make them larger, the allocation succeeds. It doesn't seem like a pointer porblem in my
code could cause this failure, since I can make the buffer bigger and it is
OK.
It's hard to know what it causing the problem, but a corrupt heap is one
possibility.

Here's the code that I use in the DLL:

int _cdecl mmDBase_dataAlloc
( OUT void **ppdData
, IN int nNumPts
, IN int nElementSize)
{
// \todo: The allocator doesn't work well if there is less than 2^19
bytes
const int knMinBytes = 524288;
if ((nNumPts * nElementSize) < knMinBytes) {
/* DEBUG -> */ nNumPts = (int)(((double)knMinBytes / nElementSize) +
0.5);
}
(*ppdData) = calloc((size_t)nNumPts, (size_t)nElementSize);
if (NULL == *ppdData) {
char acMsg[120];
_snprintf(acMsg, sizeof(acMsg), MM_THISFUNC ": Memory allocation
failed: Points:%d, SizeOf:%d, Pointer:0x%p"
, nNumPts, nElementSize, ppdData);
return mmVisa_errUpdateErrorMsg(VI_ERROR_ALLOC, acMsg);
}
return 0;
} // mmDBase_dataAlloc()

I set a breakpoint at /* DEBUG -> */ and try the allocation with the desired
num bytes and it always fails. If I set the num bytes to the min size (512K
bytes) it always succeeds.

I just don't get it? Any sugestions?


The behaviour you describe is quite strange. Do you get the exact same
behaviour with the debug version of the CRT (which uses an error
checking memory allocator)? What if you use Win32 memory allocation
functions directly, rather than the CRT ones?

Tom
Nov 17 '05 #2

"Tom Widmer" <to********@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
mef526 wrote:
I have had this problem for months now and it has been nagging me. I have
a large project that has several C++ DLL's, one of them uses malloc /
calloc / free for the other DLL's.
What do you mean, "for the other DLLs"? Do you mean that this one DLL
handles memory for the others?

Yes- MS recommnends that all memory allocation / deallocation take place in
a single DLL so as to avoid heap probllems
I process very large datafiles (100MB to 300 MB) that have more than
524,000 lines with 5 columns of numbers in text. I allocate 8 arrays with
524,000 or more doubles , and 10 arrays of 32,768 doubles. Is there a min
size for malloc / calloc required to succeed?
0 is the minimum, but malloc/calloc are never required to succeed. A
conforming (but useless) implementation might always return NULL.
I found that the allocation fails on the smaller 32K arrays.


The first one? Or do some succeed first?

It never did work. I noticed the problem several months ago when using test
files that had only 12K lines instead of the 600K line monsters that I have
to deal with every day. I put in this hack code back then.

If I make them
larger, the allocation succeeds. It doesn't seem like a pointer porblem
in my code could cause this failure, since I can make the buffer bigger
and it is OK.
It's hard to know what it causing the problem, but a corrupt heap is one
possibility.

You know, that's what it I thought too, but I have linted my code and
haven't found an error. I have been running with this hack for nearly 8
months now and I have never seen any strange behaviour. Plus, what would
make the heap start to work by using a bigger buffer? The reason I looked at
it again was that new development caused another problem. In researching it
I came accross it again. (I solved the other problem).


Here's the code that I use in the DLL:

int _cdecl mmDBase_dataAlloc
( OUT void **ppdData
, IN int nNumPts
, IN int nElementSize)
{
// \todo: The allocator doesn't work well if there is less than 2^19
bytes const int knMinBytes = 524288;
if ((nNumPts * nElementSize) < knMinBytes) {
/* DEBUG -> */ nNumPts = (int)(((double)knMinBytes /
nElementSize) + 0.5);
}
(*ppdData) = calloc((size_t)nNumPts, (size_t)nElementSize);
if (NULL == *ppdData) {
char acMsg[120];
_snprintf(acMsg, sizeof(acMsg), MM_THISFUNC ": Memory allocation
failed: Points:%d, SizeOf:%d, Pointer:0x%p"
, nNumPts, nElementSize, ppdData);
return mmVisa_errUpdateErrorMsg(VI_ERROR_ALLOC, acMsg);
}
return 0;
} // mmDBase_dataAlloc()

I set a breakpoint at /* DEBUG -> */ and try the allocation with the
desired num bytes and it always fails. If I set the num bytes to the min
size (512K bytes) it always succeeds. I just don't get it? Any
sugestions?

The behaviour you describe is quite strange. Do you get the exact same
behaviour with the debug version of the CRT (which uses an error checking
memory allocator)? What if you use Win32 memory allocation functions
directly, rather than the CRT ones? I am using the debug CRT - From MSDN:
Multi-threaded Debug DLL (/MDd)
Defines _DEBUG, _MT, and _DLL so that debug multithread- and DLL-specific
versions of the run-time routines are selected from the standard .h files.
It also causes the compiler to place the library name MSVCRTD.lib into the
..obj file.

However- I have have trouble using the heap debug functions like malloc_dbg
etc. I am using GSL (GNU Scientific lib) in the code and seems like the
stdlib.h in it conflicts with CRTDBG.H


Tom

Nov 17 '05 #3

"Tom Widmer" <to********@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
mef526 wrote:
I have had this problem for months now and it has been nagging me. I have
a large project that has several C++ DLL's, one of them uses malloc /
calloc / free for the other DLL's.
What do you mean, "for the other DLLs"? Do you mean that this one DLL
handles memory for the others?

Yes- MS recommnends that all memory allocation / deallocation take place in
a single DLL so as to avoid heap probllems
I process very large datafiles (100MB to 300 MB) that have more than
524,000 lines with 5 columns of numbers in text. I allocate 8 arrays with
524,000 or more doubles , and 10 arrays of 32,768 doubles. Is there a min
size for malloc / calloc required to succeed?
0 is the minimum, but malloc/calloc are never required to succeed. A
conforming (but useless) implementation might always return NULL.
I found that the allocation fails on the smaller 32K arrays.


The first one? Or do some succeed first?

It never did work. I noticed the problem several months ago when using test
files that had only 12K lines instead of the 600K line monsters that I have
to deal with every day. I put in this hack code back then.

If I make them
larger, the allocation succeeds. It doesn't seem like a pointer porblem
in my code could cause this failure, since I can make the buffer bigger
and it is OK.
It's hard to know what it causing the problem, but a corrupt heap is one
possibility.

You know, that's what it I thought too, but I have linted my code and
haven't found an error. I have been running with this hack for nearly 8
months now and I have never seen any strange behaviour. Plus, what would
make the heap start to work by using a bigger buffer? The reason I looked at
it again was that new development caused another problem. In researching it
I came accross it again. (I solved the other problem).


Here's the code that I use in the DLL:

int _cdecl mmDBase_dataAlloc
( OUT void **ppdData
, IN int nNumPts
, IN int nElementSize)
{
// \todo: The allocator doesn't work well if there is less than 2^19
bytes const int knMinBytes = 524288;
if ((nNumPts * nElementSize) < knMinBytes) {
/* DEBUG -> */ nNumPts = (int)(((double)knMinBytes /
nElementSize) + 0.5);
}
(*ppdData) = calloc((size_t)nNumPts, (size_t)nElementSize);
if (NULL == *ppdData) {
char acMsg[120];
_snprintf(acMsg, sizeof(acMsg), MM_THISFUNC ": Memory allocation
failed: Points:%d, SizeOf:%d, Pointer:0x%p"
, nNumPts, nElementSize, ppdData);
return mmVisa_errUpdateErrorMsg(VI_ERROR_ALLOC, acMsg);
}
return 0;
} // mmDBase_dataAlloc()

I set a breakpoint at /* DEBUG -> */ and try the allocation with the
desired num bytes and it always fails. If I set the num bytes to the min
size (512K bytes) it always succeeds. I just don't get it? Any
sugestions?

The behaviour you describe is quite strange. Do you get the exact same
behaviour with the debug version of the CRT (which uses an error checking
memory allocator)? What if you use Win32 memory allocation functions
directly, rather than the CRT ones? I am using the debug CRT - From MSDN:
Multi-threaded Debug DLL (/MDd)
Defines _DEBUG, _MT, and _DLL so that debug multithread- and DLL-specific
versions of the run-time routines are selected from the standard .h files.
It also causes the compiler to place the library name MSVCRTD.lib into the
..obj file.

However- I have have trouble using the heap debug functions like malloc_dbg
etc. I am using GSL (GNU Scientific lib) in the code and seems like the
stdlib.h in it conflicts with CRTDBG.H


Tom


Nov 17 '05 #4
mef526 wrote:
"Tom Widmer" <to********@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
mef526 wrote:
I have had this problem for months now and it has been nagging me. I have
a large project that has several C++ DLL's, one of them uses malloc /
calloc / free for the other DLL's.
What do you mean, "for the other DLLs"? Do you mean that this one DLL
handles memory for the others?


Yes- MS recommnends that all memory allocation / deallocation take place in
a single DLL so as to avoid heap probllems


Yes, this is the best way to avoid problems.
It's hard to know what it causing the problem, but a corrupt heap is one
possibility.


You know, that's what it I thought too, but I have linted my code and
haven't found an error. I have been running with this hack for nearly 8
months now and I have never seen any strange behaviour. Plus, what would
make the heap start to work by using a bigger buffer? The reason I looked at
it again was that new development caused another problem. In researching it
I came accross it again. (I solved the other problem).


Hmmm. What happens if you allocate the small arrays before the large
ones? Have you done a lot of allocations prior to the first failure?
The behaviour you describe is quite strange. Do you get the exact same
behaviour with the debug version of the CRT (which uses an error checking
memory allocator)? What if you use Win32 memory allocation functions
directly, rather than the CRT ones?


I am using the debug CRT - From MSDN:
Multi-threaded Debug DLL (/MDd)
Defines _DEBUG, _MT, and _DLL so that debug multithread- and DLL-specific
versions of the run-time routines are selected from the standard .h files.
It also causes the compiler to place the library name MSVCRTD.lib into the
.obj file.


Have you tried the release version then? It is possible you're suffering
from a strange artefact of the allocator implementation. Smaller blocks
might be allocated using one technique and larger ones another, and the
smaller block allocation technique is failing for some reason. Stepping
into the allocation calls might shed some light on this, if you
understand the basics of writing allocators.
However- I have have trouble using the heap debug functions like malloc_dbg
etc. I am using GSL (GNU Scientific lib) in the code and seems like the
stdlib.h in it conflicts with CRTDBG.H


A solution to your problem might be to use HeapCreate and HeapAlloc for
memory allocation, which will only involve modifying a few lines of code.

Tom
Nov 17 '05 #5
I have spent more time with this issue now and it looks like a heap
corruption problem. I have traced it down to a call to strdup(). I recoded
it to use malloc instead of strdup and the problem has gone away AFAICT. In
the strdup I do not pass the pointer across the DLL boundary, it is just for
scratch within a single function. It seems like the strdup uses a different
allocator and heap than the free() that is done later in the function.
Here's a helpful link that I found so far-

http://mail.python.org/pipermail/pyt...er/256723.html

and also

http://support.microsoft.com/kb/q190799/

http://www.tech-archive.net/Archive/...5-02/0564.html

Although MS won't cop to the problem within the same DLL. One thread I was
reading recommended turning off intrinsics.


"Tom Widmer" <to********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
mef526 wrote:
"Tom Widmer" <to********@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
mef526 wrote:

I have had this problem for months now and it has been nagging me. I
have a large project that has several C++ DLL's, one of them uses malloc
/ calloc / free for the other DLL's.

What do you mean, "for the other DLLs"? Do you mean that this one DLL
handles memory for the others?


Yes- MS recommnends that all memory allocation / deallocation take place
in a single DLL so as to avoid heap probllems


Yes, this is the best way to avoid problems.
It's hard to know what it causing the problem, but a corrupt heap is one
possibility.


You know, that's what it I thought too, but I have linted my code and
haven't found an error. I have been running with this hack for nearly 8
months now and I have never seen any strange behaviour. Plus, what would
make the heap start to work by using a bigger buffer? The reason I looked
at it again was that new development caused another problem. In
researching it I came accross it again. (I solved the other problem).


Hmmm. What happens if you allocate the small arrays before the large ones?
Have you done a lot of allocations prior to the first failure?
The behaviour you describe is quite strange. Do you get the exact same
behaviour with the debug version of the CRT (which uses an error checking
memory allocator)? What if you use Win32 memory allocation functions
directly, rather than the CRT ones?


I am using the debug CRT - From MSDN:
Multi-threaded Debug DLL (/MDd)
Defines _DEBUG, _MT, and _DLL so that debug multithread- and DLL-specific
versions of the run-time routines are selected from the standard .h
files. It also causes the compiler to place the library name MSVCRTD.lib
into the .obj file.


Have you tried the release version then? It is possible you're suffering
from a strange artefact of the allocator implementation. Smaller blocks
might be allocated using one technique and larger ones another, and the
smaller block allocation technique is failing for some reason. Stepping
into the allocation calls might shed some light on this, if you understand
the basics of writing allocators.
However- I have have trouble using the heap debug functions like
malloc_dbg etc. I am using GSL (GNU Scientific lib) in the code and seems
like the stdlib.h in it conflicts with CRTDBG.H


A solution to your problem might be to use HeapCreate and HeapAlloc for
memory allocation, which will only involve modifying a few lines of code.

Tom

Nov 17 '05 #6
mef526 wrote:
I have spent more time with this issue now and it looks like a heap
corruption problem. I have traced it down to a call to strdup(). I recoded
it to use malloc instead of strdup and the problem has gone away AFAICT. In
the strdup I do not pass the pointer across the DLL boundary, it is just for
scratch within a single function. It seems like the strdup uses a different
allocator and heap than the free() that is done later in the function.
Here's a helpful link that I found so far-

http://mail.python.org/pipermail/pyt...er/256723.html Although MS won't cop to the problem within the same DLL. One thread I was
reading recommended turning off intrinsics.


I don't think intrinsics are part of the problem since strdup, malloc
and free aren't intrinsics AFAIK. I'm also not quite sure how strdup is
resolved to be the same as _strdup.

My best guess is that you have some messed up linker settings. Glad
you've solved it in another way, anyway.

Tom
Nov 17 '05 #7
Tom Widmer wrote:
mef526 wrote:
I have spent more time with this issue now and it looks like a heap
corruption problem. I have traced it down to a call to strdup(). I
recoded it to use malloc instead of strdup and the problem has gone
away AFAICT. In the strdup I do not pass the pointer across the DLL
boundary, it is just for scratch within a single function. It seems
like the strdup uses a different allocator and heap than the free()
that is done later in the function. Here's a helpful link that I found
so far-

http://mail.python.org/pipermail/pyt...er/256723.html


Although MS won't cop to the problem within the same DLL. One thread I
was reading recommended turning off intrinsics.

I don't think intrinsics are part of the problem since strdup, malloc
and free aren't intrinsics AFAIK. I'm also not quite sure how strdup is
resolved to be the same as _strdup.

My best guess is that you have some messed up linker settings. Glad
you've solved it in another way, anyway.

Tom

To track down memory corruption errors in code, App verifier is a great
tool. See the following link:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/appverif/appverif/overview_of_application_verifier.asp>

Ronald Laeremans
Visual C++ team
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
37
by: Harsimran | last post by:
Can any one explain what are far pointers and what is the difference between malloc and calloc .Which is better ?
11
by: lohith.matad | last post by:
Hi all, Though the purpose of both malloc() and calloc() is the same, and as we also know that calloc() initializes the alloacted locations to 'zero', and also that malloc() is used for bytes...
8
by: venkatesh | last post by:
hai to everybody, i am having doubt in difference between the malloc and calloc function. please tell where to use and when to use those functions?
14
by: Roka100 | last post by:
Hi all, I tried 2 programs : #include <stdio.h> #include <string.h> 1, int main(void){ char *str = NULL;
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
13
by: coosa | last post by:
Dear all, Using the conio implementation i wanted to create a dynamic string, whereby its size would be determined after each keyboard hit; in other words, i don't want to ask the user to...
46
by: lovecreatesbea... | last post by:
Do you prefer malloc or calloc? p = malloc(size); Which of the following two is right to get same storage same as the above call? p = calloc(1, size); p = calloc(size, 1);
29
by: marvinla | last post by:
Hello! I'm a beginner in C, and I'm having trouble with a pointer-to-pointer reallocation. This piece of code works well, but Valkyrie warns some parts (pointed below), and is breaking my real...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.