# PIC XC8 compiler data space question

#### bug13

Joined Feb 13, 2012
1,995
Hi guys

I think I am about to run out of data space in my pic, so how reliable of the data space info generated by the mplab X when a project is compiled?

eg, the data space info below:
Memory Summary:
Program space used B221h ( 45601) of FFF8h bytes ( 69.6%)
Data space used D33h ( 3379) of EC0h bytes ( 89.5%)
Configuration bits used 4h ( 4) of 4h words (100.0%)
Data stack space used 0h ( 0) of 14Eh bytes ( 0.0%)
Thanks a lot!

#### spinnaker

Joined Oct 29, 2009
7,835
You should be able to compute it yourself by adding up all of the space in your variables. Data sizes are listed in the compiler manual. Here is the one for integers

But I would say the memory summary is accurate.

This really should be in the embedded forum.

#### JohnInTX

Joined Jun 26, 2012
4,558
Moved to Embedded Systems and Microcontrollers.

The summary is usually pretty close and includes the memory that the compiler assigns for run time variables (software stack etc).

If you start running low, review your code to see if you can put constants in ROM, declare temp variables inside functions etc. so that the compiler knows it can reuse them.

XC8 is pretty good at packing in the bytes. Maybe you need a bigger PIC. Which one are you using?

Last edited:

#### bug13

Joined Feb 13, 2012
1,995
XC8 is pretty good at packing in the bytes. Maybe you need a bigger PIC. Which one are you using?
Good to know that the compiler is pretty good.

I am using pic18f46j50, 64K rom, 3.7K ram. unfortunately, my codes are almost done, and I don't really want to change if I don't have to.

#### spinnaker

Joined Oct 29, 2009
7,835
I would take a look at your code. See if you can make use of the stack rather than declaring everything global there may be some variables you can declare inside functions.

#### bug13

Joined Feb 13, 2012
1,995
I would take a look at your code. See if you can make use of the stack rather than declaring everything global there may be some variables you can declare inside functions.
What do you mean by making use of the stack? Would you explain a bit more?

#### spinnaker

Joined Oct 29, 2009
7,835
Say for example you have a variable.

Code:
int x;

void foo()
{

x =1

}
In this case x will remain in memory the life of your program. It is fine if x is going to be used my a lot of functions.

But say it is only used by foo. Then you could do this.

Code:
void foo()
{
int x = 1;

}
In this case the space x takes up will be freed when foo exits. It uses the stack.

#### bug13

Joined Feb 13, 2012
1,995
Say for example you have a variable.

Code:
int x;

void foo()
{

x =1

}
In this case x will remain in memory the life of your program. It is fine if x is going to be used my a lot of functions.

...
Got it, thanks for explaining!!

#### spinnaker

Joined Oct 29, 2009
7,835
Look at section titled How Can I Reduce RAM Usage? in the XC8 manual for other tips. In mine it is section 3.6.3.

#### spinnaker

Joined Oct 29, 2009
7,835
Another little trick you can use to save a bit of memory.

Code:
Say you have 8 flags

unsigned char flag1;
unsigned char flag2;
unsigned char flag3;
unsigned char flag4;
unsigned char flag5;
unsigned char flag6;
unsigned char flag7;
unsigned char flag8;

This takes 8 bytes of memory when it really just needs to take 8.

You can use the bit operator.

struct flagstype
{
unsigned char flag1 : 1;
unsigned char flag2 : 1;
unsigned char flag3 : 1;
unsigned char flag4 : 1;
unsigned char flag5 : 1;
unsigned char flag6 : 1;
unsigned char flag7 : 1;
unsigned char flag8 : 1;

};

This only takes on byte.

If you have a 2 counters that only need to go to 15  you can do this

struct countertype
{
unsigned char counter1 : 4;
unsigned char counter2 : 4;

};

Last edited:

#### MMcLaren

Joined Feb 14, 2010
853
Wouldn't a four bit counter top out at 15?

#### spinnaker

Joined Oct 29, 2009
7,835
Duh! Not thinking yeah 15. Too long a day today.