newbie question: returning structs from functions 
Author Message
 newbie question: returning structs from functions

i am having trouble with a function that i created. i have created a
priority queue and functions to use with the priority queue and this
function is giving me trouble. when debugging this it seems that
everything is ok before the function returns, but after it returns the
queue is broken and not logically in order. is it because i declared a
node of my queue inside the function and then return it. should i
return a pointer instead.

struct myNode removeHighestPriorityNode(){
        struct myNode nodeA;    //declare temporary node
        nodeA = *(head.next);   //nodeA set to node with highest priority
        head.next = nodeA.next; //give nodeA.next highest priority in queue
        nodeA.next = NULL;      //detach nodeA from queue
        return(nodeA);

Quote:
}



Thu, 31 Mar 2005 05:19:18 GMT  
 newbie question: returning structs from functions
You do not understand the semantics of an automatic variable.  nodeA is an
automatic variable.  It exists on the stack space for the
removeHighestPriorityNode routine.  When this function returns, the space is
made available for re-use.  Something else can write over the space used for
nodeA.

This can be a rather insidious problem in C programs.  It is possible for a
program to run OK with this kind of error.  Then, at a later time, a
modification by another programmer would seem to break the program!  (Stack
usage changes) In fact, the bug was introduced previously.


Quote:
> i am having trouble with a function that i created. i have created a
> priority queue and functions to use with the priority queue and this
> function is giving me trouble. when debugging this it seems that
> everything is ok before the function returns, but after it returns the
> queue is broken and not logically in order. is it because i declared a
> node of my queue inside the function and then return it. should i
> return a pointer instead.

> struct myNode removeHighestPriorityNode(){
> struct myNode nodeA;    //declare temporary node
> nodeA = *(head.next); //nodeA set to node with highest priority
> head.next = nodeA.next; //give nodeA.next highest priority in queue
> nodeA.next = NULL;      //detach nodeA from queue
> return(nodeA);
> }



Thu, 31 Mar 2005 08:08:16 GMT  
 newbie question: returning structs from functions

Quote:
> i am having trouble with a function that i created. i have
> created a priority queue and functions to use with the
> priority queue and this function is giving me trouble. when
> debugging this it seems that everything is ok before the
> function returns, but after it returns the queue is broken
> and not logically in order. is it because i declared a node
> of my queue inside the function and then return it. should i
> return a pointer instead.

You haven't provided enough context, but your code seems a little
strange.

Quote:
> struct myNode removeHighestPriorityNode(){
>      struct myNode nodeA;    //declare temporary node
>      nodeA = *(head.next);     //nodeA set to node with
>      highest priority head.next = nodeA.next; //give
>      nodeA.next highest priority in queue nodeA.next = NULL;
>          //detach nodeA from queue return(nodeA);
> }

Oops! //-comments do not wrap very well. Better to use C comments.
Let's reformat the code.

Quote:
> struct myNode removeHighestPriorityNode(){

This is a function which returns a struct by value.

Quote:
>     struct myNode nodeA;   /*declare temporary node */
>     nodeA = *(head.next);  /*nodeA set to node with highest

priority */

head.next obviously points to a struct myNode, which is copied and
assigned to nodeA.

Quote:
>     head.next = nodeA.next;/*give nodeA.next highest priority in

queue */

Now it seems like nothing points to the struct previously pointed
to by head.next. Possible memory leak.

Quote:
>     nodeA.next = NULL;     /*detach nodeA from queue */
>     return(nodeA);
> }

A copy of the highest priority node is returned. You should
probably return a pointer to the node instead.

This was the best I could do with the amount of information you
gave. If my post seems incomprehensible, please post some more of
your code, like the definition of struct myNode and the global
variable head.

--

Stockholm, Sweden



Thu, 31 Mar 2005 09:53:28 GMT  
 newbie question: returning structs from functions

[Top-post corrected]

Quote:


>> i am having trouble with a function that i created. i have
>> created a priority queue and functions to use with the
>> priority queue and this function is giving me trouble. when
>> debugging this it seems that everything is ok before the
>> function returns, but after it returns the queue is broken
>> and not logically in order. is it because i declared a node
>> of my queue inside the function and then return it. should i
>> return a pointer instead.

>> struct myNode removeHighestPriorityNode(){
>> struct myNode nodeA;    //declare temporary node
>> nodeA = *(head.next); //nodeA set to node with highest
>> priority head.next = nodeA.next; //give nodeA.next highest
>> priority in queue nodeA.next = NULL;      //detach nodeA
>> from queue return(nodeA);
>> }

> You do not understand the semantics of an automatic variable.
>  nodeA is an automatic variable.  It exists on the stack
> space for the removeHighestPriorityNode routine.  When this
> function returns, the space is made available for re-use.
> Something else can write over the space used for nodeA.

> This can be a rather insidious problem in C programs.  It is
> possible for a program to run OK with this kind of error.
> Then, at a later time, a modification by another programmer
> would seem to break the program!  (Stack usage changes) In
> fact, the bug was introduced previously.

Structs are passed by value, so yes, you can return a struct from
a function. I think you are thinking of returning a pointer to an
automatic variable. That you cannot do.

--

Stockholm, Sweden



Thu, 31 Mar 2005 09:58:58 GMT  
 newbie question: returning structs from functions

Quote:



>> i am having trouble with a function that i created. i have created a
>> priority queue and functions to use with the priority queue and this
>> function is giving me trouble. when debugging this it seems that
>> everything is ok before the function returns, but after it returns the
>> queue is broken and not logically in order. is it because i declared a
>> node of my queue inside the function and then return it. should i
>> return a pointer instead.

>> struct myNode removeHighestPriorityNode(){
>> struct myNode nodeA;    //declare temporary node
>> nodeA = *(head.next); //nodeA set to node with highest priority
>> head.next = nodeA.next; //give nodeA.next highest priority in queue
>> nodeA.next = NULL;      //detach nodeA from queue
>> return(nodeA);
>> }

Please don't top post.  I have reordered your response.
Quote:

>You do not understand the semantics of an automatic variable.  nodeA is an
>automatic variable.  It exists on the stack space for the
>removeHighestPriorityNode routine.  When this function returns, the space is
>made available for re-use.  Something else can write over the space used for
>nodeA.

>This can be a rather insidious problem in C programs.  It is possible for a
>program to run OK with this kind of error.  Then, at a later time, a
>modification by another programmer would seem to break the program!  (Stack
>usage changes) In fact, the bug was introduced previously.

It is perfectly acceptable to return the *value* of an automatic
variable.  The value is passed back to the calling function without
reference the area in memory that is no longer available.

The problem you describe applies to returning a pointer to an
automatic variable and having the calling function attempt to
dereference the pointer.

<<Remove the del for email>>



Thu, 31 Mar 2005 11:42:25 GMT  
 newbie question: returning structs from functions
You are correct.  I guess I really wanted to solve his problem.


Quote:

> [Top-post corrected]



> >> i am having trouble with a function that i created. i have
> >> created a priority queue and functions to use with the
> >> priority queue and this function is giving me trouble. when
> >> debugging this it seems that everything is ok before the
> >> function returns, but after it returns the queue is broken
> >> and not logically in order. is it because i declared a node
> >> of my queue inside the function and then return it. should i
> >> return a pointer instead.

> >> struct myNode removeHighestPriorityNode(){
> >> struct myNode nodeA;    //declare temporary node
> >> nodeA = *(head.next); //nodeA set to node with highest
> >> priority head.next = nodeA.next; //give nodeA.next highest
> >> priority in queue nodeA.next = NULL;      //detach nodeA
> >> from queue return(nodeA);
> >> }

> > You do not understand the semantics of an automatic variable.
> >  nodeA is an automatic variable.  It exists on the stack
> > space for the removeHighestPriorityNode routine.  When this
> > function returns, the space is made available for re-use.
> > Something else can write over the space used for nodeA.

> > This can be a rather insidious problem in C programs.  It is
> > possible for a program to run OK with this kind of error.
> > Then, at a later time, a modification by another programmer
> > would seem to break the program!  (Stack usage changes) In
> > fact, the bug was introduced previously.

> Structs are passed by value, so yes, you can return a struct from
> a function. I think you are thinking of returning a pointer to an
> automatic variable. That you cannot do.

> --

> Stockholm, Sweden



Fri, 01 Apr 2005 07:54:17 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Question on returning an array of struct from a function

2. Question re: storing ptrs to functions-that-return-float in structs

3. newbie question: passing struct as argument to function ?

4. Newbie question: Returning LPSTR from a function

5. Newbie question: Returning LPSTR from a function

6. How to return a struct in function

7. function that returns a const struct: clarifications

8. function that returns a const struct: kosher C?

9. Warning message when a function return a newly allocated struct

10. can I return struct from function?

11. imported function returning struct or null pointer.

12. I have prolem with returning a struct in a recursive function

 

 
Powered by phpBB® Forum Software