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 support@ccsinfo.com

pass-by-pointer not changing values

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



Joined: 12 Feb 2016
Posts: 11

View user's profile Send private message

pass-by-pointer not changing values
PostPosted: Fri Nov 11, 2016 1:56 am     Reply with quote

I was writing a code for circular buffer to be used for handling incoming data packets on hardware UART on PIC18F25K80. The actual buffer seems to work perfectly, except buffer overflows randomly. After debugging, I found that the FIFO_Init() function does not initialise the variables to zero, but rather arbitary values like 253 etc.

Edit* I'm using CCS v5.064

Code:

#define UINT8 unsigned int8
#define FIFO_SIZE 128

typedef struct
{
   UINT8 Start;
   UINT8 End;
   volatile UINT8 NbBytes;   /* number of bytes stored in the FIFO, this count is increased by INT_RDA interrupt */
   UINT8 Buffer[FIFO_SIZE];
} TFIFO;

TFIFO RxFIFO;

void FIFO_Init(TFIFO *FIFO)
{
   FIFO->Start   = 0;
   FIFO->End     = 0;
   FIFO->NbBytes = 0;
   /* If I printf("%u", FIFO->NbBytes), it outputs '0', which is correct but */
}

void main()
{
   FIFO_Init(&RxFIFO);
   printf("%u", RxFIFO->NbBytes);
   /* if I print here, the value of NbBytes is non-zero value */
}


I would've thought passing a pointer to a function using '&' operator and using '*' to update the variable would work. But apparently, the variables aren't getting initialised.

Any ideas?

Thank you
Eric
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Fri Nov 11, 2016 2:10 am     Reply with quote

In your main, you need to be printing RxFIFO.NbBytes. At this point RxFIFO is not a pointer.....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 11, 2016 2:24 am     Reply with quote

Here is a test program to explore this.
Code:
#include <18F25K80.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

#define UINT8 unsigned int8
#define FIFO_SIZE 128

typedef struct
{
   UINT8 Start;
   UINT8 End;
   volatile UINT8 NbBytes;   
   UINT8 Buffer[FIFO_SIZE];
} TFIFO;

TFIFO RxFIFO;

void FIFO_Init(TFIFO *FIFO)
{
   FIFO->Start   = 0x01;
   FIFO->End     = 0x02;
   FIFO->NbBytes = 0x03;
}


//======================================
void main()
{
int8 temp;

FIFO_Init(&RxFIFO);

temp = RxFIFO->NbBytes;  // Your method
printf("RxFIFO->NbBytes = %x \r", temp);

// Two better methods:

temp = &RxFIFO->NbBytes;
printf("&RxFIFO->NbBytes = %x \r", temp);

temp = RxFIFO.NbBytes;
printf("RxFIFO.NbBytes = %x \r", temp);

while(TRUE);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Fri Nov 11, 2016 3:48 am     Reply with quote

and (of course) going on from here, the actual problem in your original code is almost certainly that at some point you are getting this selection wrong, so something then talks to the wrong variable....
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