Solved

0xC0000005:Access Violation error when using malloc.

Posted on 2003-11-06
9
1,784 Views
Last Modified: 2011-09-20
Hi,
I am experiencing this strange problem which I have never come across before. Please see the code snippet below:

Pixel=(PIXEL *)malloc(sizeof(PIXEL));

PIXEL is a strucutre which al has a pointer pointing to itself.
The problem I am facing is I am getting an access violation error when executing this statement. I traced the problem to the function __sbh_alloc_block (int intSize) line number 621 in the file sbheap.c Please see the statement below :
        //  unlink entry from list
        pEntry->pEntryPrev->pEntryNext = pEntry->pEntryNext;

I really dont know whats causing this errror. Even malloc() is not returning a NULL value so I think its not really related to insufficient memory. Looks like the program is trying to allocate a corrupted memory block, but I cant figure exactly what the problem is. Also, just before the call to malloc, I am incrementing a long int variable. Everytime the value is 13. I have already wasted 2 days over this problem. It will be really helpful for me if anybody can throw some light on this. You can also send comments/suggestion/solution on abhimj@lycos.com
Thanks and best regards.
0
Comment
Question by:Abhimj
9 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9693412
problem is that you are trying to access memory whihc has not been allocated to you...
it is not possible to pin point the reason until you post the type as well as details (struct) of pEntry and its intialization

most likely cause is that you have some junk value in pEntry or pEntryPrev or pEntryNext
0
 

Author Comment

by:Abhimj
ID: 9693472
Well, sunnycoder, heres the declaration :

typedef struct PIXEL_{
      INT32 col;
      INT32 row;

      struct PIXEL_ *pPixel;
}PIXEL;

>>problem is that you are trying to access memory whihc has not been allocated to you...
I am not trying to access the memory loacation without first allocating it. malloc() is not returning.

>>most likely cause is that you have some junk value in pEntry or pEntryPrev or pEntryNext
Thats right, pEntry has value 0x016fe00c whereas pEntryPrev and  pEntryNext both have value 0.
But I dont get the reason behind it.

The troublesome statement is inside a while loop. And it is executing exacly for 12 times before giving this error. Why do you think its working fine for first 12 iterations and failing on the 13th?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9693484
may be it is overstepping some array boundary ... and if its a linked list (looks doubly linked to me), may be you did not maintain NULL in the last node or something similar

It is very hard to pin point unless I get to see the code


>>>post the type as well as details (struct) of pEntry and its intialization

also post the code leading upto the loop and the loop itself
0
 

Author Comment

by:Abhimj
ID: 9693494
BTW, my code is calling realloc() multiple times before this particular code. Could this be the culprit?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 9693503
>BTW, my code is calling realloc() multiple times before this particular code. Could this be the culprit?
anything could be... but from what you said in the last post, I am more inclined towards believing that either you messed up the loop or you messed up initializing your list
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9693808

Hi Abhimj,

I think that Sunnycoder is right -- you're corrupting the heap.


Two things to incorporate:

After you malloc() a PIXEL, make sure that pPixel is set to NULL so that any garbage that was in the heap won't be interpretted as a pointer.

After:  Pixel=(PIXEL *)malloc(sizeof(PIXEL));

Pixel->pPixel = NULL;

or

memset (Pixel, 0, sizeof (PIXEL));


You're already testing that malloc() returns a non-NULL value.  You should also test the value returned by realloc().  If malloc() fails to move the block it returns NULL but leaves the block alone so at best you'll "lose" the item that you tried to move.  At worst, the NULL pointer will be deferenced and your program will target random data.

Ptr = realloc (Ptr);    /*  Bad thing to do!  */

Ptr1 = realloc (Ptr);  /*  Better thing to do  */
if (Ptr1)
  Ptr = Ptr1;


Kent
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 9704336
0
 

Author Comment

by:Abhimj
ID: 9706046
Hi all,
I finally found out the problem. sunnycoder, Kent, thanks for the clues.you were right, the was actually overstepping an array boundary and thus corrupting the heap. Thanks for the link KurtVon. I had already seen the page. That wasnt the problem. Because I even tried not using realloc(). But that didnt help. Anyways, thanks to all once again.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9706210
Hi Abhimj,

Thanks for the points but your choice of grade suprises me... C for a correct estimation of fault without looking at the code !!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
port mapped I/o query 10 137
C programming help for beginner sort routine 5 259
Inorder binary search tree 5 153
Socket Programming (Unix) 8 98
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now