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

pointers to const struct.

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



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

pointers to const struct.
PostPosted: Fri Oct 01, 2010 1:07 am     Reply with quote

Hi

I have set PASS_STRINGS=IN_RAM! Because I use _const_

The small program is only _test_
In MPLAB this is not working.
How to get the size in the function T1?
Code:

typedef struct {
 char s1[5];
 char s2[5];
 char s3[5];
}_pumps;

enum _ptype{spump,mpump,fpump};
const _pumps pumps[2]={
1,2,3,4,5,
5,4,3,2,1,
1,2,3,4,5,

11,22,33,44,55,
55,44,33,22,11,
99,88,77,66,55
};

void Dopump(char v){;}

void T1(char *pump){
 char i,s,v;

 //Not working but how to get the size then?
 s=sizeof(pump);
 
 for (i=0; i<5; i++){
  v=pump[i];
  DoPump(v);
 }
}

void main() {
 T1(&pumps[spump].s1);
 T1(&pumps[mpump].s2);
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Oct 01, 2010 5:13 am     Reply with quote

First comment: never start variables with a '_' character! The underscore character is reserved for use by compiler internal variables. You might run into very strange errors if by accident you choose the same name as used by the compiler.

Second comment is to use more descriptive variable names. I have no clue as to what is the difference between _pumps, pumps, pump, spump, mpump and fpump?
A good coding standard is to use capital names for constants; enums are constants too, so it becomes
Code:
enum _ptype{SPUMP,MPUMP,FPUMP};


Code:
 //Not working but how to get the size then?
 s=sizeof(pump);
Pump is a pointer to characters. Size of a pointer is 2 bytes, so that's the result you get.

Which variable do you want to determine the size of?
In your code all arrays are 'const' and all are the same size. So if the size is not going to change, why determine it dynamically?
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Fri Oct 01, 2010 8:36 am     Reply with quote

Thanks for your suggestion. Maybe my "style" is bad?

I have cut it down to this:

Code:
typedef struct{
 int8 s1[5];
 int8 s2[5];
 int8 s3[5];
}pump_;

const pump_ pump[2]={
                     1,2,3,4,5,  //p1 s1
                     1,2,3,4,5,
                     1,2,3,4,5,  //p1 s3

                     1,2,3,4,5,  //p2 s1
                     1,2,3,4,5,
                     1,2,3,4,5}; //p2 s3


void ppout(int8 *ptr,int8 len){
 int i,p;
 printf("%u\n",len);
 for (i=0; i<len; i++){
  p=ptr[i];
  printf("%u,",ptr[i]);
 }
 printf("\n");
}


void main(){
 ppout(pump[0].s1,sizeof(pump[0].s1));
}


I use MPLAB as debugger!
Code:
sizeof(pump[0].s1)

The above sizeof return 30 this is _wrong_, must be 5. 30 is the hole struct array!

What is wrong here?
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

Is sizeof broken?
PostPosted: Sat Oct 02, 2010 1:09 am     Reply with quote

Is sizeof broken?

When running the above ex. in a normally "c" compiler the result is 5 as expected.

sizeof(pump[0].s1); must return 5, but return 30 in ccs.

I try to make a simple const array and on this the sizeof is working well.


**EDIT** The problem is _only_ on const struct! If removing const all is working, now I think it is an error in sizeof.
**EDIT**

Comment?
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Sat Oct 02, 2010 12:08 pm     Reply with quote

Hi

I have made lot of test, and looking in the forum, back to 2005 I can see other people having the same problem, I dont understand it is still the same?

I have now compiled with different chip in 16F & 18F all result is the same.

Test this out if you have time to give it a try. Then look in the list file.
If removing the "const" all is working!

Code:
struct {
 int8 s1;
 int8 s2[5];
 int8 s3;
 }const stTest1={0, 1,2,3,4,5 ,6};

void main(){
 s=sizeof(stTest1.s1);
 s=sizeof(stTest1.s2);
 s=sizeof(stTest1.s3);
}


Result without "const"
Code:
....................  s=sizeof(stTest1.s1);
0032:  MOVLW  01
0034:  MOVWF  s
....................  s=sizeof(stTest1.s2);
0036:  MOVLW  05
0038:  MOVWF  s
....................  s=sizeof(stTest1.s3);
003A:  MOVLW  01
003C:  MOVWF  s


And this with const
Code:
0018:  MOVLW  01
001A:  MOVWF  s
....................  s=sizeof(stTest1.s2);
001C:  MOVWF  s
....................  s=sizeof(stTest1.s3);
001E:  MOVWF  s



Is it me or the compiler?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 02, 2010 12:23 pm     Reply with quote

If you don't find a work-around for the problem in the forum archives,
then email CCS Support a short (but compilable) example.

This is what I mean by a short example. In fact, it could be shorter.
You don't need your ppout() routine to show the problem. Just a simple
printf of the sizeof() result is enough. You can run this in MPLAB simulator
and study the problem.
Code:

#include <18F452.H>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

typedef struct{
 int8 s1[5];
 int8 s2[5];
 int8 s3[5];
}pump_;


const pump_ pump[2]={
1,2,3,4,5,  //p1 s1
1,2,3,4,5,
1,2,3,4,5,  //p1 s3

1,2,3,4,5,  //p2 s1
1,2,3,4,5,
1,2,3,4,5}; //p2 s3

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

result = sizeof(pump[0].s1);

printf("%x \r", result);

while(1);
}

I worked on your problem briefly. I tried to fix it by type-casting the
structure reference in the sizeof statement to _pump, and that changed it
to 0F (from 1e) so maybe it helped, but it's not the solution. For all I
know, there might be a solution in the forum archives, maybe even by
me, but I don't have time to search for it. If you own the compiler then
you can email CCS support about it. Maybe they might help, or maybe
fix it in the next version.
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Sat Oct 02, 2010 12:50 pm     Reply with quote

Thanks for testing it out, then it's not just me. I will let it go for now and just use the exact array count.
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