Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Addressing same memory using different name in C

Status
Not open for further replies.

tom12sg

Member level 2
Joined
Feb 1, 2004
Messages
45
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,288
Activity points
422
Hi,

I used to code in assemble and normally I will use
#define newname oldname
to reuse the same global variable in another module. Thus saving ram usage.

How can I do that in C language for global variable or structure. Cos after my program exit from a state, the values in structure and variables are no longer required. I know of using union, is that the only way out?

Please advice by giving simple example to illustrate if there is better way. By the way I'm using Keil complier, which follow ANSI standard.

Thanks for the help.
 

Hi,
Use pointers to access the memory. i have done it may times.

If you want to save the pointers also, just define the variable as global and use it.

BRMadhukar
 

you can use dynamic memory allocation in keil

see :
void init_mempool (void xdata *p,unsigned int size);
void *malloc (unsigned int size);
void free (void xdata *p);


have fun

Fire in the Wire :sm2:
 

Hi,

Thought that malloc can't be used for embedded system.... I'm wrong then. Will it use extra code? what does it translate into assemble code, just curious.
As for using global pointer, is not the name can't be change? It will not be so readable when next time view through the code isn't?
 

Seqrch on net , there are reentrant malloc libraries especially for your case .

Also , why do not you use 'extern' keyword to make other module's global var accessible without allocating memory for that var ?
 

Aren't using extern will need to use the same old variable name again?
It would be nice to use the same variable but with different name, so that the name chosen will be meanful for that particular usage.
 

Actually this practice is not recommended at all , but you can define different name for that variable like
in one file where actual name memory allocation is done :

int global1;

in another file where this variable is to be used under different name :

extern int global1;

#define global2 global1

then access that variable as

global2 = 0 ; just example .
 

artem said:
Actually this practice is not recommended at all
So what is the correct or best way to reuse the same memory for other purpose usage?
Let's teach and share with one another, thanks!
 

tom12sg,

if I do not misunderstand your question you want to reuse memory that was used by one subroutine in another subroutinte?

I would say the C compiler does this automatically for you, that is one of the reasons compilers have been invented. They know much better than you which portion of memory is used by which subroutine and which portion can be reused later.

So I would say let Keil do this for you (it does this automatically) and enjoy :)

best regards
 

There is much way such
* using Extern Keyword
* using Pointer ex. char *temp = &a
* using special embedded compiler option such int a = @20;
that say variabel a point at ram 20

Maybe the other guys have another way ?
 

@C-Man:
tom12sg probably doesn't mean in subroutines since that would be obvious. I think he means he has 2 or more functional blocks in his software but not all of them are active at the same time.

@tom12sg:
The correct way is to use a kind of memory management unit in the software
eg malloc or a custom allocation unit specifically designed for your software. And no, it isn't bad to use malloc in uC systems! You just have to be carefull ;-)! But hey, you have to be carefull when using pointers too.

What should the memory management unit do:
you have a pool of memory (1 big buffer, a number of linked sectors, ...)
Your module requests memory (malloc of a number of bytes or blocks)
=> memory allocation unit marks that part of the memory as free and returns a 'pointer' to the requesting module.
When the module doesn't need the memory it tells the memory allocation unit to free that part of the memory.

The complexity can vary allot ;-):
* a few fixed size buffers to be allocated
* malloc as we know it (handing out linear arrays of bytes)
* complete filesystem-like structure
* database like structure
on bigger systems you probably have an hardware mmu (and probably an OS too ;-) )

What's best depends on the application, amount of memory available, size of the blocks to be allocated (and fixed or variable size), amount of blocks, how dynamic allocation is, ...

A simple 2 buffer allocation example (it's written quickly so there might be errors in it ;-) ):
Code:
char buffer1[16], buffer2[16];
char buffer1Free = 1;
char buffer2Free = 1; // These can be in 1 byte too

char * myMalloc(void)
{
  if (buffer1Free == 1)
    {
      buffer1Free = 0;
      return buffer1;
    }
  else if (buffer2Free == 1)
    {
      buffer2Free = 0;
      return buffer2;
    }
  return 0; // null pointer when there is no free memory
}

void myFree(char * buffer)
{
    if (buffer == buffer1)
      buffer1Free = 1;
    else if (buffer == buffer2)
      buffer2Free = 1;
}

and an module uses this like this:
Code:
char * buffer;
...
buffer = myMalloc();
if (buffer == 0)
  error ...;
...
myFree(buffer);

Antharax
 

do anyone have some intro embedded system books?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top