Memory Problem in PIC18F46K22

takao21203

Joined Apr 28, 2012
3,702
Bob, I find that interesting.

XC8 does take it into account. I tried it.

If x is a pointer, y*(y) will do a typecast, even when it will complain it is illegal.

When x is an integer, it does a multiplication.

x will contain the int value of y as address, I stepped through it, no multiplication taking place.

Maybe it is question of which standard the compiler supports. How it behaves must be defined.

Anyway, (unsigned char*)y looks better to me.

So you could also use (y*)y probably?

C sources can be very confusing when the way of doing these things is mixed.
 

BobTPH

Joined Jun 5, 2013
8,813
if XC8 is doing that, it is a bug.

Can you produce the source code and disassembly for each case, i.e. the same expression with x being and int and x being and int *?

Bob
 

takao21203

Joined Apr 28, 2012
3,702
Eventually I did not understand it right.

What I wrote in above replies was wrong, it does a multiplication.

The
Rich (BB code):
while((SHOWDispWrite++)<13)
fooled me.

Here is the disassembly:

Rich (BB code):
!void main(void)
!{unsigned char SHOWDispWhat=0;
0x1B4: CLRF SHOWDispWhat
!unsigned char* x;
!ConfigureOscillator();
0x1B5: CALL 0x7
!InitApp();
0x1B6: CALL 0x6
!    while(1)
0x1D7: GOTO 0x1B7
!    {
!	while((SHOWDispWhat++)<13)
0x1B7: MOVLW 0x1
0x1B8: MOVWF 0x1F
0x1B9: MOVF 0x1F, W
0x1BA: ADDWF SHOWDispWhat, F
0x1BB: MOVLW 0xE
0x1BC: SUBWF SHOWDispWhat, W
0x1BD: BTFSC STATUS, 0x0
0x1BE: GOTO 0x1D5
0x1D4: GOTO 0x1B7
!	{
!	    x=(SHOWDispWhat*(SHOWDispWhat));
0x1BF: MOVF SHOWDispWhat, W
0x1C0: MOVWF 0x1C
0x1C1: CLRF 0x1D
0x1C2: MOVF 0x1C, W
0x1C3: MOVWF __pcstackBANK0
0x1C4: MOVF 0x1D, W
0x1C5: MOVWF 0x8
0x1C6: MOVF SHOWDispWhat, W
0x1C7: MOVWF 0x1C
0x1C8: CLRF 0x1D
0x1C9: MOVF 0x1C, W
0x1CA: MOVWF multiplicand
0x1CB: MOVF 0x1D, W
0x1CC: MOVWF 0xA
0x1CD: CALL 0x5
0x1CE: MOVF __pcstackBANK0, W
0x1CF: MOVWF 0x1C
0x1D0: MOVF 0x8, W
0x1D1: MOVWF 0x1D
0x1D2: MOVF 0x1C, W
0x1D3: MOVWF x
!	
!	}
!	SHOWDispWhat=1;
0x1D5: CLRF SHOWDispWhat
0x1D6: INCF SHOWDispWhat, F
No I cant see typecasting happening, so I was fooled, and wrote nonsense, feeling so sorry now.
 

Thread Starter

Nebulas_PSP

Joined Aug 22, 2014
5
Hello Guys,

Thank You so much for you wonderful responses and i appreciate too, it helped me a lot to optimize my code :D.

I feel The problem is not yet solved. Right now, when i separated each set of code with similar functionality in different file and included as .h file in the main program. And shifted all the variables at specific location in the EEPROM with some additional space after each variable(to avoid overflow).

When i did the same, i did not face the problem as i stated earlier. :)

I will be updating you the results as soon as i get it :)
 

ErnieM

Joined Apr 24, 2011
8,377
And shifted all the variables at specific location in the EEPROM with some additional space after each variable(to avoid overflow).
Oh bother... you should never have to do that. You never want to do that.

Your time would be better spent searching the code for the pointer that is pointing where is should not.
 

NorthGuy

Joined Jun 28, 2014
611
I feel The problem is not yet solved. Right now, when i separated each set of code with similar functionality in different file and included as .h file in the main program. And shifted all the variables at specific location in the EEPROM with some additional space after each variable(to avoid overflow).
I'm afraid the memory may still leak through the gaps that you left between varibales. To prevent the leaks, you may need to use silicon sealant (don't confuse with silicone sealant!)
 
Top