#Simple C code doesnt work

31 messages · Page 1 of 1 (latest)

pulsar pawn
#
#include <stdio.h>

double *add(double a, double b);

int main (void){
    double a=2, b=4;
    double *sum = add(a,b);

    printf("%f, %f, %f",a,b,*sum);

    return 0;
}

double *add(double a, double b){
    double sum = a + b;

    return &sum;
}

Trying to find out whats wrong, it prints out nothing even tho it should, I am currently experimenting with dangling pointers but unsure if im printing it correctly

neat flintBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

high grotto
#

Your add function is returning the address of a local variable which gets automatically deallocated when the function returns.

#

If you want to return a pointer you shoud allocate memory on the heap so that it gets preserved after the function returns.

#

Example with your code:

#include <stdio.h>
#include <stdlib.h>

double *add(double a, double b){
    double *sum = malloc(sizeof(double));
    *sum = a + b;

    return sum;
}

int main (void){
    double a=2, b=4;
    double *sum = add(a,b);

    printf("%f, %f, %f",a,b,*sum);
    free(sum);

    return 0;
}
carmine thunder
#

Or better, why is add returning a pointer at all, @pulsar pawn?

pulsar pawn
carmine thunder
#

I see

#

Then you need to understand the difference between stack allocation and heap allocation

#

In a simplified model of C/C++, there are two areas of memory from which you can allocate: stack or heap

#

a and b are stack-allocated variables. They each take 8 bytes of stack memory (8 being sizeof(double))

#

sum here is also stack-allocated. Also, worth noting, a and b here are new variables that will also be stack allocated

#

In this last line here, you are returning the address in stack memory of where sum variable exists

#

But then, once you exist this function, that stack memory is "released"

#

So you return a pointer to undefined data

#

Here's what your stack memory may look like before calling add
[main::a (double type)], [main::b (double type)]

#

And then, just as you call add, your memory becomes
[main::a (double type)], [main::b (double type)] [main::sum (pointer type)]

#

And, just when you enter add,

#

[main::a (double type)], [main::b (double type)] [main::sum (pointer type)] [add::a] [add::b]

#

Then, you create the sum variable in add
[main::a (double type)], [main::b (double type)], [main::sum (pointer type)], [add::a], [add::b], [add::sum]

#

What you are returning, is the memory location of [add::sum]. However, once the function returns, your memory goes back to looking like [main::a (double type)], [main::b (double type)] [main::sum (pointer type)] (everything afterwards becomes undefined values)

#

Technically, the memory at [add::sum] should be unchanged for now, however, you next call printf, which will create stack-allocated variables in the memory address right after [main::sum], most definitely overwriting whatever value used to be at [add::sum]

#

That's why you're getting incorrect results.

#

Does that help, @pulsar pawn?

pulsar pawn
#

Yes somewhat

#

Thanks

#

!solved

neat flintBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

carmine thunder
#

you mean this?