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

anything wrong with this code?

P: n/a
hey guys
anything wrong with this code??
if it is then what?

int *array(int n){
return new int(n);
}
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
printf( "%d\n", p[0] );
p = array(10);
printf( "%d\n", p[0] );
return 0;
}

Mar 7 '07 #1
Share this Question
Share on Google+
28 Replies


P: n/a
On 7 Mar, 08:21, "hijkl" <s_mah...@yahoo.comwrote:
hey guys
anything wrong with this code??
if it is then what?
First of all you are using pointers, which is generally no a good
idea. And second, you use an array, we generally prefer that you use a
vector.
int *array(int n){
return new int(n);
return new int[n];
}
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {
It is often preferred to use ++i instead, which is at least as fast as
i++ but can potentially be faster, especially when working with
iterators instead of integers.
p[i] = 0;
}
printf( "%d\n", p[0] );
Memory leak, you forgot to free the memory you allocated on the first
call to array(), insert delete[] p;
p = array(10);
printf( "%d\n", p[0] );
return 0;
}
I fail to see the point of your code, is it to show that a variable
allocated with new is uninitialized?

--
Erik Wikstr鰉

Mar 7 '07 #2

P: n/a
On 3月7日, 下午3时21分, "hijkl" <s_mah...@yahoo.comwrote:
hey guys
anything wrong with this code??
if it is then what?

int *array(int n){
return new int(n);
}
This is equal to
int *array(int n)
{
int *i = new int;
*i = 10;
return i;
}
Surely, the functionality is not matching the function name.
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
printf( "%d\n", p[0] );
p = array(10);
printf( "%d\n", p[0] );
return 0;
}

Mar 7 '07 #3

P: n/a
sorry but u misunderstood.
array is a function name i think ..

Mar 7 '07 #4

P: n/a
or i am not about array()

Mar 7 '07 #5

P: n/a
mimi
but that doesnt matters..its same thing wat u mentioned/

Mar 7 '07 #6

P: n/a
hijkl wrote:
sorry but u misunderstood.
array is a function name i think ..
Misunderstood what? Leave some context and drop the silly abbreviations.

--
Ian Collins.
Mar 7 '07 #7

P: n/a
hijkl wrote:
hey guys
anything wrong with this code??
if it is then what?
It won't compile and even if it did, it leeks memory.
int *array(int n){
return new int(n);
}
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
printf( "%d\n", p[0] );
p = array(10);
printf( "%d\n", p[0] );
return 0;
}
--
Ian Collins.
Mar 7 '07 #8

P: n/a
ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.

thanks
sanjay

Mar 7 '07 #9

P: n/a
hijkl wrote:
ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.

thanks
sanjay
Only two int pointer that need to be freed.

Your main misunderstanding seem to be that you think this code

int *array(int n){
return new int(n);
}

allocates n integers. It only allocates *one*. If you want to allocate n
integer you write it like this

int *array(int n){
return new int[n];
}

[n] not (n)

john
Mar 7 '07 #10

P: n/a
hijkl wrote:
ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.
What program? Please keep enough context for your reply to make sense.

--
Ian Collins.
Mar 7 '07 #11

P: n/a
On Mar 7, 12:08 am, Ian Collins <ian-n...@hotmail.comwrote:
hijkl wrote:
ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.

What program? Please keep enough context for your reply to make sense.

--
Ian Collins.
same program i posted

Mar 7 '07 #12

P: n/a
On Mar 7, 12:07 am, John Harrison <john_androni...@hotmail.comwrote:
hijkl wrote:
ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.
thanks
sanjay

Only two int pointer that need to be freed.

Your main misunderstanding seem to be that you think this code

int *array(int n){
return new int(n);
}

allocates n integers. It only allocates *one*. If you want to allocate n
integer you write it like this

int *array(int n){
return new int[n];
}

[n] not (n)

john

Mar 7 '07 #13

P: n/a
On Mar 7, 12:07 am, John Harrison <john_androni...@hotmail.comwrote:
hijkl wrote:
ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.
thanks
sanjay

