During writing a code, I need to define dynamic arrays several times, so I wrote the following simple function for defining a double 1D array:

Function for Difining 1-D Dynaimcs Arrays for Storing Double Values 
double *allocation_1d_double(int size)
double *array;
if ( (array = (double *)malloc(size*sizeof(double)) ) == NULL )
printf("\nError, memory not allocated.\n");
return array;

The function works well for more than 20 calling but suddenly it gives the following error:
“Unhandled exception in a.exe: Access Violation"

I checked the code several times and I'm quite sure that error is happened in calling this function. If there was any problem in memory, I expected to receive memory error message but now, I don't know what is wrong with code. Would you please help me?

Recommended Answers

All 4 Replies

something is probably corrupting the memory pool some place else in your program and that's a difficult problem to find. If you have a large progrm I would start by commenting out large blocks of code until the problem goes away. That at least narrows down the search.

Does this occur for specific values of size ? If yes, which compiler are you using ? Also post the relevant code..

Thanks for the guide.

The code is so large and for other size of input data this error is occurred in some other allocation part. so It seems that I have memory problem. My compiler is Visual C++ v.6.0 and my pc has32 bit processor.

I think that I should change the code in the way that I can free some of the arrays after using so that I will have some free memory space for writing rest of the code. What is your idea? Does using 64 bit processor or other compilers have effect? Please advise.


>>Does using 64 bit processor or other compilers have effect?

The compiler and processor are not relevent to memory leaks. Offhand I'd say your program has buffer overruns somewhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.