By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

new -- allocate memory question

P: n/a
"U_BYTE Mem[0x1000000];" is stored in the global variable. It contains
10 megabytes. Do run-time automatically use new keyword to allocate 10
megabytes into memory, and then zero is filled automatically during the
initialization before it reaches to use main() function?
Do you think that it is always unsafe because it may unexpect crash by
not having enough memory? Is it safe to use new keyowrd and fill zero
manually inside main() function?
How much space can global variable be limited? 64KB?

Look at my example code.

If you say that second example is the best, please say the limit in the
global variable as static memory, but not dymanic memory. Thanks...

First Example:
unsigned char Mem[0x01000000]; // 10MB

int main(void)
{
for (int a = 0; a < 0x01000000; a++)
Mem[a] = 0x41;

return 0;
}

Second Example:
unsigned char Mem2[0x00010000]; // 64KB

int main(void)
{
unsigned char* Mem = new Mem[0x01000000];

if (Mem == 0) // exit if NULL
return -1;

for (int a = 0; a < 0x01000000; a++)
Mem[a] = 0x41;

delete [] Mem;

return 0;
}

Bryan Parkoff
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
* Bryan Parkoff:
"U_BYTE Mem[0x1000000];" is stored in the global variable. It contains
10 megabytes. Do run-time automatically use new keyword to allocate 10
megabytes into memory, and then zero is filled automatically during the
initialization before it reaches to use main() function?
No high quality implementation would do that.

Do you think that it is always unsafe because it may unexpect crash by
not having enough memory?
That depends entirely on the environment.

Is it safe to use new keyowrd and fill zero manually inside main()
function?
"Safe" is not very precise, but that approach gives you better control.

How much space can global variable be limited? 64KB?
Depends on the compiler and the runtime environment.

Look at my example code.

If you say that second example is the best, please say the limit in the
global variable as static memory, but not dymanic memory. Thanks...
Impossible to know since my telepathic abilities were damaged some time
again.
First Example:
unsigned char Mem[0x01000000]; // 10MB
That's 16 MiB, or approximately 16.78 MB; perhaps you meant to write
0x10 MiB?

int main(void)
{
for (int a = 0; a < 0x01000000; a++)
Mem[a] = 0x41;

return 0;
}
You're not guaranteed that 'int' can represent numbers that large,
but in practice this is OK.

Second Example:
unsigned char Mem2[0x00010000]; // 64KB

int main(void)
{
unsigned char* Mem = new Mem[0x01000000];

if (Mem == 0) // exit if NULL
return -1;
This will never execute for a conforming C++ implementation.

If allocation fails you'll either get a hang, a crash, or a std::bad_alloc
exception.

You will not get pointer value 0 in any event.

for (int a = 0; a < 0x01000000; a++)
Mem[a] = 0x41;

delete [] Mem;

return 0;
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.