Only two int pointer that need to be freed.

Your main misunderstanding seem to be that you think this code

int *array(int n){
return new int(n);
}

allocates n integers.
HI JOhn
i understand that this code allocates only one intger.
but

for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
this code will initialize next 9 integer to consecutive locations..
if you will debug then u will notice that..
let me know if you not agree with me.

Mar 7 '07 #14

P: n/a
hijkl wrote:
On Mar 7, 12:07 am, John Harrison <john_androni...@hotmail.comwrote:
>>
Only two int pointer that need to be freed.

Your main misunderstanding seem to be that you think this code

int *array(int n){
return new int(n);
}

allocates n integers.

HI JOhn
i understand that this code allocates only one intger.
but

for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
this code will initialize next 9 integer to consecutive locations..
if you will debug then u will notice that..
let me know if you not agree with me.
No, it will write over what ever lives in memory after the single
integer you have allocated.

--
Ian Collins.
Mar 7 '07 #15

P: n/a
hijkl wrote:
On Mar 7, 12:08 am, Ian Collins <ian-n...@hotmail.comwrote:
>>hijkl wrote:
>>>ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.

What program? Please keep enough context for your reply to make sense.

same program i posted
On Usenet, each post should make sense on its own, so keep enough
context in your replies. You should also snip people's signatures (the
bit after "-- ").

--
Ian Collins.
Mar 7 '07 #16

P: n/a
On Mar 7, 1:09 am, "hijkl" <s_mah...@yahoo.comwrote:
On Mar 7, 12:07 am, John Harrison <john_androni...@hotmail.comwrote:


hijkl wrote:
ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.
thanks
sanjay
Only two int pointer that need to be freed.
Your main misunderstanding seem to be that you think this code
int *array(int n){
return new int(n);
}
allocates n integers.

HI JOhn
i understand that this code allocates only one intger.
but

for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
this code will initialize next 9 integer to consecutive locations..
if you will debug then u will notice that..
let me know if you not agree with me.- Hide quoted text -

- Show quoted text -
John
try following code and you will understand how its working
....
for( int i = 0; i < 10; i++ ) {
p[i] = i;
}

printf("%d", [0]);
printf("%d", [1]);
printf("%d", [2]);
printf("%d", [3]);
Mar 7 '07 #17

P: n/a
hijkl wrote:
On Mar 7, 1:09 am, "hijkl" <s_mah...@yahoo.comwrote:
>>On Mar 7, 12:07 am, John Harrison <john_androni...@hotmail.comwrote:
>>>Your main misunderstanding seem to be that you think this code
>>>int *array(int n){
return new int(n);
}
>>>allocates n integers.
<snip>
>
so its cleary says that it allocated 10 integers.
No, it clearly shows that 10 unfortunate memory locations have been
written over.

--
Ian Collins.
Mar 7 '07 #18

P: n/a
On 7 Mar, 10:26, "hijkl" <s_mah...@yahoo.comwrote:
On Mar 7, 1:09 am, "hijkl" <s_mah...@yahoo.comwrote:
On Mar 7, 12:07 am, John Harrison <john_androni...@hotmail.comwrote:
hijkl wrote:
ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.
thanks
sanjay
Only two int pointer that need to be freed.
Your main misunderstanding seem to be that you think this code
int *array(int n){
return new int(n);
}
allocates n integers.
HI JOhn
i understand that this code allocates only one intger.
but
for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
this code will initialize next 9 integer to consecutive locations..
if you will debug then u will notice that..
let me know if you not agree with me.- Hide quoted text -
- Show quoted text -

John
try following code and you will understand how its working
...
for( int i = 0; i < 10; i++ ) {
p[i] = i;
}

printf("%d", [0]);
printf("%d", [1]);
printf("%d", [2]);
printf("%d", [3]);
.
.
.
printf("%d", [9]);
...
the output will be
0
1
2
.
.
.
9

