This question already has answers here:
How to access a local variable from a different function using pointers?
(10 answers)
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 6 years ago.
This is my C++ code .
According to me , it should give output:
abc
Garbage
abc
But it is giving output:
abc
Garbage
Garbage
#include<bits/stdc++.h>
using namespace std;
char **func()
{
char* PA = new char[10];
PA[0]='a';
PA[1]='b';
PA[2]='c';
PA[3]='\0';
printf("%s\n",PA);
printf("Garbage\n");
char **PPA = &PA;
return PPA;
}
int main()
{
printf("%s\n",*func());
return 0;
}
Where am I doing wrong?
char **PPA = &PA;
Retrieves the address of the variable PA itsself, which is an automatic variable and goes out of scope as soon as the function terminates. That means you have undefined behavior here. The C standard doesn't guarantee any consistent behavior, so anything may happen, including what you experienced.
To fix that, you could change the function prototype to char* func() and return PA directly and remove PPA altogether.
Related
This question already has answers here:
Does "Undefined Behavior" really permit *anything* to happen? [duplicate]
(9 answers)
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 1 year ago.
This is a simple program I wrote to induce segfault
#include<iostream>
using namespace std;
char* func(){
char ch='a';
char *ptr=&ch;
return ptr;
}
int main()
{
char *ptr=func();
cout<<*ptr;
}
As variable ch must've been freed after func() scope ends, I thought that this program would give segmentation fault.
But it shows output as 'a'. What am i missing ?
This question already has answers here:
Return a pointer that points to a local variable [duplicate]
(3 answers)
Closed 2 years ago.
void* func1(int a)
{
void *b = &a;
return b;
}
int main(int argc, char** argv) {
int d = 9;
void *c = func1(d);
printf("%d\n", *((int*)c));
return 0;
}
I am confused about why it works.
From my understanding, memory of "a" will release after func1.
Why the void* c can still access "a" after finish the function?
I am confused about why it works.
The behaviour of the program is undefined. The program doesn't "work" - In my opinion. Although "workingness" may be subjective.
From my understanding, memory of "a" will release after func1.
Correct.
Why the void* c can still access "a" after finish the function?
Behaviour of the program is undefined. Appearance of having particular behaviour is within the limits of undefined behaviour. Anything is within limits of undefined behaviour because the language imposes no limits on behaviour of the program.
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 3 years ago.
What is the output of following C program And how is the output printed when all variables of fun() after closing got their memory de-allocated.
#include<stdio.h>
int * fun()
{
int q = 10;
int *p;
p = &q;
return p;
}
int main()
{
int *res;
res= fun();
printf("%d", *res);
return 0;
}
I expected the pointer res to point to null
You are returning a pointer to an address on the stack. A reasonable compiler will give you a warning like this:
"warning C4172: returning address of local variable or temporary: q"
But basically the behavior is undefined and also depends on your level of optimization.
This question already has answers here:
How to access a local variable from a different function using pointers?
(10 answers)
Closed 6 years ago.
If I understand correctly, variables that are not dynamically allocated are supposed to be deleted at the end of their scope.
However, I wanted to try it out with this code (which I think is not correct as I am supposed to use dynamic allocation) :
int* function()
{
int a = 3;
return &a;
}
int main(int argc, char const *argv[]) {
int* a(function());
std::cout << *a << std::endl; // prints 3
}
Why can I still access the value of the variable a by using the pointer returned by the function when it is supposed not to exist anymore ?
a and hence the return value from function has gone out of scope. You are just lucky.
Just be careful and compile with all the warnings enables - and take heed of those warnings.
The fact that you can access the value is pure luck. The code has undefined behaviour (since the variable is destroyed) and the compiler can generate whatever it wants - but you cannot rely on it.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Pointer to local variable
#include <iostream>
using namespace std;
char* func();
int main() {
char* str;
str = func();
cout<<str;
return 0;
}
char* func() {
char * str;
char p[] = "priyanka is a good girl";
str = p;
cout<<str<<"\n";
return str;
}
gives the output,
priyanka is a good girl
priy
I did not understand what just happened here, why an incomplete array was given as output. I am a little new with this. Please help.
Your function func() returns a pointer to a local variable, which later causes undefined behaviour when you try to access it.
In func2() char p[] is a local variable initialized on stack. Returning a pointer to stack variable is a bad idea(and is undefined behaviour as well), and I think your string "priyanka is a good girl" got overwritten when the function returned.