local allocation of array and external reference to it

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

local allocation of array and external reference to it

5,055 Views
stevec
Contributor III
I'm not too experienced at C yet and have a question on (possibly) the use of pointers and arrays. I wish to allocate a temporary area of memory in a routine to hold 256 bytes of data. I then wish to access this block of memory in another routine called from this routine. I cant quite get my head around what is required to do this. Could someone help me out here, please?

I'm using an 8 bit uP (9S08RE16) and want to use the RAM space efficiently so want to have this 256 bytes available for use later in the program.

Steve
Labels (1)
0 Kudos
Reply
16 Replies

1,179 Views
CrasyCat
Specialist III
Hello
 
First you need to define a stack which is big enough to contain your 256 bytes array.
In that purpose just open the .prm from your application and increase stack size.
define it as 0x130 for example
 
Then define the array in the calling function and pass the symbol to the called function. Make sure to have a prototype for the called function prior to implementation of the calling function.
See example below:
 
void foo2(char*param);
 
void foo(void) {
  char tab[256];
 
  /*Insert code here */
  foo2(tab);
 
}
 
Memory area for the array can then be used for further parameters and local variables.
 
That should be it
 
CrasyCat
0 Kudos
Reply

1,179 Views
stevec
Contributor III
Crasy,

I've increased STACKSIZE from0x50 to 0x150 to allow for my 256 bytes of buffer and now the compiler throws up a link error "L1102:smileysurprised:ut of allocation space in segment Z_RAM at address 0x41".

    Z_RAM                    =  READ_WRITE   0x0040 TO 0x00FF;
    _DATA_ZEROPAGE, MY_ZEROPAGE, DEFAULT_RAM         INTO  Z_RAM;

These are the only references to Z_RAM in the .prm file.

Putting STACKSIZE back to 0x50 makes the error disappear but of course the code doesnt work!!!

I'm sure there's a simple explaination (apart from my own ignorance of course).

Steve

0 Kudos
Reply

1,179 Views
stevec
Contributor III
Is this because the stack by default is in Z_RAM and I need to move it to another area? If so how do I define where the stack goes? The Help files allude to this but their examples are confusing.

steve
0 Kudos
Reply

1,179 Views
CrasyCat
Specialist III
Hello
 
If you create your project for a HCS08RE16 MCU from the wizard, there is a memory area called RAM, which is outside of the zeropage.
 
default PRM file specifies:
 
PLACEMENT 
    DEFAULT_RAM                         INTO  RAM;
    ...
 
