473,382 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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?

================================================== =======
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
6 14684
Banfa
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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
>>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
9,065 Expert Mod 8TB
Yes .
Sep 15 '10 #6
donbock
2,426 Expert 2GB
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

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

Similar topics

5
by: Bob | last post by:
Hi, I have a std::vector, say myVec, of some user defined object, say myOb. In my code, I have a function that searches myVec for a particular myOb. The way I was doing this was searching...
7
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above....
9
by: Robert | last post by:
Hi all, class Homer { public: int dot(int) const {return 1;} ... } what's the meaning of const after function name?
19
by: Sergey Koveshnikov | last post by:
Hello, If my function return a pointer on a memory area, where do I free it? e.x.: char *foo() { char *p; p = malloc(10); strcpy(p, "something"); return(p); }
21
by: Roman Werpachowski | last post by:
I can't understand this: double * x = new double; const double * p = x; delete p; works. Why am I allowed to delete a const pointer? On the same note: why am I allowed to do this: class...
7
by: lovecreatesbea... | last post by:
Shoud we declare non-pointer function parameters with const keywords? int main(void){ int f(const int i); int i; f(i); return 0; }
4
by: Rusty | last post by:
Can somebody can explain the following behaviour. I have a small code as below: class B { public: int b; B () { b = 0; }; void foo2 (int h) { b = h; } // non-const function };
14
by: Wolfgang | last post by:
As I understand, a const member function is used by const object to ensure that its instance isn't modified throughout its life. Am I missing something.. #include <iostream> using namespace...
3
by: George2 | last post by:
Hello everyone, 1. Returning non-const reference to function local object is not correct. But is it correct to return const reference to function local object? 2. If in (1), it is correct...
3
by: kk_oop | last post by:
Hi. I just wrote a function that returns a const pointer: MyClass { .... public: virtual ReturnClass * const getReturnClass( ); .... }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.