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?
================================================== ======= - #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.
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. -
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.
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?
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.
>>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?
Banfa 9,065
Expert Mod 8TB - 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
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?
|
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);
}
|
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...
|
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;
}
|
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
};
|
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...
|
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...
|
by: kk_oop |
last post by:
Hi. I just wrote a function that returns a const pointer:
MyClass
{
....
public:
virtual ReturnClass * const getReturnClass( );
....
}
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
| |