Thread: Struct as return value from a function?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    4

    Struct as return value from a function?

    Hello I'm totally new to C and trying to translate a matlab program to C.

    The question I have is if it's ok to return a struct from a function. I know I should be using pointers but haven't really gotten the hang of it yet, and as i understand you don't have to use them but for speed purposes you should?

    Anyways here is some code that I wonder if it's ok to use in C.

    Code:
    typedef struct results {
    	double F_TT_out;
    	double F_PP_out;
    	double F_GG_out;
    	double F_HH_out;
    	double F_SS_out;
    	double F_EE_out;
    	double Tout;
    } results;
    
    results the_under(results output_r);
    results the_over(results output_c);
    
    results the_process(//here there will be inputs){
    
    results output_c;
    results output_r;
    
    values are assigned to output_r...
    
    double n_iter = 0;
    double E = 100;										/* Error */
    double last_temp_out = 10000						/* assigned value, will store the temp from previous iteration */
    	while (E>0.0001){
    		n_iter=n_iter+1;
    
                            if(n_iter==????){
    				break;
    		}
                    
                    output_c = the_under(output_r);
    
                    output_r = the_over(output_c);
    
                    E = fabs(last_temp_out-output_r.Tout);
    		last_temp_out = output_r.Tout;
    
    	}
    return //some struct
    }
    
    results the_under(results output_r){
    
    results under;
    
    Calculations that assign values to the struct results under...
    
    return under;
    }
    
    results the_over(results output_r){
    
    results over;
    
    Calculations that assign values to the struct results over...
    
    return over;
    }
    Is this way of coding an ok way?

    Thank you so much for any answers!

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Returning this struct costs about 14 times the time of returning a pointer on 32-bit systems. On newer, 64-Bit systems, it shrinks to 7 times - but still, just malloc a pointer and return it for now.

    Maybe more if if the pointer would have been passed in a register.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Take into consideration the time it takes to allocate memory for that struct, as well. So long as your program runs quickly, there's no need to bother with malloc. It's easier to merely return the struct. Or better yet, pass in a pointer to a struct and fill in it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    That would be the best idea I suppose, but the poster said he hadn't really understood pointers, so I posted an easier solution.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Brafil View Post
    That would be the best idea I suppose, but the poster said he hadn't really understood pointers, so I posted an easier solution.
    Hadn't understood pointers and you unnecessarily suggested using malloc. That makes no sense.

    NRVO likely means that returning the struct by value might not be worse for performance at all over simply filling it in by a passed-in pointer. Just return the struct and don't prematurely optimise.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Didn't think about that.

    BTW, Limeny, nobody tells you that you have to write an optimized program - learn the language first and do what you want as long as it works.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Brafil View Post
    Didn't think about that.

    BTW, Limeny, nobody tells you that you have to write an optimized program - learn the language first and do what you want as long as it works.
    The whole idea of translating MATLAB to C is optimization... Otherwise - MATLAB is doing its work, No?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by vart View Post
    The whole idea of translating MATLAB to C is optimization... Otherwise - MATLAB is doing its work, No?
    No Or, more accurately, not necessarily.

    The most common reason I've seen for translating matlab code to some other language is when matlab has been used for prototyping, and there is a wish to provide some functionality in another program that doesn't require installation of matlab, purchase of licenses, etc.

    Optimisation is only one of the potential considerations in such cases.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by grumpy View Post
    No Or, more accurately, not necessarily.

    The most common reason I've seen for translating matlab code to some other language is when matlab has been used for prototyping, and there is a wish to provide some functionality in another program that doesn't require installation of matlab, purchase of licenses, etc.

    Optimisation is only one of the potential considerations in such cases.
    in this case some automatic MATLA2C converter will do the work
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4
    Well it's not for optimization purely a conversion from matlab to c in order to implement the what the code does in an user defined component in a simulation program for chemical engineering. I've tried using matlabs own converter but I don't understand the code it gives at all and since I need to take inputs from the program I don't know how to implement that part. But if anyone knows a good converter please do tell I haven't found any except matlabs own.

    But anyways doing it this way doesn't give that big a difference performance wise?

    Anyways if I were to use pointers would this be the way to use them?

    Code:
    int main()
    {
    	struct results{
    		int hat;
    		int leek;
    	};
    	struct results *output_r;
    	struct results *over;
    	struct results r1;
    	struct results r2;
    	
    	int the_over(struct results *output_r,struct results *over);
    	{
    
    
    	over->hat = output_r->leek + 1;
    
    
    	return 0;
    
    	};
    
    r1.hat=2;
    r1.leek = 5;
    r2.hat= 1;
    r2.leek=7;
    
    
    the_over(&r1,&r2);
    
    printf("Now hat is %i",r2.hat);
    		
    
    		return 0;
    
    }

  11. #11
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Kinda. You have to define the_over outside of main, not inside - you can't define functions in functions (at least not according to any standard). Then you don't need the declarations of "struct results *output_r" and "*over" before the parameter list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM