Is the compiler placing memory on the heap? [duplicate] - c

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 6 years ago.
I don't understand why the following C code works (prints '53'). I thought that int a would be placed on the stack and wiped away as soon as foo() exits. If the compiler is instead placing int a on the heap, is there a way to tell it not to?
#include "stdio.h"
int * foo()
{
int a = 53;
int * b = &a;
return b;
}
int main(void)
{
int * c = foo();
printf("%d\n",*c);
return 0;
}

The integer a is stored on the stack. The reason this works is that function foo returns the address of a and function main dereferences this address to print it before the contents of a's address are overwritten. This program works by accident in this instance. If you want to preserve the value of a for the life of the program you'll either need to apply the static qualifier to a's declaration, or allocate a on the heap.

Related

Unexpected C behaviour with pointers [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 3 years ago.
Suppose I have this block of code:
#include <stdio.h>
#include <unistd.h>
int *local_pointer(void)
{
int x = 6;
return &x;
}
void add(void)
{
int a;
a = 4;
a = a + 1;
}
int main()
{
int *result;
result = local_pointer();
printf("int is %d\n", *result);
add();
printf("int is %d\n", *result);
return 0;
}
The output of this code is
int is 6
int is 5
I am confused as to why this is?
If we go through main, the result variable is returned a pointer to x and then when it the pointer is dereferenced the its value is 6 however why does calling the add function make the next printf statement print 5?
Your code has a bug. Your local_pointer function returns a pointer to its x variable. But as soon as that function returns, x no longer exists since it was local. So when you dereference the returned pointer, the results are undefined.
If I had to guess, I'd say that a happens to occupy the memory that x used to occupy. But that's just a guess. The behavior might be due to something else. It might change with compiler options. It might change with a different compiler. Who knows? Fix the bug and the mystery will go away.
The function local_pointer probably (cf. other comments on undefined behaviour) returns a pointer to the stack. So I think you are basically printing the content of the stack. The call to add writes the value 5 to the stack, that's what your second printf is outputing 5.

C - false value of integer var [duplicate]

This question already has answers here:
(Why) is using an uninitialized variable undefined behavior?
(7 answers)
Is reading into uninitialized memory space ALWAYS ill advised?
(6 answers)
Closed 4 years ago.
The output in the following code is really mysterious.
#include <stdio.h>
void funktion(int x) {}
void test() {
int foo;
printf("test = %d\n", foo);
}
int main() {
test();
funktion(5);
test();
return 0;
}
Why will the foo-var initialised with the value of the parameter of funktion()? I don't understand why foo-var is initialised by the function-parameter. I know that I can fix it with a explicit initialisation - but I want to understand the mystery. Thanks!
I've added comments to your main() function that explains why is it so. By the way, you're in undefined behaviour land, and different platforms might yield different results.
int main() {
// no return, no parameter, one local variable of type int.
// This pushes an int on the stack, uninitialized (the local variable).
test();
// Stack pointer is back to where it was.
// no return, one int parameter, no local variables.
// This pushes an int with value=5 on the stack (the parameter).
funktion(5);
// Stack pointer is back to where it was.
// no return, no parameter, one local variable of type int.
// This pushes an int on the stack, uninitialized (the local variable).
// Previous value of that memory comes from funktion() call.
test();
// Stack pointer is back to where it was.
return 0;
}

Returning pointer to local variable [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
How to access a local variable from a different function using pointers?
(10 answers)
Closed 8 years ago.
What is the problem associated with returning a pointer to a local variable?
And (i don't know if its legal to do this) What is the problem with returning a reference to pointer in main?
ex:
int *p;
p=abc();
where abc would be returning int&
P.S. Sorry if not clear. I am confused too :P
You can syntactically return a pointer to a local variable, but that variable will no longer be valid after the function has returned.
If your code is:
int *abc() {
int n = 3;
return &n;
}
void foo() {
int m = 4;
}
int main() {
int *p;
p=abc();
foo();
printf("%d", *p);
}
then m will probably overwrite n and the result will be 4.
In simple terms, local variables are available only as long as it is in that function is in the function stack, once the function is done, its popped out of the function stack.

C static variables [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
I've been trying to wrap my head around static variables in C and so I wrote this:
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int *pointer;
void stat();
int main()
{
stat();
printf("%i", *pointer);
}
void stat()
{
int c = 2;
pointer = &c;
}
This does work and display 2 in the command line, but I don't understand why.
Doesn't int c cease to exist when the function stat exits? Then what does pointer point to? And how does it retain the value of the integer? Why can I do without making int c static here?
c ceases to exist, but you're still allowed to look at that memory location, and you're getting lucky; the value is still there at that moment-- it hasn't been overwritten by anything else yet.
Your understanding is correct; pointer is pointing at memory you shouldn't be looking at once stat returns.
c as an object ceases to exist.
But your pointer does not cease to exist because it is global.
Your pointer is pointing to the memory address where c was.
It will continue pointing there unless you poi it somewhere else.
But, the content at that address could change if something else is assigned that address or overlaps that in memory later.
I should add that nothing here is a static anything.
The static keyword has special significance and it has a deferent effect depending on the scope in which it is declared.

Returning an address of local variable behaviour [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can a local variable's memory be accessed outside its scope?
input:
#include <stdlib.h>
#include <stdio.h>
int func2(void);
int* func1(void);
int func2(void)
{
int* b;
b = func1();
printf("%d", *b);
printf("%d", *b);
printf("%d", *b);
}
int* func1()
{
int a = 13;
return &a;
}
int main()
{
func2();
}
Output:
13 -1077824828 -1077824828
Can someone explain what happened in the stack and OS? Why the result changed from 13 to garbage after getting the value of the pointer?
Sure.
The result will differ between debug and release (clean).
A local variable is EBP-(some offset) if you look at the assembly.
This means, HIGHER IN STACK, as in "further".
This is the address you return.
Normally it would be untouched if the function just returns. In debug build on some compilers, it would be garbaged on purpose to help you catch the dangling pointer error faster. Now, printf call reuses the same addresses in the stack to pass parameters and for its own local variables (it has some). They will be written to the address emptied by func1 return, thus overwriting whatever is pointed by the address you obtained.
Calling printf creates a new stack frame that overwrites the location previously occupied by a.

Resources