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

Nested structures problem

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



Joined: 20 Sep 2003
Posts: 54

View user's profile Send private message

Nested structures problem
PostPosted: Mon Aug 16, 2004 7:04 am     Reply with quote

I've found a serious bug in an access to nested structures
in PCM 3.207. This error is present there since 3.190 AFAIK.
I've already sent a bug report to CCS.
See the listing file of my simple test program and you will know what I mean:
Code:
CCS PCM C Compiler, Version 3.207, 22683               16-VIII-04 14:41

               Filename: D:\SOURCE\pokusy\test4.LST

               ROM used: 57 words (1%)
                         Largest free fragment is 2048
               RAM used: 29 (8%) at main() level
                         29 (8%) worst case
               Stack:    0 locations

*
0000:  MOVLW  00
0001:  MOVWF  0A
0002:  GOTO   004
0003:  NOP
....................  #opt 0 
.................... #include <16F877.h>
....................  //////// Standard Header file for the PIC16F877 device //////////////// 
.................... #device PIC16F877 
.................... #list 
.................... 
.................... #device *=16 //use 16 bit pointers 
....................   
.................... struct outer { 
....................    int8 o1; 
....................    int8 o2; 
....................    int16 o3; 
....................    struct inner { 
....................       int8 i1; 
....................       int8 i2; 
....................       int16 i3; 
....................    } x,y; 
.................... } a,b; 
....................   
.................... void load_inner(struct inner &ptr) { 
....................    ptr.i1=1; //pointer is correct 
*
000E:  MOVLW  01
000F:  MOVWF  24
*
0015:  MOVLW  01
0016:  MOVWF  28
....................    ptr.i2=2; //!!! WRONG pointer !!! 
*
0010:  MOVLW  02
0011:  MOVWF  21
*
0017:  MOVLW  02
0018:  MOVWF  21
....................    ptr.i3=3; //!!! WRONG pointer !!! 
*
0012:  CLRF   23
0013:  MOVLW  03
0014:  MOVWF  22
*
0019:  CLRF   23
001A:  MOVLW  03
001B:  MOVWF  22
.................... } 
....................   
.................... void load_outer(struct outer &ptr) { 
....................    ptr.o1=1; //pointer is correct 
001C:  MOVLW  01
001D:  MOVWF  20
*
002A:  MOVLW  01
002B:  MOVWF  2C
....................    ptr.o2=2; //pointer is correct 
*
001E:  MOVLW  02
001F:  MOVWF  21
*
002C:  MOVLW  02
002D:  MOVWF  2D
....................    ptr.o3=3; //pointer is correct 
*
0020:  CLRF   23
0021:  MOVLW  03
0022:  MOVWF  22
*
002E:  CLRF   2F
002F:  MOVLW  03
0030:  MOVWF  2E
....................    ptr.inner.i1=1; //!!! WRONG pointer !!! 
*
0023:  MOVLW  01
0024:  MOVWF  1F
*
0031:  MOVLW  01
0032:  MOVWF  2B
....................    ptr.inner.i2=2; //!!! WRONG pointer !!! 
*
0025:  MOVLW  02
0026:  MOVWF  20
*
0033:  MOVLW  02
0034:  MOVWF  2C
....................    ptr.inner.i3=3; //!!! WRONG pointer !!! 
*
0027:  CLRF   22
0028:  MOVLW  03
0029:  MOVWF  21
*
0035:  CLRF   2E
0036:  MOVLW  03
0037:  MOVWF  2D
.................... } 
....................   
.................... void main(void) { 
....................    load_inner(a.x); 
*
0004:  CLRF   04
0005:  BCF    03.7
0006:  MOVLW  1F
0007:  ANDWF  03,F
0008:  BSF    03.5
0009:  BSF    1F.0
000A:  BSF    1F.1
000B:  BSF    1F.2
000C:  BCF    1F.3
000D:  BCF    03.5
....................    load_inner(a.y); 
....................    load_outer(a); 
....................    load_outer(b); 
.................... } 
.................... 
0038:  SLEEP
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Mon Aug 16, 2004 8:54 am     Reply with quote

I am not sure if this line is valid
Code:

ptr.inner.i1=1;


If you are passing the addresses, I would use pointers instead. Like this:

Code:

#include <16F877.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)


struct outer

  int8 o1; 
  int8 o2; 
  int16 o3; 
  struct inner
  { 
    int8 i1; 
    int8 i2; 
    int16 i3; 
  }x,y; 
} a,b; 

void load_inner(struct inner *ptr)

  ptr->i1=1;
  ptr->i2=2;
  ptr->i3=3;


void load_outer(struct outer *ptr)

  ptr->o1=1;
  ptr->o2=2;
  ptr->o3=3;
  ptr->x.i1=4;
  ptr->x.i2=5;
  ptr->x.i3=6;


void main(void)

  load_inner(&a.x); 
  load_inner(&a.y); 
  load_outer(&a); 
  load_outer(&b); 
}


You can also use the #separate which apears to work for the way you are doing it.
Code:

#include <16F877.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)


struct outer

  int8 o1; 
  int8 o2; 
  int16 o3; 
  struct inner
  { 
    int8 i1; 
    int8 i2; 
    int16 i3; 
  }x,y; 
} a,b; 

#separate
void load_inner(struct inner &ptr)

  ptr.i1=1;
  ptr.i2=2;
  ptr.i3=3;


#separate
void load_outer(struct outer &ptr)

  ptr.o1=1;
  ptr.o2=2;
  ptr.o3=3;
  ptr.x.i1=4;
  ptr.x.i2=5;
  ptr.x.i3=6;
  ptr.y.i1=7;
  ptr.y.i2=8;
  ptr.y.i3=9;


void main(void)

  load_inner(a.x); 
  load_inner(a.y); 
  load_outer(a); 
  load_outer(b); 
}
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