944,117 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2991
  • C RSS
Apr 10th, 2007
0

Access violation in C

Expand Post »
Hi

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:
  1. /*----------------------------------------------------------------------------
  2. Function for Difining 1-D Dynaimcs Arrays for Storing Double Values
  3. ------------------------------------------------------------------------------*/
  4.  
  5. double *allocation_1d_double(int size)
  6.  
  7. {
  8. double *array;
  9. if ( (array = (double *)malloc(size*sizeof(double)) ) == NULL )
  10.  
  11. {
  12. printf("\nError, memory not allocated.\n");
  13.  
  14. exit(1);
  15. }
  16. return array;
  17. }

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?
Last edited by WaltP; Apr 11th, 2007 at 3:19 am. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ahad is offline Offline
2 posts
since Apr 2007
Apr 10th, 2007
0

Re: Access violation in C

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Apr 10th, 2007
0

Re: Access violation in C

Does this occur for specific values of size ? If yes, which compiler are you using ? Also post the relevant code..
Last edited by ~s.o.s~; Apr 10th, 2007 at 12:23 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 11th, 2007
0

Re: Access violation in C

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.

BR,
Ahad
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ahad is offline Offline
2 posts
since Apr 2007
Apr 11th, 2007
0

Re: Access violation in C

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: boot?
Next Thread in C Forum Timeline: Function declaration error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC