Allocating memory

Started by
5 comments, last by Keba 19 years, 11 months ago
When I allocate memory could i specifie where to start allocate? like start at: 0xFFFFFF04?, to just allign one part of the moemory would be good to, say start at: 0xYYYYYY04, where Y could be anything the computer like.... or what if i already had an array of let say chars(1byte each), and want to allign to an adress. To an adress where they start in memory. Let Say the array start att 0xFFFFFF04 and i want to allign the start to 0xFFFFFF10, is one of these possible? And I don''t want to copy the array, just change the adress. I guess it would be a pretty dangerous operation because i don''t know whats in that memory area.... thanks
Advertisement
You could probably do such a thing, provided you first allocate a huge chunk of memory and then build your own manager on top of that
And still your program is working only with virtual memory. Means it gets translated to real memory before the memory is accessed. So when you have pointer to 0x00000001 it can actually point to 0x00891a42 in the RAM. I think that OS with compiler align the addresses to something your computer will love. I want to tell you - don't care about this in the days of protected mode... Not on this level.

Oxyd

- Unreadable code is code written on a piece of paper, but not the one, in which the programmer is using a space in the place you don't.
- Real programmers aren't afraid of goto

[edited by - Oxyd on May 18, 2004 12:47:07 PM]
if you''re worried about memory alignment/fragmentation (and eventually it is a good thing to be worried about) you should look into writing a memory mananger. essentially a memory manager at its most basic level works something like this:

1) at beginning of game allocate a huge chunk of memory (256 / 512 / whatever your game requires). then divide that memory into perhaps a couple different "chunks" for different size/types of objects.

2) override the new/delete operators to actuall be call-throughs to the memory manager which will worry about fragmentation and whatnot.

essentially when you call new/delete the memory manager will pick the most appropriate location in the pre-allocated memory space and instantiate the object there. (picking that memory address is the whole art of the memory management process). you can pass a memory address to new and new will instantiate your object there. i believe the syntax is just new(memoryAddress), but don''t trust me on that i haven''t done this in a while...go look it up.

anyway, you need to go the route of pre-allocating that large memory chunk because you can''t just pass any old memory address to new. you might be inadvertently trying to allocate your object in an invalid memory space which would crash your program.

so yeah, spend some time researching memory managers and i bet you will end up with something that will accomplish what you want to do.

-me
Look up VirtualAlloc() on windows, and mmap() on *nix.
And on Windows you''ll have a lot of trouble referencing virtual memory addresses greater than 0x80000000. That is where the kernel mode memory starts. Trying to access that range of addresses from user mode will kill your program immediately. In fact, you probably should avoid addresses from 0x00000000 to 0x01000000 and from 0x75000000 to 0x7fffffff in the user mode address range as well.

In the end, you''ll be much better off letting Windows decide what the address to that chunk of memory should be.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
It is very simple to allocate memory starting at some aligned address. Is that what you are asking? If you want to allocate X bytes starting at an address aligned on an N-byte boundary (N is a power of two), do this:
    void * p;    Foo * pAligned;    p = malloc( X + N - 1 );    pAligned = (Foo *)( ( (unsigned)p + N - 1 ) & ~( N - 1 ) );  


John Bolton
Page 44 Studios
Current project: NHL Faceoff 2005 PS2
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement