d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Easy C Question
Prev12
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Sep 18 2012 08:02pm
Just 1 more question :P

What does the asterisk in the function header mean? And why is this function wrong? Is the return type supposed to be a pointer?

Code


int *get_value(int x) {
  int y = x * 2;
return &y;
}

Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Sep 18 2012 08:11pm
yes, the function's return type is a pointer. and yes, you're returning a pointer. but your code won't compile because you're returning the address of a variable on the stack. that's a huge no-no.

in C, you need to allocate the memory required to store the data that you want to manipulate (via malloc), and typically, you should never allocate memory and allow that memory to leave the scope of the function it was allocated in.

perhaps you should tell us what it is you're trying to do, and then we can tell you how far off you are from even beginning to attempt to do so.

This post was edited by irimi on Sep 18 2012 08:16pm
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Sep 18 2012 08:20pm
Quote (irimi @ Sep 18 2012 10:11pm)
yes, the function's return type is a pointer.  and yes, you're returning a pointer.  but your code won't compile because you're returning the address of a variable on the stack.  that's a huge no-no.

in C, you need to allocate the memory required to store the data that you want to manipulate (via malloc), and typically, you should never allocate memory and allow that memory to leave the scope of the function it was allocated in.

perhaps you should tell us what it is you're trying to do, and then we can tell you how far off you are from even beginning to attempt to do so.


Oh these are just practice questions I was going over. I was just trying to clarify what a pointer did and it's purpose.

The last question was just a practice question we were given and I didn't know the answer to.

So the above problem doesn't work because &y refers to the address, not the value, y is pointing towards?

God I miss java :P

This post was edited by lopelurag on Sep 18 2012 08:21pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Sep 18 2012 08:27pm
either you've been skipping a lot of classes, or they're teaching things quite out of order if you're asking these sorts of questions...

have they not taught you anything about passing by reference vs. passing by value, memory allocation, stack vs. heap yet?

(this is also why C is such a terrible language to begin learning programming with... you have to actually know those things in order to really understand how your program works)
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Sep 18 2012 08:41pm
Quote (irimi @ Sep 18 2012 10:27pm)
either you've been skipping a lot of classes, or they're teaching things quite out of order if you're asking these sorts of questions...

have they not taught you anything about passing by reference vs. passing by value, memory allocation, stack vs. heap yet?

(this is also why C is such a terrible language to begin learning programming with... you have to actually know those things in order to really understand how your program works)


We've only been in class for 2 weeks, and we don't do shit in discussion which kinda blows.

We just went over pointers last week, everything before that was basic knowledge and bitwise operators topics.

I understand stack and heap and memory allocation back when I did java
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Sep 18 2012 11:03pm
OK, then you should know that variables local to functions in Java are created on the stack (at least for primitives; it's more complicated for larger data structures -- the pointer is allocated on the stack while the data is allocated on the heap, but this is automatically done behind the scenes).

In C, any local variables inside a function are allocated on the stack. The only way to allocate memory on the heap is by explicitly doing so via a call to malloc. So if you look at the function you posted above again, it should be pretty obvious why the compiler is complaining -- you've allocated a variable on the stack and returned a pointer to it to outside of the function's scope. The moment the function exits (i.e. when it returns), the stack gets popped and the memory location referenced by that pointer is no longer valid.

This post was edited by irimi on Sep 18 2012 11:03pm
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll