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

function with return value

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



Joined: 12 Aug 2009
Posts: 3

View user's profile Send private message

function with return value
PostPosted: Thu Jul 01, 2010 1:47 am     Reply with quote

Hi all,

I'm using CCS to compile for PIC16F688. I've met a strange problem with function with return value.

Originally I had this code, which didn't work:
Code:

int16 func_A()
{
    int16 my_local_var;
    my_local_var = 3000;
    return my_local_var;
}

int16 my_glob_array[8];

void func_B()
{
     int8 i;
     for (i=0; i<8; i++)
           my_glob_array[i] = func_A();
}

Not all element in my_glob_array are assigned to 3000, but 28900, -9160.
Isn't that strange?

Has anybody experienced the same thing? I'd very appreciate if someone could give a me a hint where I did wrong?
sjb



Joined: 13 Apr 2010
Posts: 34
Location: UK

View user's profile Send private message

PostPosted: Thu Jul 01, 2010 6:19 am     Reply with quote

How do you know the array value are wrong?

It does look odd from what you've said, but a simple typo could be the cause (eg. a ';' after the for(;;) statement). So it's often best to post a complete example if you can.

You suspect the return value. Why? (just a hunch, or something else?)
kaimi



Joined: 12 Aug 2009
Posts: 3

View user's profile Send private message

PostPosted: Thu Jul 01, 2010 7:22 am     Reply with quote

The values (my_glob_array, which are global) are asynchronously sent via serial when requested and I get them displayed in PC.

I think the problem is with the function return value because if I made this (only) change in my code then it's fine:
Code:

int16 my_global_var;

void func_A()
{
int16 my_local_var;
my_local_var = 3000;
my_global_var = my_local_var;
}

int16 my_glob_array[8];

void func_B()
{
int8 i;
for (i=0; i<8; i++)
{
     func_A();
     my_glob_array[i] = my_global_var;
}
}

That is I replaced the use of the return value of the func_A by the use of a global var. Of course the existence of func_A doesn't look to have much sense here, but in my real func_A there's lots code inside before the last assignment to my_local_var. But since the real code failed, I reduced everything for testing down to exactly the same like posted previously, and the output was as posted.

I have one suspicion:
Since my_glob_array is read inside an interrupt (serial RX) and the above code runs in the idle loop, is there possibility that when I have
my_glob_array[i] = func_A() and its execution is interrupted then my_glob_array[i] had some intermediate value instead of the expected?

I've tried disable/enable interrupt around my_glob_array[i] = func_A() too. But that also didn't help. Sad
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Thu Jul 01, 2010 8:13 am     Reply with quote

What compiler version?.
This is the sort of 'strangeness', that was common with the early 4.xxx releases....

Best Wishes
sjb



Joined: 13 Apr 2010
Posts: 34
Location: UK

View user's profile Send private message

PostPosted: Thu Jul 01, 2010 9:27 am     Reply with quote

Well, when does you interrupt routine start firing. If if fires before or while the func_B() then that could easily cause the problem.

A complete example would help.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Jul 01, 2010 9:53 am     Reply with quote

the interrupt can't be a problem because you reduced all your code down to what you have posted here!
Quote:
I reduced everything for testing down to exactly the same like posted previously

and as I see no interrupt it can't be that ;)

Also the code you posted won't run as there is no main routine or fuses etc.

Your problem could be a number of things but until you show me a complete program exhibiting the problem, I am out Smile

[rant]
So many times we have gone through the routine of asking questions to track down a problem based on very little info and without a complete piece of code only to find it was some stupid error somewhere else in the code which either the person with the error finally spotted or they actually, eventually posted something that someone on here could spot! [/rant]

Sorry Smile
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