So with this configuration the stack will go into RAM area (where DEFAULT_RAM is located.
If you wish to keep DEFAULT_RAM into zeropage and only the stack outside of zeropage,
just write:
 
PLACEMENT 
    SSTACK                                 INTO  RAM;
   _DATA_ZEROPAGE, MY_ZEROPAGE, DEFAULT_RAM         INTO  Z_RAM;
  ...
 
CrasyCat
0 Kudos
Reply

1,179 Views
mccPaul
Contributor I
Hi
 
Why don't you try an alternative approach that doesn't use the stack? If you declare the array statically, the linker will create space for it the BSS section and the array will be around for the life of the program. Or you can use malloc() to create the array on the heap if you want to free it. You can leave your stack as it is.
 
Cheers,
 
Paul.
0 Kudos
Reply

1,179 Views
stevec
Contributor III
Thanks for the suggestions.

mccp, I am trying your suggestion first. I have declared an array of 256 bytes. I have assigned my DEFAULT_RAM  INTO RAM in the placement area of the .prm file. But now I'm getting another error "Fixup overflow in wait, to timeout type 1 at offset 0x6"   and also "........ at offset 0xD.   "wait...




0 Kudos
Reply

1,179 Views
CrasyCat
Specialist III
Hello
 
  How is variable timeout defined? Is it located in a user default segment or in DEFAULT_RAM?
  Which memory model are you using (or what is your current compiler command line)?
 
CrasyCat
0 Kudos
Reply

1,179 Views
stevec
Contributor III
'timeout' is define in the calling files header file as an unsigned char and is referenced as an extern in the called function. I believe I am using the 'tiny' memory model which is probably causing no end of problems (now you mention it). How do I change the memory model within CW to 'small'. Also how can I identify a particular variable as being used in zero page (for speed critical stuff)?

Steve
0 Kudos
Reply

1,179 Views
stevec
Contributor III
Ok, found how to change the memory model. Now get error L1401: incompatible memory model:smileyfrustrated:MALL in previous file (main.c.o) and TINY in ASSERT.C.o (ansitis.lib). I get 17 of these referencing the TINY model in various ansitis.lib files.
Assuming that this is memory size related (and I'm assuming it is) am I best just starting a new project with the right definitions and importing the files I've writen so far?

Steve

0 Kudos
Reply

1,179 Views
mccPaul
Contributor I
Hi
 
Your project is linking against small memory model versions of the libraries so you either need to change the library dependencies or start a new project using the small memory model.
 
Paul.
0 Kudos
Reply

1,179 Views
stevec
Contributor III
I've started a new project with SMALL memory and imported my source files. Seems a lot better now. Also I've discovered the .map file so I can see where things are allocated.
Thanks for your help. I can now go away on holiday without this problem waiting for me when I get back!

Steve
0 Kudos
Reply

1,179 Views
mccPaul
Contributor I
Nice one!
0 Kudos
Reply

1,179 Views
stevec
Contributor III
CrasyCat,
the function
    void foo2(char*param);

defines param as a pointer to char. How do I use this in the function? Something like
    byte = param[i];
?
Haven't got my head quite round this yet.

Steve
0 Kudos
Reply

1,179 Views
CrasyCat
Specialist III
Hello
 
The ANSI C standard defines that a pointer to char is compatible to an array of char.
 
That means inside of the function foo2 you can use param as a pointer or as an array.
 
For example you can write
 
#define SIZE 256
 
void foo2(char*param) {
  int i;
 
  for (i = 0; i<SIZE; i++) {
    param[i]= i;
  }
}
or you can write
#define SIZE 256
 
void foo2(char*param) {
  int i;
 
  for (i = 0; i<SIZE; i++) {
    *param++= i;
  }
}
It just depends how you prefer to write it :smileywink:
 
CrasyCat
0 Kudos
Reply

1,179 Views
stevec
Contributor III
Thanks,
So variables are allocated stack space. This is my first time with hcs08. Most experience on  8051 where stack is limited to 128 bytes or 256 at best so using a stack over this (i.e more than 8 bits) is a novelty.

Steve
0 Kudos
Reply

1,179 Views
mccPaul
Contributor I
Hi
 
Automatic variables are allocated space on the stack as CrasyCat says. When you leave the scope of the function or block where the variable was declared it will be deleted automatically.
 
You can allocate variables yourself using malloc() or whatever equivalent your C runtime library offers. These are allocated on the heap and remain there untile your explicitly free them.
 
You can also create static variables, either declared within the scope of a function or block; or declared outside all functions and blocks. These are usually allocated space in the BSS section by the linker and are part of your runtime image.
 
Pointers and arrays are a very important part of the C language. IMHO they are the _most_ important thing to understand properly. If you come from an assembler background they are usually easy to understand, but newer languages which try to be more type safe tend to hide the gory details so pointers can be hard to follow.
 
K&R The C Programming Language is not a bad place to start, but you should learn as much as you can about pointers, dereferencing pointers, pointers to pointers, arrays, arrays of pointers, multi-dimensional arrays, pointers to arrays of pointers, etc, etc!
 
Cheers,
 
Paul.
 
Edit:
 
PS I say all this having absolutely no experience with the 8 bit CPU or compiler that you are using but with 20 years of other C experience.
 


Message Edited by mccp on 2007-06-20 10:00 AM
0 Kudos
Reply