473,394 Members | 1,773 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,394 software developers and data experts.

anything wrong with this code?

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
28 2318
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öm

Mar 7 '07 #2
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
sorry but u misunderstood.
array is a function name i think ..

Mar 7 '07 #4
or i am not about array()

Mar 7 '07 #5
mimi
but that doesnt matters..its same thing wat u mentioned/

Mar 7 '07 #6
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
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
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
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
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
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
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
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
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
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
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
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
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öm

Mar 7 '07 #19
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
On Mar 7, 1:54 am, "Erik Wikström" <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öm- Hide quoted text -

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

Mar 7 '07 #21
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
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
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
>
>>
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
Erik Wikström 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris | last post by:
Could anyone please help me. IS there something wrong with it? char * function getString() { char myString; sprintf(myString, "hello world");
6
by: greenflame | last post by:
I have been working for some time on a script that will show a matrix with the elements aligned on the right for sometime and finally got it to work. Then I did some patching up and ran the script...
2
by: atv | last post by:
Hi, is there something wrong with this code? It seems that it crashes at the else { clause, when allocating a second record. Excuse for the indentation, the g_warnings are from gtk. Also, i'm...
3
by: Roubles | last post by:
Hi All, Here's my problem, I have a bunch of code that passes an allocated object (say obj) to a function, and then dereferences that allocated object when the function returns: foo(obj);...
4
by: QQ | last post by:
Hi Here is part of my code typedef struct{ int len; char code; }Code; typedef struct{ .... Code *a;
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
10
by: Jim Langston | last post by:
I use a game engine using MSVC++ .net 2003 and have no problems. Some users of DevC++ who use the same engine crash at times when a copy of this structure is the return variable. I don't have...
4
by: beebob | last post by:
Hi all, I have just changed my JDK, from 1.5.0 to 1.6.0, and also change my Netbeans, from NB 4.0 to NB 5.5. I've got a problem with my code, which I wrote on NB 4.0 + JDK 1.5.0.. I fail to put...
19
by: =?Utf-8?B?anZjb2FjaDIz?= | last post by:
I've got a global.aspx file that works in my dev environment (vs 2005). When i publish the site to a windows 2000 sp4 box running IIS, the global does not seem to fire. Since it's a test server,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.