473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

new -- allocate memory question

"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
1 2303
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Curt | last post by:
If this is the complete program (ie, the address of the const is never taken, only its value used) is it likely the compiler will allocate ram for constantA or constantB? Or simply substitute the...
4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
5
by: lixiaoyao | last post by:
hi all I use matrix & vector function to allocate the space myself in c, typedef struct matrix_array newdata; struct matrix_array{ float **sy,*sxx; }; newdata ndata;//new data struct...
6
by: bluekite2000 | last post by:
I was able to allocate a single array having 2.6GB (ie. w/ 700200000 elements of type float) yet when I did a cpuinfo on my computer, it showed a cache size of 256KB and 1GB of memory. What...
12
by: gc | last post by:
I am writing a function that given nx1 vector a and a nx1 b solves a system of equations f(a,c)=b for a nx1 c. While writing the function: 1] Should I allocate the memory for c within the...
4
by: marora | last post by:
I have created class definition which contains a charater pointer as one of it's data memeber. The objective is to read some data from a file, and assign it to a data member; Size of data is...
11
by: Divick | last post by:
Hi, can somebody help me figure out how can I make write a function which inturn uses malloc routine to allocate memory which is 2^k aligned? The condition is such that there should not be any...
5
by: ampeloso | last post by:
Hello, I would like to allocte memory, but I want it to start at a predefined address. I have a program that writes data to ROM in an embedded device and I would like to state where it goes. Can...
17
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
11
by: Bryan Parkoff | last post by:
I want to know how much static memory is limited before execution program starts. I would write a large array. The large array has 65,536 elements. The data size is double word. The static...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.