Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
ANSI C: Returning struct variable on the stack from a func

 
Post new topic   Reply to topic Printable version
 View previous topic  winsock linker error? Post :: Post win32  View next topic  
Author Message
grazzhoppa
Novice


Joined: Jan 03 2007
Posts: 29
Offline

PostPosted: Fri Nov 30, 2007 8:21 pm    Post subject: ANSI C: Returning struct variable on the stack from a func Reply to topic Reply with quote

I was under the impression that C will create a copy of the returned struct so that memory corruption is not being risked. Is this true?

I was told that using the returned struct risks memory corruption since the struct being returned by AFunction() was created on the stack within the function and hence it's memory area gets marked "available" to the OS when the function exists.

typedef struct _whatever {
    /* variables */

} StructReturn;

StructReturn AFunction(void) {
    StructReturn stack_struct;

    return stack_struct;
    /* C compiler will create a copy identical to "stack_struct" and automatically allocate memory on the stack of the calling function for this copy, no?*/

}
Back to top
View users profile Send private message Add User to Ignore List
Bak
?ls -s
0 in


Age:24
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Fri Nov 30, 2007 9:05 pm    Post subject: Reply to topic Reply with quote

yeah it'll create a copy... whoever told you memory will be corrupted probably meant if you returned a pointer to a struct declared on the stack inside the function.
_________________
SubSpace Discretion: A Third Generation SubSpace Client
Back to top
View users profile Send private message Add User to Ignore List AIM Address
grazzhoppa
Novice


Joined: Jan 03 2007
Posts: 29
Offline

PostPosted: Sat Dec 01, 2007 11:34 am    Post subject: Reply to topic Reply with quote

No, he was explicit about returning a regular struct created on the stack within the function.

The person recommended 1)creating a pointer, 2)using malloc(), 3)then returning the dereferenced pointer such as this:

AStruct AFunction(void) {
    AStruct *malloced_AStruct = (AStruct*) malloc( sizeof(AStruct) );
    return *malloced_AStruct;
}

But, in the context of what we were talking about, this would've been a memory leak because the calling function doesn't free() anything.
Back to top
View users profile Send private message Add User to Ignore List
Bak
?ls -s
0 in


Age:24
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Sat Dec 01, 2007 11:53 am    Post subject: Reply to topic Reply with quote

yeah.

in general people discourage returning structs as it does create a copy which is a bit wasteful for large structs. A better way to do it is to pass in a pointer to the struct as an argument, then modify it in the function.
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Non-Subspace Related Coding All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 673 page(s) served in previous 5 minutes.

phpBB Created this page in 0.523583 seconds : 29 queries executed (83.5%): GZIP compression disabled