Re: anything wrong with this code?
On Wed, 07 Mar 2007 21:06:24 -0800, 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:
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