CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

returning structs by value

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cwedgwood



Joined: 16 Mar 2012
Posts: 12

View user's profile Send private message

returning structs by value
PostPosted: Wed Sep 19, 2012 6:43 am     Reply with quote

Code:
typedef struct
{
   float r,i;
} complex;


complex cadd(complex a, complex b)
{
   complex c;
   c.r=a.r+b.r;
   c.i=a.i+b.i;
   return c;
}


So, this should work, but does not. It compiles fine.

The return value is always junk. I know the solution will be to pass by ref.

Anybody managed to make return struct by value work?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Sep 19, 2012 8:09 am     Reply with quote

Please read the sticky thread on top of this forum before posting!
- Always post your compiler version number (as it might be an old bug that has already been solved).
- Always post your used PIC number (as PIC16 uses another compiler than PIC18).
- Always post the smallest but complete compilable program that we can copy/paste into our compiler (because we are lazy and because there might be a problem in the code you didn't think relevant).
cwedgwood



Joined: 16 Mar 2012
Posts: 12

View user's profile Send private message

PostPosted: Wed Sep 19, 2012 8:20 am     Reply with quote

ckielstra wrote:
Please read the sticky thread on top of this forum before posting!
- Always post your compiler version number (as it might be an old bug that has already been solved).
- Always post your used PIC number (as PIC16 uses another compiler than PIC18).
- Always post the smallest but complete compilable program that we can copy/paste into our compiler (because we are lazy and because there might be a problem in the code you didn't think relevant).


None of those things are required to answer the question I posed, so I'll ask again:-

Anybody managed to make return struct by value work?
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Wed Sep 19, 2012 8:38 am     Reply with quote

ckielstra has posted thousands of responses on this board over 8 years. He has the status of a venerable professor. When the professor asks for specific information the student doesn't get to dictate the conditions under which his question will be answered. Unlike college where professors are paid to help students no such obligation exists here. Please don't annoy our experts since many with intermediate expertise still have much to learn from them.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Sep 19, 2012 8:56 am     Reply with quote

The compiler version number is certainly relevant. The others perhaps less so in this instance, but are generally important, sometimes vital.

Passing structs by value in an environment with as limited memory as PICs is not particularly useful nor often sensible. Most PICs, 16s and 18s do not have fully viable variable stacks, so the passing mechanism must be by copies in memory (yes, I know stacks generally are also in memory so...). Early Cs did not have this ability, for much the same reasons, i.e. limited resources, as above. Pass by value has since become an expected and "standard", but potentially memory intensive, part of C. Early C compilers required pointers to be passed and used as references. This remains the most efficient means of passing and returning structures in C. Or put another way, dont pass the structure, pass a pointer to it instead. But I feel sure you know all this already.

As to your original question: has anybody managed to make return struct by value work [in CCS C]. We really don't know. Maybe it did work with a particular compiler version, but doesn't now, or doesn't with the version of the compiler you're using. Maybe someone got it working... maybe. So please post your compiler version and stop shouting at us! Just remember all disappointment comes from expectation. This is a problem for you because you expect it to work, and it doesn't, hence your angst. Instead simply accept that it doesn't and find something that does work: which you've already said you've done!

RF Developer
cwedgwood



Joined: 16 Mar 2012
Posts: 12

View user's profile Send private message

PostPosted: Wed Sep 19, 2012 9:01 am     Reply with quote

OK

18F26K22

4.132
jeremiah



Joined: 20 Jul 2010
Posts: 1349

View user's profile Send private message

PostPosted: Wed Sep 19, 2012 9:39 am     Reply with quote

Also bear in mind, without the compilable code that shows the issue, we might not know if the problem is with the code you posted or the code you used to display it. We know for a fact some versions of the compiler have trouble displaying floating points, so the function might be working perfectly and the display routine bugged. Since we cannot see how you did that, we have no clue.

If you are looking at it in some versions of MPLAB or Proteus, sometimes the values are incorrectly displayed as well. There can be a lot of things that affect it.

That is why the "smallest compilable program" is important to us. It's not to make your life hard, but to help isolate the problem, and if a bug is found, help give you something to provided to CCS support so they can fix it for you.
temtronic



Joined: 01 Jul 2010
Posts: 9228
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Sep 19, 2012 1:51 pm     Reply with quote

One reason to supply a small program is that old guys like me can't type or read too good and might mistype your code( I have enough trouble already !) thus compounding your original problem.

When you say the result is always junk...it'd be helpful to supply a known set of good data(input and output),again so old guys like me can see what it should be versus what's happening.

The easier you make it for all here, the faster someone will figure out what's going on.

hth
jay
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Thu Sep 20, 2012 2:33 am     Reply with quote

cwedgwood wrote:
Anybody managed to make return struct by value work?


I've used it before. (and had it work)

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
cwedgwood



Joined: 16 Mar 2012
Posts: 12

View user's profile Send private message

PostPosted: Thu Sep 20, 2012 3:23 am     Reply with quote

bkamen wrote:
cwedgwood wrote:
Anybody managed to make return struct by value work?


I've used it before. (and had it work)

-Ben


Thanks Ben.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Sep 20, 2012 3:33 am     Reply with quote

Two of the requested items have been provided but the complete example program is still missing...
We are terrible people here on this forum, instead of just answering the question we keep on pressing to get the list of three requirements complete. Smile

I felt bad and created a small test program in v4.077. First tests show that the compiler is working correctly and returns the struct.
Now I feel even worse because I've wasted my time on probably some programming error from the original topic poster. My original point on requesting a complete program has proven itself right again!

The OP is too lazy to post a complete test program and expects us to create a test program from his code fragments. Sorry, but the OP is the one having a problem, not we, so let him do the work instead of wasting our spare time. Especially now it looks the problem is outside the code posted here.

Unless a compilable program is posted I'm out of here.
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Thu Sep 20, 2012 4:13 am     Reply with quote

Perhaps Google is to blame. Google may make some feel any inquiry has to be effortless on the behalf of the inquirer. A bit like asking asking the teacher to do your homework and having the teacher say yes. But for some they even get annoyed if the teacher asks for a pencil to do the work.
Now we have an expert like ckielstra believing this thread was a waste of his valuable time. When one of the best of us feels used then we all are being used. Now hard would it to have been to supply a small compilable
amount of code that exhibited the problem and the output that was in question?
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Thu Sep 20, 2012 12:50 pm     Reply with quote

not to be argumentative, but...


Perhaps the question was exploratory and not examining an existing problem.

With the other retarded compilers I've used that don't support some of the most basic things that C offers (function args, bitfields, etc...)

Maybe he just wanted to know if it works first before bothering to code it up and THEN having it not work.

Maybe he just wanted a simple "yes, it works with CCS" or "no, CCS doesn't support that"...

Just like CCS's weird support of pointers to constants (for reasons of the PIC architecture not some whim of the compiler author).

Not everything C is supported by all microcontroller compilers --- nor is exactly what they support always mentioned specifically in the docs.

Perhaps the OP could have framed the question better to indicate he wasn't chasing down a bug or a problem - but rather wondering if a method would even work before embarking down a long road of coding only to find it doesn't work.

Not so terrible.

Every -- one deep breath.

-Ben

p.s. we should be thankful he's not running Proteus. (and trying to hide it)
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Sep 20, 2012 3:38 pm     Reply with quote

I can confirm that the code in post #1 works in V4.132 with PIC18.

Presuming the OP reported everything correctly, the occurance seems to depend at least on additional conditions.
Which once more refers to the necessity of an example reproducing the problem.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot 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


Powered by phpBB © 2001, 2005 phpBB Group