Access violation in C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 2
Reputation: ahad is an unknown quantity at this point 
Solved Threads: 0
ahad ahad is offline Offline
Newbie Poster

Access violation in C

 
0
  #1
Apr 10th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Access violation in C

 
0
  #2
Apr 10th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,620
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Access violation in C

 
0
  #3
Apr 10th, 2007
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 2
Reputation: ahad is an unknown quantity at this point 
Solved Threads: 0
ahad ahad is offline Offline
Newbie Poster

Re: Access violation in C

 
0
  #4
Apr 11th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Access violation in C

 
0
  #5
Apr 11th, 2007
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC