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. local pointer need to initialize or not?

local pointer need to initialize or not?

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 3 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.
  • S Offline
    S Offline
    samzcs
    wrote on last edited by
    #1

    Hi , I read a block of code, there is a function like:

    void funcA( unsigned int * dtc, unsigned int* status)
    {
    int rval;
    unsigned int* lstatus;
    rval = funcB(dtc, lstatus);
    .....
    }

    int funcB(unsigned int *idtc, unsinged int* istatus ) {
    *istatus = XXX;
    }

    I understood, lstatus is not initialized, is that good way to use a local pointer like the above?

    J CPalliniC 2 Replies Last reply
    0
    • S samzcs

      Hi , I read a block of code, there is a function like:

      void funcA( unsigned int * dtc, unsigned int* status)
      {
      int rval;
      unsigned int* lstatus;
      rval = funcB(dtc, lstatus);
      .....
      }

      int funcB(unsigned int *idtc, unsinged int* istatus ) {
      *istatus = XXX;
      }

      I understood, lstatus is not initialized, is that good way to use a local pointer like the above?

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      That code will not work. You are correct to question it. If the code actually runs it is only because of the way that the compiler lays down the stack and/or what the exact code did just before the first method was called. In the method the pointer is considered to be non-initialized but because it is a pointer that was created on the stack it will have a value, whatever was in the memory that the stack is using at that point. Thus correctly stated 'garbage'.

      S 1 Reply Last reply
      0
      • S samzcs

        Hi , I read a block of code, there is a function like:

        void funcA( unsigned int * dtc, unsigned int* status)
        {
        int rval;
        unsigned int* lstatus;
        rval = funcB(dtc, lstatus);
        .....
        }

        int funcB(unsigned int *idtc, unsinged int* istatus ) {
        *istatus = XXX;
        }

        I understood, lstatus is not initialized, is that good way to use a local pointer like the above?

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        That is wrong. The usual idiom is instead

        void funcA( unsigned int * dtc, unsigned int* status)
        {
        int rval;
        unsigned int lstatus;
        rval = funcB(dtc, &lstatus);
        .....
        }

        int funcB(unsigned int *idtc, unsinged int* istatus ) {
        *istatus = XXX;
        }

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • J jschell

          That code will not work. You are correct to question it. If the code actually runs it is only because of the way that the compiler lays down the stack and/or what the exact code did just before the first method was called. In the method the pointer is considered to be non-initialized but because it is a pointer that was created on the stack it will have a value, whatever was in the memory that the stack is using at that point. Thus correctly stated 'garbage'.

          S Offline
          S Offline
          samzcs
          wrote on last edited by
          #4

          It works. Yes, I think this is typical wild pointer.

          J 1 Reply Last reply
          0
          • S samzcs

            It works. Yes, I think this is typical wild pointer.

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            samzcs wrote:

            It works.

            It runs, which is a bit different. If the compiler changes in the future or the flow does then it might stop. Or it might cause problems in other places.

            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