so its cleary says that it allocated 10 integers.
It's just luck that keeps this code from crashing, or doing something
worse. What you have is undefined behaviour, to see that just consider
what would happen if the integer you allocated were located on the
last addressable memory location?

--
Erik Wikstr鰉

Mar 7 '07 #19

P: n/a
On Mar 7, 4:21 pm, "hijkl" <s_mah...@yahoo.comwrote:
hey guys
anything wrong with this code??
if it is then what?

int *array(int n){
return new int(n);
}
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
printf( "%d\n", p[0] );
p = array(10);
printf( "%d\n", p[0] );
return 0;
}
Seems you are trying to do the following
int *array(int n)
{
return new int[n];
}
int main()
{
UINT ARRAY_SIZE = 10;
int *p = array(ARRAY_SIZE);
memset( array, 0, ARRAY_SIZE))
printf( "%d\n", p[0] );
/* This statement will make the previous allocated memory orphan.
by doing this you are leaving handle the memory location where an
array of
integer pointers allocated. p will take any address which we are
assigning.
putting a delete []p; will avoid memory leak problems
*/
p = array(10);
return 0;
}
Mar 7 '07 #20

P: n/a
On Mar 7, 1:54 am, "Erik Wikstr鰉" <eri...@student.chalmers.sewrote:
On 7 Mar, 10:26, "hijkl" <s_mah...@yahoo.comwrote:


On Mar 7, 1:09 am, "hijkl" <s_mah...@yahoo.comwrote:
On Mar 7, 12:07 am, John Harrison <john_androni...@hotmail.comwrote:
hijkl wrote:
ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.
thanks
sanjay
Only two int pointer that need to be freed.
Your main misunderstanding seem to be that you think this code
int *array(int n){
return new int(n);
}
allocates n integers.
HI JOhn
i understand that this code allocates only one intger.
but
for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
this code will initialize next 9 integer to consecutive locations..
if you will debug then u will notice that..
let me know if you not agree with me.- Hide quoted text -
- Show quoted text -
John
try following code and you will understand how its working
...
for( int i = 0; i < 10; i++ ) {
p[i] = i;
}
printf("%d", [0]);
printf("%d", [1]);
printf("%d", [2]);
printf("%d", [3]);
.
.
.
printf("%d", [9]);
...
the output will be
0
1
2
.
.
.
9
so its cleary says that it allocated 10 integers.

It's just luck that keeps this code from crashing, or doing something
worse. What you have is undefined behaviour, to see that just consider
what would happen if the integer you allocated were located on the
last addressable memory location?

--
Erik Wikstr鰉- Hide quoted text -

- Show quoted text -
yea make sense :)..

Mar 7 '07 #21

P: n/a
hijkl wrote:
On Mar 7, 1:09 am, "hijkl" <s_mah...@yahoo.comwrote:
>>On Mar 7, 12:07 am, John Harrison <john_androni...@hotmail.comwrote:

>>>hijkl wrote:

ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.
>>>>thanks
sanjay
>>>Only two int pointer that need to be freed.
>>>Your main misunderstanding seem to be that you think this code
>>>int *array(int n){
return new int(n);
}
>>>allocates n integers.

HI JOhn
i understand that this code allocates only one intger.
but

for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
this code will initialize next 9 integer to consecutive locations..
if you will debug then u will notice that..
let me know if you not agree with me.- Hide quoted text -

- Show quoted text -


John
try following code and you will understand how its working
...
for( int i = 0; i < 10; i++ ) {
p[i] = i;
}

printf("%d", [0]);
printf("%d", [1]);
printf("%d", [2]);
printf("%d", [3]);
.
.
.
printf("%d", [9]);
...
the output will be
0
1
2
.
.
.
9

so its cleary says that it allocated 10 integers.

thanks
sanjay
No it doesn't. You allocate 1 integer, and write the rest of your code
as if you have allocated 10. This is UNDEFINED BEHAVIOUR. How is what
your program doing different from undefined behaviour. Undefined
behaviour means ANYTHING CAN HAPPEN.

The rules of C++ say that new int(n) allocates one integer. That is the
only thing that matters.

