473,883 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initialized arrays using new


This produces an initialized array to zero:

int *i = new int[100]() ;

004124B0 push ebp
004124B1 mov ebp,esp
004124B3 mov eax,dword ptr [count]
004124B6 push eax
004124B7 call operator new (411212h)
004124BC add esp,4
004124BF pop ebp

But this doesn't initialize the array. The assembly output is identical.
What's going on?

int *i = new int[100] ;

004124B0 push ebp
004124B1 mov ebp,esp
004124B3 mov eax,dword ptr [count]
004124B6 push eax
004124B7 call operator new (411212h)
004124BC add esp,4
004124BF pop ebp
Aug 17 '06
34 13256

"Frederick Gotham" <fg*******@SPAM .comwrote in message
news:_w******** ***********@new s.indigo.ie...
Greg Comeau posted:
>It probably didn't actually do the zero new in the latter case
and happened to return a pointer to a block that was already zero'd.


The assembly code was identical though -- so "new" must zero its memory by
default on that system... no?
No, it was just an oversight on the poster's part. The assembly code was
not identical. There was code after that part which initialized the memory
in the case with the () included, just as there should have been.

-Howard
Aug 18 '06 #31
Pete Becker wrote:
Frederick Gotham wrote:
Signed char may trap in C (it may also contain padding).

6.2.6.1/5:

Certain object representations need not represent a value of the
object type. If the stored value has such a representation and
is read by an lvalue expression THAT DOES NOT HAVE CHARACTER
TYPE, the behavior is undefined. If such a representation is
produced by a side effect that modifies all or any part of the
object by an lvalue expression THAT DOES NOT HAVE CHARACTER
TYPE, the behavior is undefined. Such a representation is
called a trap representation. [emphasis added]

Reading and writing a value through any character type are both well
defined, i.e. they do not trap.
And that's an important feature, because it allows one to cast the
pointer to an object to a char type (unsigned ususally) and examine the
byte representation of that object without worrying about a trap.

Brian

Aug 18 '06 #32
In article <11************ **********@h48g 2000cwc.googleg roups.com>,
<to***********@ yahoo.comwrote:
>
Is this related to the C++ feature where;

int x;

int y = int();

y is guaranteed to be initialized?
Related may not be the right word, but yes.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 18 '06 #33
In article <_w************ *******@news.in digo.ie>,
Frederick Gotham <fg*******@SPAM .comwrote:
>Greg Comeau posted:
>It probably didn't actually do the zero new in the latter case
and happened to return a pointer to a block that was already zero'd.

The assembly code was identical though -- so "new" must zero its memory by
default on that system... no?
Then the compiler is one or more of: broken, not conformant,
not in strict mode, has an extension, or, the 0'ing code was
unobvious.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 18 '06 #34
Default User posted:
>Reading and writing a value through any character type are both well
defined, i.e. they do not trap.

And that's an important feature, because it allows one to cast the
pointer to an object to a char type (unsigned ususally) and examine the
byte representation of that object without worrying about a trap.

Yes, I do so frequently -- but I always use the "unsigned" flavour.

In C++, neither signed char nor unsigned char may contain padding. As plain
char is congruent to one of these types, plain char may not contain padding
either.

In C, unsigned char may not contain padding, but signed char may. As plain
char is congruent to one of these types, plain char may contain padding.

When talking about C, it's best to indicate whether you're talking about
C89 or C99. C89 is the more dominant of the two.

Signed char can trap in C89.

As for signed char trapping in C++, it's hard to get a definitive answer --
just look how conclusive the responses are to this thread:

http://groups.google.ie/group/comp.s...9edfa2eed6267b
c2/47ccdf97703d87c 1?lnk=st&q=&rnu m=1&hl=en#47ccd f97703d87c1

--

Frederick Gotham
Aug 18 '06 #35

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
3703
by: Dennis | last post by:
I would like to dynamically allocate in a sub a 2 dimensional Array float *myarray = new float ; of course I get an error. How do you allocate a 2D array using the New operator? I currently use static float gxy;
6
2384
by: rainy6144 | last post by:
Does the following code have defined behavior? double *new_array(unsigned n) { double *p = malloc(n * sizeof(double)); unsigned i; for (i = 0; i < n; i++) p = 0.0; return p; }
5
3685
by: plsHelpMe | last post by:
How to create dynamic javascript arrays using dojo toolkits Hello frens, I am in a big trouble. My objective is: I am having some categories shown by differnent radio buttons, on the click of a particular radio button i making an ajax call using dojo and retirieving the data corresponding to that category. Now i am suppose to create array of that data using the javascript dynamically. Can anyone please help me for the same. For your...
0
9932
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11109
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10833
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10405
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7959
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3227
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.