memory access violation with linked-list
-
I have the folowing piece of code
typedef struct _HEAP_STRUCT { struct _HEAP_STRUCT *next; struct _HEAP_STRUCT *prev; }HEAP_STRUCT, *PHEAP_STRUCT; PHEAP_STRUCT pStruct; char HeapSpace[4096]; void Init(void) { pStruct->next = (PHEAP_STRUCT)HeapSpace[4096]; //<-- Access violation here pStruct->prev = (PHEAP_STRUCT)HeapSpace[0]; };
Every time it comes to the init procedure, it gives me the access violation. Even i attempt to putpStruct->next = NULL;
it still give me access violation. Does anyone have any idea? Thanks -
I have the folowing piece of code
typedef struct _HEAP_STRUCT { struct _HEAP_STRUCT *next; struct _HEAP_STRUCT *prev; }HEAP_STRUCT, *PHEAP_STRUCT; PHEAP_STRUCT pStruct; char HeapSpace[4096]; void Init(void) { pStruct->next = (PHEAP_STRUCT)HeapSpace[4096]; //<-- Access violation here pStruct->prev = (PHEAP_STRUCT)HeapSpace[0]; };
Every time it comes to the init procedure, it gives me the access violation. Even i attempt to putpStruct->next = NULL;
it still give me access violation. Does anyone have any idea? ThanksThat is because pStruct is declared as a PHEAP_STRUCT object which is actually a pointer to a _HEAP_STRUCT object. This means that no memory has been allocated to the structure itself, just the pointer. Therefore when you try to dereference the
next
parameter, you will get an access violation. In order to fix this you can do a number of things: 1) Declare pStruct as a HEAP_STRUCT object instead, then it will not be a pointer and memory will be allocated for this object on the stack.HEAP_STRUCT struct; void Init() { struct.next = HeapSpace[4095]; struct.prev = HeapSpace[0]; }
2) You can dynamically alloate memory for your pointer to your struct inside of your Init function and that will associate memory with your object.PHEAP_STRUCT pStruct; void Init() { pStruct = new HEAP_STRUCT; pStruct->next = HeapSpace[4095]; pStruct->prev = HeapSpace[0]; }
Finally, I do not know if this was a typo, but you assigned the HeapSpace[4096] element to the next variable. However for the array that you declared the 4095 index is the last element in the array that you delcared. Good Luck
Build a man a fire, and he will be warm for a day
Light a man on fire, and he will be warm for the rest of his life! -
That is because pStruct is declared as a PHEAP_STRUCT object which is actually a pointer to a _HEAP_STRUCT object. This means that no memory has been allocated to the structure itself, just the pointer. Therefore when you try to dereference the
next
parameter, you will get an access violation. In order to fix this you can do a number of things: 1) Declare pStruct as a HEAP_STRUCT object instead, then it will not be a pointer and memory will be allocated for this object on the stack.HEAP_STRUCT struct; void Init() { struct.next = HeapSpace[4095]; struct.prev = HeapSpace[0]; }
2) You can dynamically alloate memory for your pointer to your struct inside of your Init function and that will associate memory with your object.PHEAP_STRUCT pStruct; void Init() { pStruct = new HEAP_STRUCT; pStruct->next = HeapSpace[4095]; pStruct->prev = HeapSpace[0]; }
Finally, I do not know if this was a typo, but you assigned the HeapSpace[4096] element to the next variable. However for the array that you declared the 4095 index is the last element in the array that you delcared. Good Luck
Build a man a fire, and he will be warm for a day
Light a man on fire, and he will be warm for the rest of his life!