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. Other Discussions
  3. Clever Code
  4. Random Stack Pointers

Random Stack Pointers

Scheduled Pinned Locked Moved Clever Code
data-structurestutorialquestionlounge
3 Posts 3 Posters 3 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.
  • B Offline
    B Offline
    Blake Miller
    wrote on last edited by
    #1

    When using the CMapXXX classes, you are supposed to pass in the address to a 'type' to receive the value at the key. It is even more interesting if you merely declare a pointer as a stack variable and pass that instead... In header file... typedef CTypedPtrMap< CMapWordToPtr, WORD, WORD* > HandleMap; class whatever { ... HandleMap m_HandleMap ... }; Then you perform a lookup like this, for example: BOOL WhatEver::DoesKeyExist(WORD wKey) { WORD *pDummy; :wtf: if( !m_HandleMap.Lookup(wKey, pDummy) ){ return FALSE; } return TRUE; } Assuming it DOES find the key, where does the value stored at the key go ? What does pDummy actually point to ?

    Any sufficiently gross incompetence is nearly indistinguishable from malice.

    R M 2 Replies Last reply
    0
    • B Blake Miller

      When using the CMapXXX classes, you are supposed to pass in the address to a 'type' to receive the value at the key. It is even more interesting if you merely declare a pointer as a stack variable and pass that instead... In header file... typedef CTypedPtrMap< CMapWordToPtr, WORD, WORD* > HandleMap; class whatever { ... HandleMap m_HandleMap ... }; Then you perform a lookup like this, for example: BOOL WhatEver::DoesKeyExist(WORD wKey) { WORD *pDummy; :wtf: if( !m_HandleMap.Lookup(wKey, pDummy) ){ return FALSE; } return TRUE; } Assuming it DOES find the key, where does the value stored at the key go ? What does pDummy actually point to ?

      Any sufficiently gross incompetence is nearly indistinguishable from malice.

      R Offline
      R Offline
      Rama Krishna Vavilala
      wrote on last edited by
      #2

      This is a very common mistake and I have seen this plenty of times made by beginners.

      IInterface** ppItf;
      pUnk->QueryInterface(ppItf);


      Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

      1 Reply Last reply
      0
      • B Blake Miller

        When using the CMapXXX classes, you are supposed to pass in the address to a 'type' to receive the value at the key. It is even more interesting if you merely declare a pointer as a stack variable and pass that instead... In header file... typedef CTypedPtrMap< CMapWordToPtr, WORD, WORD* > HandleMap; class whatever { ... HandleMap m_HandleMap ... }; Then you perform a lookup like this, for example: BOOL WhatEver::DoesKeyExist(WORD wKey) { WORD *pDummy; :wtf: if( !m_HandleMap.Lookup(wKey, pDummy) ){ return FALSE; } return TRUE; } Assuming it DOES find the key, where does the value stored at the key go ? What does pDummy actually point to ?

        Any sufficiently gross incompetence is nearly indistinguishable from malice.

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        In debug builds, local variables are initialized to CC, so the pointer will point to 0xCCCCCCCC and the app will crash when it tries to write to that address. In release builds, there's no predicting what value it'll get since it will pick up whatever data was at that point on the stack.

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

        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