john
Mar 7 '07 #22

P: n/a
On Mar 7, 1:27 pm, John Harrison <john_androni...@hotmail.comwrote:
hijkl wrote:
On Mar 7, 1:09 am, "hijkl" <s_mah...@yahoo.comwrote:
>On Mar 7, 12:07 am, John Harrison <john_androni...@hotmail.comwrote:
>>hijkl wrote:
>>>ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.
>>>thanks
sanjay
>>Only two int pointer that need to be freed.
>>Your main misunderstanding seem to be that you think this code
>>int *array(int n){
return new int(n);
}
>>allocates n integers.
>HI JOhn
i understand that this code allocates only one intger.
but
for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
this code will initialize next 9 integer to consecutive locations..
if you will debug then u will notice that..
let me know if you not agree with me.- Hide quoted text -
>- Show quoted text -
John
try following code and you will understand how its working
...
for( int i = 0; i < 10; i++ ) {
p[i] = i;
}
printf("%d", [0]);
printf("%d", [1]);
printf("%d", [2]);
printf("%d", [3]);
.
.
.
printf("%d", [9]);
...
the output will be
0
1
2
.
.
.
9
so its cleary says that it allocated 10 integers.
thanks
sanjay

No it doesn't. You allocate 1 integer, and write the rest of your code
as if you have allocated 10. This is UNDEFINED BEHAVIOUR. How is what
your program doing different from undefined behaviour. Undefined
behaviour means ANYTHING CAN HAPPEN.

The rules of C++ say that new int(n) allocates one integer. That is the
only thing that matters.

john- Hide quoted text -

- Show quoted text -
John
but it only allocates one integer, then initialize memory next to
current allocated memory. correct?
isnt it undefined behaviour? because memory you are trying to
initialize is not allocated by you.
If this is the case then how can we access that memory without
allocating it?
for example
int *p = new int(2);
_________
| 2 |
|_________|
p = 1233724
so it will allocate one memory location of type integer and assign
value to it.
then why we are using p as a array i.e. p[0],p[1]...
i am really confused and dont understand this concept cleary..sorry
about that but i need to clearify it :)

so p[1] is equal to value at p++ right i.e. value at address 1233728
correct?
u understand my confusion?? if yes then help me out :)
thanks guys

Mar 8 '07 #23

P: n/a
hijkl wrote:
On Mar 7, 1:27 pm, John Harrison <john_androni...@hotmail.comwrote:
>>hijkl wrote:
>>>On Mar 7, 1:09 am, "hijkl" <s_mah...@yahoo.comwrote:
>>>>On Mar 7, 12:07 am, John Harrison <john_androni...@hotmail.comwrote:
>>>>>hijkl wrote:
>>>>>>ok i tried running this program..it will run fine only problem with
>>this code is memory leak..
>>there atleast 12 int pointers that needs to free at the end.
>>>>>>thanks
>>sanjay
>>>>>Only two int pointer that need to be freed.
>>>>>Your main misunderstanding seem to be that you think this code
>>>>>int *array(int n){
return new int(n);
}
>>>>>allocates n integers.
>>>>HI JOhn
i understand that this code allocates only one intger.
but
>>> for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
this code will initialize next 9 integer to consecutive locations..
if you will debug then u will notice that..
let me know if you not agree with me.- Hide quoted text -
>>>>- Show quoted text -
>>>John
try following code and you will understand how its working
...
for( int i = 0; i < 10; i++ ) {
p[i] = i;
}
>>printf("%d", [0]);
printf("%d", [1]);
printf("%d", [2]);
printf("%d", [3]);
.
.
.
printf("%d", [9]);
...
the output will be
0
1
2
.
.
.
9
>>>so its cleary says that it allocated 10 integers.
>>>thanks
sanjay

No it doesn't. You allocate 1 integer, and write the rest of your code
as if you have allocated 10. This is UNDEFINED BEHAVIOUR. How is what
your program doing different from undefined behaviour. Undefined
behaviour means ANYTHING CAN HAPPEN.

The rules of C++ say that new int(n) allocates one integer. That is the
only thing that matters.

john- Hide quoted text -

- Show quoted text -


John
but it only allocates one integer, then initialize memory next to
current allocated memory. correct?
Yes
isnt it undefined behaviour?
Yes

because memory you are trying to
initialize is not allocated by you.
If this is the case then how can we access that memory without
allocating it?
Because its undefined behaviour, undefined behaviour does not mean 'you
can't do that', it means 'if you do that, then what will happen is
undefined'.

for example
int *p = new int(2);
_________
| 2 |
|_________|
p = 1233724
so it will allocate one memory location of type integer and assign
value to it.
then why we are using p as a array i.e. p[0],p[1]...
i am really confused and dont understand this concept cleary..sorry
about that but i need to clearify it :)
Undefined behaviour means anything can happen. The C++ standard does not
say how your program will run. Anything could happen, it could crash, it
could 'work', it could print out garbage, it could print out something
sensible, anything could happen. It could work one way with one compiler
and a different way with another compiler, it could work the first time
you run it, but not work the second time. Anything could happen.
>
so p[1] is equal to value at p++ right i.e. value at address 1233728
correct?
u understand my confusion?? if yes then help me out :)
thanks guys
Undefined behaviour does not mean something bad must happen, it means
anything could happen.

john
Mar 8 '07 #24

P: n/a
>
>>
so p[1] is equal to value at p++ right i.e. value at address 1233728
correct?
u understand my confusion?? if yes then help me out :)
thanks guys
The C++ standard is very good at saying how correct programs must
behave. If you write legal C++, then (pretty much) the C++ standard says
how that program will behave.

But if you write illegal C++ (like you did) then the C++ standard says
very little about how you're program must behave, it is 'undefined
behaviour'. If you want to know why your illegal C++ program behaved the
way it did, that is not a question for C++. It's a question for the
people who wrote the compiler you are using. Only they can say why they
made the compiler produce the code it did. They were not following the
C++ standard when they wrote that part of the compiler because the C++
standard has nothing to say other than 'undefined behaviour'.

There are three kinds of C++ program (at least)

1) Those that don't compile.
2) Those that compile and run and have defined behaviour according to
C++ standard.
3) Those that compile and run but don't have defined behaviour according
to C++ standard.

It is a shock to newbies to find that this third kind of program exists,
and it undoubtedly makes programming C++ harder, because eyour program
can seem to be doing something sensible yet in reality it has undefined
behaviour.

If you really have a philosophical problem with undefined behaviour then
there are languages that have none (e.g. Java).

john
Mar 8 '07 #25

P: n/a
Erik Wikstr鰉 wrote:
On 7 Mar, 08:21, "hijkl" <s_mah...@yahoo.comwrote:
anything wrong with this code?? if it is then what?

First of all you are using pointers, which is generally no a good
idea. And second, you use an array, we generally prefer that you use a
vector.
but surely there are times when pointers make sense?

- life time of object doesn't correspond to any scope
- data structures like trees
- where an object needs a "reference" to another object
and it changes (general case of previous reason)
- polymorphism
- implementaion of vector or string
<snip>
--
Nick Keighley

"When anyone says `theoretically,' they really mean `not really.'"
-- David Parnas

Mar 8 '07 #26

P: n/a
On Wed, 07 Mar 2007 21:06:24 -0800, hijkl wrote:
On Mar 7, 1:27 pm, John Harrison <john_androni...@hotmail.comwrote:
>hijkl wrote:
On Mar 7, 1:09 am, "hijkl" <s_mah...@yahoo.comwrote:
>>On Mar 7, 12:07 am, John Harrison <john_androni...@hotmail.comwrote:
>>>hijkl wrote:
>>>Your main misunderstanding seem to be that you think this code
>>>int *array(int n){
return new int(n);
}
allocates n integers.
[snip]
John
but it only allocates one integer, then initialize memory next to
current allocated memory. correct?
isnt it undefined behaviour? because memory you are trying to
initialize is not allocated by you.
If this is the case then how can we access that memory without
allocating it?
for example
int *p = new int(2);
_________
| 2 |
|_________|
p = 1233724
so it will allocate one memory location of type integer and assign
value to it.
then why we are using p as a array i.e. p[0],p[1]...
i am really confused and dont understand this concept cleary..sorry
about that but i need to clearify it :)

