开发者

Checking if memory had been initialized

If I have allocated some amount of memory via ::operator new(nbytes) and later I have constructed in this pool via "new (where) what" some number (but lesser than nbytes) of objs of appropriate type, is there a way to check where is the uninitialized memory starts?

Example

struct T{};
short noOfObj = 10;
T* p = static_cast<T*>(::operator new(sizeof(T) * noOfObj));
for (short i = 0; i < (noOfObj - 2); ++i)//here I'm constructing two less obj than available mem
{
new (p + i) T();
}

How could I check (not knowing by how much noOfObj has been decresed) where uninitialized memory pointed by p starts? (feel like I've screwed grammar again but I'm afraid that the best I can do for now)开发者_如何学Go


The only way is to keep track of the size of the object(s) that you placement newd and maintain a pointer to the unused portion yourself. There's no way for the system to provide that information to you.


wow... ok, trying to battle through this question...

It seems like you are on about doing your own memory management... such as requesting a huge array of chars to get a big block of memory under control of your program then using this to initialise your own variables, but you want to know what you have given to variables and what is still free.

Unless you keep track of what memory you have assigned and what you have not assigned, or keep track of every object you assign into this memory pool, no. As far as the OS will be concerned, it gave you that huge block of memory for chars (or what ever basic type you use).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