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 CCS Technical Support

Memory And Variables

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



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

Memory And Variables
PostPosted: Wed May 18, 2011 7:18 am     Reply with quote

Hi,

Is there any way to section off an area of memory so I can declare two sets of variables in the same section? To explain further....I'm creating a device that has a standalone mode and a PC mode. The issue I'm having is finding a way to ONLY declare the PC-mode-related variables when in PC mode and the standalone-mode-related variables only when in standalone.

The functions they use are the same but obviously the interface is different so don't want my graphic LCD string variables utilising memory than can be reallocated to create a larger PC buffer.

I could use alloc() but wasn't sure if there was a better (CCS specific) way?

Regards,
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Wed May 18, 2011 8:34 am     Reply with quote

You do realise that variables declared 'locally' to a function, are _local_. If you declare variables inside a function, the memory space used by these can be re-used in any other function, and _will_ be re-used automatically. Only functions that are declared inside other functions, reserve memory space when they exit (or variables declared as static).
So:
Code:

#include <16F628.h>
#device *=16
#fuses INTRC
#use delay(clock=4MHz)

void function1(void) {
    char buffer[80];
    //do something with buffer
    int8 val;
    for (val=0;val<80;val++)
       buffer[val]=val;
}

void function2(void) {
    float calcval[20];
    //do something with calcval
    int8 val;
      for (val=0;val<20;val++)
         calcval[val]=val*3.14;
}

void main(void) {
    function1();
    function2();
   
    do {
       delay_us(1);
    }while(TRUE);
}
}

If you compile this, and look at the symbol table, you see:
Code:

004-005 @READ_PROGRAM_MEMORY8.P1
015     CCP_1_LOW
015     CCP_1
016     CCP_1_HIGH
01F.6   C1OUT
01F.7   C2OUT
021-070 function1.buffer
021-070 function2.calcval
071     function2.val
071     function1.val
072     function1.@SCRATCH1
072     function2.@SCRATCH1
073     function1.@SCRATCH2
073     function2.@SCRATCH2

with function1.buffer, and function2.calcval, _both using the same memory_.

This is not CCS specific, but general to the concept of 'local' variables. It is done exactly to allow multiple routines that are never called inside each other, and require lots of local storage, to re-use memory space.

Best Wishes
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Wed May 18, 2011 9:06 am     Reply with quote

Yes sorry I perhaps should have mentioned that. The issue is though, is that I need to have global variables since their contents is changed by multiple functions and interrupt handlers.
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Wed May 18, 2011 9:28 am     Reply with quote

Just use #byte, or a union.
Code:

int8 buffer[128];

struct somedata {
    int16 silly[10];
    int32 another[26];
} storage;

#byte storage=buffer //Locates 'storage' to the same location as buffer.

union {
   buffer[128];
   struct somedata fred;
} shared;


shared.buffer, is then a 128byte buffer, and shared.fred is a structure accessing the same memory area.

Best Wishes
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Wed May 18, 2011 9:31 am     Reply with quote

Excellent thanks Ttelmah, I'll give it ago.
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