Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. memory access violation with linked-list

memory access violation with linked-list

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tongc
    wrote on last edited by
    #1

    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 put pStruct->next = NULL; it still give me access violation. Does anyone have any idea? Thanks

    P 1 Reply Last reply
    0
    • T tongc

      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 put pStruct->next = NULL; it still give me access violation. Does anyone have any idea? Thanks

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #2

      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!

      T 1 Reply Last reply
      0
      • P Paul M Watt

        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!

        T Offline
        T Offline
        tongc
        wrote on last edited by
        #3

        Excellent answer! It explains everything !!! Thank you very much!!!! I really appreciate it!!!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups