View previous topic :: View next topic |
Author |
Message |
Gavin Pinto
Joined: 18 Oct 2009 Posts: 27 Location: Australia
|
|
Posted: Tue Feb 09, 2010 12:47 pm |
|
|
Wayne_ wrote: | Well, my compiler works differently to yours.
Sizeof(strings) = 8; // This is a pointer array. Each index contains a pointer to the string which is sizeof(char *) 2 bytes. so strings is 8 bytes in size.
sizeof(char *) = 2;
From the lst file
Code: |
.................... a = sizeof(strings);
*
00AB: MOVLW 08
00AC: MOVWF 27
.................... b = sizeof(char *);
00AD: MOVLW 02
00AE: MOVWF 28
.................... i = sizeof(strings) / sizeof(char *);
00AF: MOVLW 04
00B0: MOVWF 29
|
The fomula
i = sizeof(strings) / sizeof(char *); returns 4 which is the number of null terminated strings in the pointer array strings. Which is correct.
Show me your .lst code where
"sizeof(strings) evaluates to 4"
Or did you work it out some other way ? |
Here is my listing
Code: |
.................... rom char* ptr;
....................
.................... rom char *strings[] =
.................... {
.................... "HELLO",
.................... "WORLD",
.................... "CONST",
.................... "STRINGS"
.................... };
0EB62: MOVLW 48
0EB64: MOVWF xDA
0EB66: MOVLW 45
0EB68: MOVWF xDB
0EB6A: MOVLW 4C
0EB6C: MOVWF xDC
0EB6E: MOVWF xDD
0EB70: MOVLW 4F
0EB72: MOVWF xDE
0EB74: CLRF xDF
0EB76: MOVLW 57
0EB78: MOVWF xE0
0EB7A: MOVLW 4F
0EB7C: MOVWF xE1
0EB7E: MOVLW 52
0EB80: MOVWF xE2
0EB82: MOVLW 4C
0EB84: MOVWF xE3
0EB86: MOVLW 44
0EB88: MOVWF xE4
0EB8A: CLRF xE5
0EB8C: MOVLW 43
0EB8E: MOVWF xE6
0EB90: MOVLW 4F
0EB92: MOVWF xE7
0EB94: MOVLW 4E
0EB96: MOVWF xE8
0EB98: MOVLW 53
0EB9A: MOVWF xE9
0EB9C: MOVLW 54
0EB9E: MOVWF xEA
0EBA0: CLRF xEB
0EBA2: MOVLW 53
0EBA4: MOVWF xEC
0EBA6: MOVLW 54
0EBA8: MOVWF xED
0EBAA: MOVLW 52
0EBAC: MOVWF xEE
0EBAE: MOVLW 49
0EBB0: MOVWF xEF
0EBB2: MOVLW 4E
0EBB4: MOVWF xF0
0EBB6: MOVLW 47
0EBB8: MOVWF xF1
0EBBA: MOVLW 53
0EBBC: MOVWF xF2
0EBBE: CLRF xF3
0EBC0: MOVLW 08
0EBC2: MOVWF xD3
0EBC4: MOVLW DA
0EBC6: MOVWF xD2
0EBC8: MOVLW 08
0EBCA: MOVWF xD5
0EBCC: MOVLW E0
0EBCE: MOVWF xD4
0EBD0: MOVLW 08
0EBD2: MOVWF xD7
0EBD4: MOVLW E6
0EBD6: MOVWF xD6
0EBD8: MOVLW 08
0EBDA: MOVWF xD9
0EBDC: MOVLW EC
0EBDE: MOVWF xD8
....................
.................... a= sizeof(strings);
0EBE0: MOVLW 04
0EBE2: MOVWF xC3
.................... b=sizeof(ptr);
0EBE4: MOVWF xC4
.................... c=sizeof(strings)/sizeof(ptr);
0EBE6: MOVLW 01
0EBE8: MOVWF xC5
.................... |
_________________ Clear Logic |
|
|
Gavin Pinto
Joined: 18 Oct 2009 Posts: 27 Location: Australia
|
|
Posted: Wed Feb 10, 2010 4:52 pm |
|
|
Wayne_ wrote: | Well, my compiler works differently to yours.
Or did you work it out some other way ? |
So whose compiler is doing the right thing? _________________ Clear Logic |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Feb 11, 2010 3:32 am |
|
|
.................... ROM char *strings[] =
.................... {
.................... "HELLO",
Code: |
.................... "WORLD",
.................... "CONST",
.................... "STRINGS"
.................... };
.................... ROM char *ptr;
.................... ROM char *ptr2[1];
.................... const char *ptr3 = 0;
.................... char *ptr4;
....................
.................... void main()
.................... {
*
0099: CLRF 04
009A: BCF 03.7
009B: MOVLW 1F
009C: ANDWF 03,F
009D: BSF 03.5
009E: BSF 1F.0
009F: BSF 1F.1
00A0: BSF 1F.2
00A1: BCF 1F.3
00A2: MOVLW 07
00A3: MOVWF 1C
.................... //char k;
.................... int oldportc;
.................... int newportc;
.................... int a, b, c, d, e, f, i;
....................
.................... a = sizeof(strings);
*
00AB: MOVLW 08
00AC: MOVWF 29
.................... b = sizeof(char *);
00AD: MOVLW 02
00AE: MOVWF 2A
.................... c = sizeof(ptr); // ROM char *ptr;
00AF: MOVLW 01
00B0: MOVWF 2B
.................... d = sizeof(ptr2); // ROM char *ptr2[1];
00B1: MOVLW 02
00B2: MOVWF 2C
.................... e = sizeof(ptr3); // const char *ptr3;
00B3: MOVWF 2D
.................... f = sizeof(ptr4); // char *ptr4;
00B4: MOVWF 2E
.................... i = sizeof(strings) / sizeof(char *);
00B5: MOVLW 04
00B6: MOVWF 2F
|
The problem or feature appears to be related to a ROM char * pointer.
If you define a single pointer ROM char *ptr; then it has a size of 1, if you define an array of pointers ROM char *ptr[1]; or ROM char *ptr = {"fred"}; and it has a size of at least 1 then the size of ptr is 2.
So I have no idea why ? But the example doesn't refer to the size of the ptr.
As the routine requires the number of strings in the array then the formula in my case needs to return a value of 4.
In your case
sizeof(strings) = 4;
sizeof(ptr) = 4;
So the result would be wrong.
I would do it differently and report a problem to CCS. |
|
|
Gavin Pinto
Joined: 18 Oct 2009 Posts: 27 Location: Australia
|
|
Posted: Mon Feb 15, 2010 10:08 pm |
|
|
Wayne_ wrote: | ....
I would do it differently and report a problem to CCS. |
well it should report 16/4 =4. Dont you think so?
sizeof(strings) = 16;
sizeof(rom char*) = 4;
My device is PIC18F8722 so the size of pointer to ROM is 4 bytes _________________ Clear Logic |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Feb 16, 2010 2:53 am |
|
|
Gavin Pinto wrote: |
well it should report 16/4 =4. Dont you think so?
sizeof(strings) = 16;
sizeof(rom char*) = 4;
My device is PIC18F8722 so the size of pointer to ROM is 4 bytes |
No, strings is an array of pointers containing 4 entries so if a char * pointer has a size of 2 the size of strings should be 8, the result should be 4 to match the number of entries.
edit - I just noticed your last statement. Why do you think a rom pointer for a PIC18F8722 is 4 bytes ? |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Feb 16, 2010 3:05 am |
|
|
I have to admit, I didn't think about the device type while I was testing and have it set to 16f877a. I just tried changing it to 18f8722 but it doesn't compile. |
|
|
Gavin Pinto
Joined: 18 Oct 2009 Posts: 27 Location: Australia
|
|
Posted: Tue Feb 16, 2010 12:06 pm |
|
|
Wayne_ wrote: |
edit - I just noticed your last statement. Why do you think a rom pointer for a PIC18F8722 is 4 bytes ? |
I do not think it is 4 bytes. I know it is 4 bytes because the program space is 128K bytes.
The pointer size will be int32 because C does not have int24.
I prefer to say 'pointer to ROM' rather than ROM pointer. _________________ Clear Logic |
|
|
|