Page 1 of 1
where are the variables in C?
Posted: Tue Apr 15, 2003 11:00 pm
by OSNewbie
hi, i just started using djgpp (gcc & ld) and nasm to create a kernel that ran my C code. this works fine, but i am just wondering how the compiler knows where in memory to store the variables that i create. for example, if i do this:
unsigned int myVar = 0;
where does myVar actually exist in RAM? i need to know where the variables are in memory so that i do not put something else there. also, how do i tell the compiler/linker to put them somewhere else, and is there any way to put a maximum on the amount of variable space it uses?
RE:where are the variables in C?
Posted: Tue Apr 15, 2003 11:00 pm
by carbonBased
last I checked, all _data_ was stored in the _data segment_.
Flagging the memory in use by your kernel is pretty easy... say you have a 100k kernel, that's loaded at the 1MB mark. It'd be pretty safe to say you shouldn't write to first 100k after the 1MB mark.
The only thing that gets somewhat difficult is the BSS section, but you've got two options:
1. make sure all your data is initialized (don't do this... it's a kludge!)
2. write a linker script that defines a variable at the end of your BSS section which, coincidentally, also defines the last byte of your kernel (assuming your linker script also defines your BSS segment as the last segment in the binary)
You can also use a linker script to arrange how the _linker_ assembles your binary (the compiler doesn't do this).
As for putting a limit on memory!? You should probably do some more research on how compilers work. Gcc isn't going to use any memory you don't tell it to (aside from padding, etc, which you _want_, for performance reasons).
Cheers,
Jeff
RE:where are the variables in C?
Posted: Tue Apr 15, 2003 11:00 pm
by VoidLogic
1. I'm sure you realise you can use the & (address of) operator to see the actual memory location.
example:
int A = 700;
cout<<A; (you get "700")
cout<<&A; (you get "00x0000" or some varient)
or
int* A;
*A = 100;
cout *A; (you get "100")
cout A; (you get "00x0000" or some varient)
2. Memory use opimization, due to the level of abstraction in C/C++, is heavily compiler dependent.
I hope that was some help.
-VoidLogic
RE:where are the variables in C?
Posted: Tue Apr 22, 2003 11:00 pm
by heliosc
All depends on where you declare it. There are only a few types of variables in C/C++:
static int gv = 0;
void Func()
{
static int sv = 0;
int av = 0;
int * hv = (int*)malloc(sizeof(int));
}
1) Automatic. This would be 'av' in the example.
These are created on the stack, on stack pages pointed to by esp. To keep these from conflicting, just make sure that no two threads are assigned the same stack pages.
2) Heap. This would be 'hv' in the example. These are created on heap pages, and it is up to your memory manager (since you're the one who writes malloc()) where they go.
3) Static. This would be 'sv' and 'gv' in the examples. Since it is known ahead of time how many of these variables there will be, space for these variables is set aside in the kernel file in two sections, .data and .bss. There is actually no .bss space per se. The .bss section occupies zero bytes in the file, but takes up X bytes (you can find X by parsing the kernel image header) in memory right after the kernel file. In gcc, there are two special variables (something like __bss_start and __bss_end) defined by the linker that let you find out the location of the bss without having to parse the kernel header.
Now if you want to move the .bss or .data sections, you can pass the linker a command of the form "-Tdata" or "-Tbss" that lets you specify where in memory you want the data and bss to reside.
RE:where are the variables in C / malloc()
Posted: Tue Apr 22, 2003 11:00 pm
by OSNewbie
thank you this is very helpful.
one other thing, since i am just starting my C kernel, what are the purposes for the malloc() function? how does it work and keep track of where different things are located, etc...?
thank you in advance
RE:where are the variables in C / malloc()
Posted: Wed Apr 23, 2003 11:00 pm
by Anton
malloc() and free() are for dynamic memory allocation. Dynamic means that you know how much memory you need at run time only(,not at compile time).
You use it like this:
double *a;
int n;
n = getSizeofArray();
a = (double*)malloc(sizeof(double)*n);
if( !a ){
printf("error:need more memory\n");
}
malloc works and keeps track of thigs only if it is used correctly, since it is a manual memory manager, which means a)you need to call free() by your self b)you need to pass a correct(the one got from malloc) pointer to free. If you pass a wrong pointer to free(), malloc will break(at some time). When you get a memory block from malloc(), a couple bytes before the start of it contain some info used by free(and malloc), like the size of the block. So if you write something in that area, you will also break the mamemory manager.
Anton.
RE:where are the variables in C / malloc()
Posted: Fri Apr 25, 2003 11:00 pm
by heliosc
How malloc() keeps track of memory all depends on how you write it. Basically, malloc()/free() have the following prototypes:
void* malloc(size_t sz);
void free(void* ptr);
sz is a variable (usually, size_t just means int) that tells how many bytes the program wants. malloc() is supposed to return a pointer to a block of memory at least that big, which can be located anywhere it wants. This memory must not be given out again to any other client until the original client calls free() on that block. These are the only hard requirements. All the decisions about how to implement it are up to you. A very straightforward technique is the following:
- You have a fixed block of memory to serve malloc(). Say it starts at address 0x100000 (the 1MB mark) and runs for 1MB to 0x200000. Now, the central data structure is a linked list that keeps track of all the free-areas in memory. It is common practice to keep this list inside the unallocated memory itself, since nobody else is using it. Now whenever you allocate X bytes to someone, you go through the list to find a chunk big enough to satisfy the request. If the chunk is bigger than necessary, you split it into two chunks, give one to the application requesting the memory, and put the other back on the free list. When an application returns the memory it allocated, you have to not only return it to the free list, but you also have to check to see if freeing that memory allows several smaller chunks to be merged (coalesced is the common term) into one larger chunk.
RE:where are the variables in C / malloc()
Posted: Sun Apr 27, 2003 11:00 pm
by VoidLogic
you can also write macros to redefine new, delete, if you wish your code to look more modern
RE:where are the variables in C / malloc()
Posted: Sun Apr 27, 2003 11:00 pm
by OSNewbie
ok, so how do malloc() and free() relate to the basic ways of creating a variable in C? for example, as of right now i have a basic kernel and for keeping track of stuff i write code like this:
unsigned char kbdFlags = 0;
unsigned int cursorPosition = 0;
so does the linker put variables like this in the data section and is it good to use the data section or should the creation of all variables in C include a call to malloc()? i understand more now how malloc() works but not when to use it, do you only create variable with a call to malloc() when the are temporary variables or what?
thanks again