new -- allocate memory question

B

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?
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
 
A

Alf P. Steinbach

* 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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top