1

I have a double link list that stores some information. When I try and return one of the values inside the link list, I get the warning: function returns address of local variable.

This is my return statement:

return curr_val->value;

value is of type const void*.

Method signature is like: void *get_val(int key)

curr_val is of a struct type. It is equal to one of my nodes in the linked list.

how do I return the value and it not disappear after I return? BTW, I can not change the method signature.

5
  • 1
    What is the method signature? What is curr_val? Commented Jan 26, 2012 at 2:52
  • 1
    Edit your question to show us the function definition. Commented Jan 26, 2012 at 2:59
  • 1
    You haven't added enough info, but @Borealid is probably correct. Also +1 for heeding a copmiler warning. Commented Jan 26, 2012 at 3:00
  • You should probably edit the question to add the code of the function so that we can work out why the compiler is warning you. But, as Seth said, you get good marks from us for paying attention to the compiler warnings. Anything the compiler bothers to warn about is probably a problem; they struggle to avoid giving false positives because that would undermine the usefulness of the warnings. Commented Jan 26, 2012 at 3:23
  • Duplicate: stackoverflow.com/questions/8743411/… Commented Jan 26, 2012 at 3:25

1 Answer 1

3

The problem is likely that you have assigned the address of a stack-allocated variable to value. You need to use new or malloc to get memory for variables you intend to have continue to exist beyond the current stack frame.

Sign up to request clarification or add additional context in comments.

4 Comments

I also have a set_val(int key, const void* value) method. There I am just setting "curr_val->value = value;". Is that where I should be using malloc? If so, how can I use malloc and still get it equal to the value passed in?
@user972276: If you're passing it in, the problem is where it's being gotten by whoever is calling set_val.
new is not going to help for a C question.
@Complicatedseebio: oops, didn't look at the tags. I mentioned malloc anyhow, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.