View previous topic :: View next topic |
Author |
Message |
cwedgwood
Joined: 16 Mar 2012 Posts: 12
|
returning structs by value |
Posted: Wed Sep 19, 2012 6:43 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Sep 19, 2012 8:09 am |
|
|
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). |
|
![](templates/subSilver/images/spacer.gif) |
cwedgwood
Joined: 16 Mar 2012 Posts: 12
|
|
Posted: Wed Sep 19, 2012 8:20 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Wed Sep 19, 2012 8:38 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Wed Sep 19, 2012 8:56 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
cwedgwood
Joined: 16 Mar 2012 Posts: 12
|
|
Posted: Wed Sep 19, 2012 9:01 am |
|
|
OK
18F26K22
4.132 |
|
![](templates/subSilver/images/spacer.gif) |
jeremiah
Joined: 20 Jul 2010 Posts: 1328
|
|
Posted: Wed Sep 19, 2012 9:39 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Wed Sep 19, 2012 1:51 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
bkamen
Joined: 07 Jan 2004 Posts: 1611 Location: Central Illinois, USA
|
|
Posted: Thu Sep 20, 2012 2:33 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
cwedgwood
Joined: 16 Mar 2012 Posts: 12
|
|
Posted: Thu Sep 20, 2012 3:23 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Sep 20, 2012 3:33 am |
|
|
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.
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. |
|
![](templates/subSilver/images/spacer.gif) |
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Thu Sep 20, 2012 4:13 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
bkamen
Joined: 07 Jan 2004 Posts: 1611 Location: Central Illinois, USA
|
|
Posted: Thu Sep 20, 2012 12:50 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Sep 20, 2012 3:38 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|