so p[1] is equal to value at p++ right i.e. value at address 1233728
correct?
u understand my confusion?? if yes then help me out :)
thanks guys
Sanjay, it works like this:

int *p = new int(2);

allocates _one_ int's worth of memory, assigns the pointer p to point to
that area of memory, and initialises the _contents_ of that area of
memory (which you access in your code as *p) to 2.

Memory locations beyond that pointed to by p - for example the locations
pointed to by p+1, p+2, etc. - are _not_ allocated; hence if you attempt
to access the contents of any such location, eg. by using *(p+1) in your
code, then you invoke undefined behaviour; basically p+1 could point to
some area of memory already allocated to something else in your program,
or to an area of system memory, or to nowhere in particular. It doesn't
make sense to reference it.

The next thing to note is that p[0] is basically the same as *p, p[1] is
basically the same as *(p+1) and so on; so in this scenario, where you
have allocated only one int, you can use p[0] in your program, but not
p[1], p[2], etc.; they reference unallocated memory and invoke undefined
behaviour.

Now... contrast the above with:

int *p = new int[2];

This does something completely different: it allocates _two_ int's worth
of memory and assigns the pointer p to point to the first int. [Aside:
note that "in general" you can't assume that the _contents_ of memory
allocated by "new" in this way have been initialised to anything in
particular - in the case of int, in fact, you can; an int will always be
initialised to zero].

Now, because we have allocated two ints the locations pointed to by p and
p+1 are allocated - this is because using "new" as above always allocates
what is known as "contiguous" memory; that is, the locations allocated lie
at consecutive addresses. But note that p+2, p+3, etc. have still not been
allocated. So now it is perfectly ok to refer in your program to *p or
*(p+1) or, equivalently, to p[0] and p[1] - but it is not ok to refer to
*(p+2), *(p+3) or, equivalently, to p[2], p[3], ... these locations are
unallocated and referencing them will invoke undefined behaviour as before.

Hope this helps - a good book should explain it better than I can.

--
Lionel B
Mar 8 '07 #27

P: n/a
On 8 Mar, 11:38, Lionel B <m...@privacy.netwrote:
int *p = new int[2];

This does something completely different: it allocates _two_ int's worth
of memory and assigns the pointer p to point to the first int. [Aside:
note that "in general" you can't assume that the _contents_ of memory
allocated by "new" in this way have been initialised to anything in
particular - in the case of int, in fact, you can; an int will always be
initialised to zero].
No it won't.

int* p = new int[2];

allocates you space for two ints, but they are uninitialised. If you
want to zero-initialise them, do

int* p = new int[2]();

Gavin Deane

Mar 8 '07 #28

P: n/a
On Thu, 08 Mar 2007 04:17:11 -0800, Gavin Deane wrote:
On 8 Mar, 11:38, Lionel B <m...@privacy.netwrote:
> int *p = new int[2];

This does something completely different: it allocates _two_ int's worth
of memory and assigns the pointer p to point to the first int. [Aside:
note that "in general" you can't assume that the _contents_ of memory
allocated by "new" in this way have been initialised to anything in
particular - in the case of int, in fact, you can; an int will always be
initialised to zero].

No it won't.

int* p = new int[2];

allocates you space for two ints, but they are uninitialised.
Oops, my bad. I was thinking of POD data types being initialised as in C,
but of course they're not initialised at all there.
If you want to zero-initialise them, do

int* p = new int[2]();
Yup (and it's always irked me that you can't do:

int* p = new int[2](42);

for example).

--
Lionel B
Mar 8 '07 #29

This discussion thread is closed

Replies have been disabled for this discussion.