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

Pointer to struct V4.127

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



Joined: 20 Dec 2005
Posts: 16

View user's profile Send private message Send e-mail

Pointer to struct V4.127
PostPosted: Tue Nov 22, 2011 5:56 am     Reply with quote

Hello,

To access the elements of my structure I used the pointer. It worked fine with Compiler Version 4.122 and older.

With new version V4.126 and V4.127 it doesn`t work.

I wrote simple program to demonstrate it. See variables b0 and b1 in Func().

Code:

#case
#include <18F26K22.h>

#FUSES INTRC                    //Internal RC Osc

#use delay(clock=16000000)

struct AA
{
   int8 x;           
   int8 y;
   int8 Buff[16];   // prijimaci buffer
};

struct AA Probe;

void Func(struct AA *pRx)
{
   int8 *ptr;
   int8 xx, yy, b0, b1;
   
   ptr = &pRx->x;          // pointer to Probe.x
   xx = *ptr;              // x = 5  works o.k.
   
   ptr = &pRx->y;          // pointer to Probe.y
   yy = *ptr;              // y = 6  works o.k.
   
   ptr = pRx->Buff+0;    // pointer to Probe.Buff[0]
   b0 = *ptr;            // b0 = 3   with V4.122
                         // b0 = 0   with V4.127 !!!!!!!!!!!!!!!
         
   ptr = pRx->Buff+1;   // pointer to Probe.Buff[1]
   b1 = *ptr;           // b1 = 4   with V4.122
                        // b1 = 0   with V4.127  !!!!!!!!!!!!!!
}

#zero_ram
void main()
{
   Probe.x = 5;
   Probe.y = 6;
   Probe.Buff[0] = 3;
   Probe.Buff[1] = 4;
   
   Func(&Probe);
     
   while (TRUE)
   {
   }
}




What is wrong ?

Thank`s

Peter
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 22, 2011 2:30 pm     Reply with quote

If I cast the pointers to int8* before using them, then it works. This is a
fairly common type of "fix" that has to be done with CCS, sometimes.

The program below has this output in MPLAB simulator:
Quote:

b0 = 03
b1 = 04


Code:

#include <18F26K22.h>
#fuses INTRC_IO
#use delay(clock=16M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

struct AA
{
int8 x;           
int8 y;
int8 Buff[16];   // prijimaci buffer
};

struct AA Probe;


void Func(struct AA *pRx)
{
int8 *ptr;
int8 b0, b1;
   
ptr = (int8 *)(pRx->Buff) +0;   // Cast pointer to int8* 
b0 = *ptr;             
printf("b0 = %x \r", b0);

         
ptr = (int8 *)(pRx->Buff) +1;   // Cast pointer to int8* 
b1 = *ptr;         
printf("b1 = %x \r", b1);
}

//===================================
void main()
{
   Probe.x = 5;
   Probe.y = 6;
   Probe.Buff[0] = 3;
   Probe.Buff[1] = 4;
   
   Func(&Probe);
     
   while (TRUE)
   {
   }
}
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Tue Nov 22, 2011 3:18 pm     Reply with quote

Hi

Please report it to CCS.

I have reported some other error on 4124->4127.

I think there came more and more funny errors every time there are a new release.
A major problem is to know what there are really changed, because all the new problem I not stated in the change log?
Sorry to say it:-(
Ttelmah



Joined: 11 Mar 2010
Posts: 19256

View user's profile Send private message

PostPosted: Tue Nov 22, 2011 3:51 pm     Reply with quote

This is why 'old users' of CCS, have a number of mantras:
1) Don't treat the releases as useable, till _you_ have tested them. CCS 'releases', are at best 'beta' code.
2) If writing a program, and you don't _need_ a fix that supposedly comes with a new 'release', stick with the older compiler.
3) When you have written code with a particular compiler version, save both the code, _and the compiler used_ together.

This is why people like PCM_Programmer, have access to dozens of older versions. I've got at least a dozen V3 compilers, and even some V2 ones, still stored, and the source code for my programs, always has at the top, just after the fuses etc., details of what compiler version, and include files were used to build it.

CCS, at various times, have claimed to be 'improving' their testing of new releases, but I've not seen any sign of it....

Learn not to think 'newer is better' with CCS...

Best Wishes
Pekub



Joined: 20 Dec 2005
Posts: 16

View user's profile Send private message Send e-mail

PostPosted: Wed Nov 23, 2011 8:34 am     Reply with quote

Thank`s PCM programmer.

The solution with "cast pointer" works right.

Code:

ptr = (int8 *)(pRx->Buff) +0;   // Cast pointer to int8*



I think this relates to the CCS note in readme.txt:

Users that have old code with expresions of the form:
*(&data + i)
need to change them to:
*((int8 *)(&data) + i)
A compiler change was made to be ANSI compliant.



In any case, this is very troublesome change. I need "casting" all my program
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Nov 23, 2011 10:09 am     Reply with quote

Quote:
I think this relates to the CCS note in readme.txt

This is a quite different problem I think. It's about how pointers are increased, in bytes or size of the entity pointed to.

The problem revealed in your code is a "simple" bug in calculating pointer addresses. You can look to the generated assembly code, it's just rubbish, nothing about ANSI or previous CCS interpretation of pointers. If you abstract from the involved data types, three of the four pointer dereference cases are just an offset that can be calculated at compile time. CCS C however presents three different forms of calculation, more long-winded than necessary and two of them faulty.

Because of this "many different ways", there's a fair chance to make the compiler using a variant with correct result, as apparently achieved with the casting operation. Unfortunately, there's no systematics behind it. My personal rule of thumb is: When meeting a CCS bug, try simple instruction instead of complex one's, don't mind some extra lines of code. CCS C wants to be spoon-feeded.
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