Re: anything wrong with this code?
hijkl wrote:
On Mar 7, 1:27 pm, John Harrison <john_androni...@hotmail.com> wrote:
hijkl wrote:
On Mar 7, 1:09 am, "hijkl" <s_mah...@yahoo.com> wrote:
On Mar 7, 12:07 am, John Harrison <john_androni...@hotmail.com> 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.
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