anything wrong with this code?

H

hijkl

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

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?


yea make sense :)..
 
J

John Harrison

hijkl said:
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.

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

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
 
H

hijkl

hijkl said:
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 = 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;
}

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
 
J

John Harrison

hijkl said:
hijkl said:
On Mar 7, 1:09 am, "hijkl" <[email protected]> wrote:
On Mar 7, 12:07 am, John Harrison <[email protected]> wrote:
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.

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

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
 
J

John Harrison

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
 
N

Nick Keighley

Erik said:
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>
 
L

Lionel B

[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.
 
G

Gavin Deane

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
 
L

Lionel B

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).
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top