Hello all

I'm a fresher to Microcontroller programming. I have defined certain global variables that I need to store at consecutive memory locations, say starting from 0x8000h . Will appreciate if anyone can give me the syntax / instructions to go about the same.

thanks

techie

If you know where you want the data, just create a global variable pointing to it. This of course assumes that you point it to a valid address, and you'll have to make sure they line up (and don't overlap) yourself.

hi

can you please give me an example... assuming I have three interger gloabal variables a, b, c and an array test[10] of type double, how do i go about allocating memory for them from say, 0x8000h ??

thanks

techie

This isn't tested, but it should give you the idea:

int* a = 0x8000;
int* b = 0x8000 + sizeof(int); // account for a
int* c = 0x8000 + 2*sizeof(int); // account for a and b
double* test = 0x8000 + 3*sizeof(int); // account for a,b,c
int* yetAnother = 0x8000 + 3*sizeof(int) + 10*sizeof(double); // etc...

Alternatively, you could hardcode direct addresses (0x8000, 0x8004, etc...), but you'd have to know the size of each type.

Placing things in memory is down to the linker, not the compiler.
Which tool chain (compiler, linker etc) are you using?

Seems like you're trying to do something wrong. :)
If you want contiguous chunk of memory you can just declare an array of appropriate size and it's guaranteed that it's continuous. But I suppose your problem is variables and not just one variable.
Well I donno whether you can do this or not, my guess is it's not possible to force allocation of variables on continous locations. But irrespective of that I somehow don't like the idea of writing some code that depends on how the variable are allocated in memory.

Now if you are hell bent on it and have a very good reason, you can misuse typecasting like this.

//say we want to have 3 global variables,
//one double, one char array[10] and one int.
//say on the machine where we run, sizeof() double is 8,
//char is 1 and int is 4.
//So total = 8 + (1*10 + 1) + 4 = 23 bytes

//declare/define a global char array of this size
char global_allocated_memory[23] ;

//declare 3 variables. (have to be pointers in C, in C++
//you have the option of using reference instead)
double* g_double ;
int* g_int ;
char* g_charArr;

int main()
{
    g_charArr = (char*) &(global_allocated_memory[0]) ;
    g_double = (double*) &(global_allocated_memory[11]) ;
    g_int = (int*) &(global_allocated_memory[19]) ;

    *g_int = 11 ;
    *g_double = 22 ;
    strcpy( g_charArr, "Char Arr" ) ;

    printf("%f, %d, \"%s\"\n", *g_double, *g_int, g_charArr ) ;

    return 0 ;
}

Remember you still have the problem that you are NOT controlling the allocation of 3 pointers ( g_double, g_int and g_charArr), they may not be continuous.

Seems like you're trying to do something wrong. :)

No, I'm pretty sure his demands are legit. There are times in a low-level application where you need to specify a certain address (MMIO is an example of this, albeit a poor one). He'll be just fine if he sets the addresses as he needs, so long as he double checks his memoy layout.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.