Return a const pointer from a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FerozAhmed
    New Member
    • Sep 2010
    • 4

    Return a const pointer from a function

    Hi, Let me know if there is any thing wrong with this code snippet.

    1. Will this code works fine every time?
    2. Returning "ptr" will be dangling?
    3. Where my "ptr" is stored? in Read-Only data segment/stack/some where else?

    =============== =============== =============== ============
    Code:
    #include <stdio.h>
    
    int a = 20;
    
    int *foo(void)
    {
    const int *ptr = NULL;
    ptr = &a;
    printf("%p\n",ptr);
    
    return (int *)ptr;
    }
    
    main()
    {
    int *x = NULL;
    x = foo();
    printf("%p\n",x);
    }
    =============== =============== =============== ============

    Result on GCC -
    [feroz@buzz TestPgms]$ ./a.out
    0x80495b4
    0x80495b4
    [feroz@buzz TestPgms]$


    PS: If im not wrong we should not return a local variable/pointer from a function, so I made it a 'const' pointer.
    Last edited by Banfa; Sep 14 '10, 03:06 PM. Reason: Added [code]...[/code] tags round the code
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Making it const makes absolutely no difference what so ever (especially since you cast away the constness) to if you are returning local data.

    Also you have it wrong, you can return local data, what you must avoid is returning a pointer to local data.

    Code:
    int fn1()
    {
        int a = 5;
        return a; // OK
    }
    
    int* fn2()
    {
        int a = 5;
        return &a; // Bad
    }
    You don't return a pointer to local data, you return a pointer to global data.

    Of course using global data in itself is considered very poor practice.

    Comment

    • FerozAhmed
      New Member
      • Sep 2010
      • 4

      #3
      Banfa,
      Actually, I have a global linked list, I need to return pointer to a data member for a given node.
      I can't have my entire code here, so i have given a code snippet similar to what I have.

      >>You don't return a pointer to local data, you return a pointer to global data.

      You mean to say, my code works fine all the time if i remove 'const', since I'm returning pointer to a global member?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        That's right, the const just modifies how the program can interact with the variable, not where the data is stored (on PCs).

        And what is important is the storage location of the data pointed to and returned; which is global as I said.

        Comment

        • FerozAhmed
          New Member
          • Sep 2010
          • 4

          #5
          >>That's right, the const just modifies how the program >>can interact with the variable, not where the data is >>stored (on PCs).

          Moving back to basics,you mean if i have a code like

          void foo(void)
          {
          const int x = 10;
          }

          My "x" still be stored on stack, but the only difference is "x" cannot be changed am I right?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Yes .

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Code:
              void foo(void) {
                  const int x = 10;
              }
              My "x" still be stored on stack, but the only difference is "x" cannot be changed am I right?
              To be painfully pedantic, the C Standard doesn't actually say anything about stacks. Your "x" will be an automatic variable that is initialized to 10, but that your program is not allowed to change.

              In reality, I haven't encountered any C compilers that don't use the stack to implement automatic variables.

              Comment

              Working...