By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

Return a const pointer from a function

P: 4
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?

================================================== =======
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int a = 20;
  4.  
  5. int *foo(void)
  6. {
  7. const int *ptr = NULL;
  8. ptr = &a;
  9. printf("%p\n",ptr);
  10.  
  11. return (int *)ptr;
  12. }
  13.  
  14. main()
  15. {
  16. int *x = NULL;
  17. x = foo();
  18. printf("%p\n",x);
  19. }
================================================== =======

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.
Sep 14 '10 #1
Share this Question
Share on Google+
6 Replies


Banfa
Expert Mod 5K+
P: 8,916
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.

Expand|Select|Wrap|Line Numbers
  1. int fn1()
  2. {
  3.     int a = 5;
  4.     return a; // OK
  5. }
  6.  
  7. int* fn2()
  8. {
  9.     int a = 5;
  10.     return &a; // Bad
  11. }
  12.  
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.
Sep 14 '10 #2

P: 4
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?
Sep 15 '10 #3

Banfa
Expert Mod 5K+
P: 8,916
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.
Sep 15 '10 #4

P: 4
>>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?
Sep 15 '10 #5

Banfa
Expert Mod 5K+
P: 8,916
Yes .
Sep 15 '10 #6

Expert 100+
P: 2,400
Expand|Select|Wrap|Line Numbers
  1. void foo(void) {
  2.     const int x = 10;
  3. }
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.
Sep 15 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.