-
April 29th, 2004, 02:48 PM
#1
pointer question in C
hi
i have a question on pointer. suppose p is a dynamically allocated memory pointer. now if i do a p++ does it mean i will get p[0], p[1],p[2] etc ?
or in other words, does it mean
p[0]= p
p[1] = = p+sizeof(data_type) // this is basically p++
can you tell what is the relation between p++ and pointer indexes ?
thanks
-
April 29th, 2004, 04:18 PM
#2
Re: pointer question in C
Originally posted by TheMummy
hi
i have a question on pointer. suppose p is a dynamically allocated memory pointer. now if i do a p++ does it mean i will get p[0], p[1],p[2] etc ?
or in other words, does it mean
p[0]= p
p[1] = = p+sizeof(data_type) // this is basically p++
can you tell what is the relation between p++ and pointer indexes ?
thanks
p++ would be the same address as &p[1]
p[0] != p but *p
p[1] != p+sizeof(data_type) but rather *(p+1).
Also... p+sizeof(data_type) would only be the same address as p++ if data_type is char. sizeof(int) for example, is 4 (On a 32bit machine), and therefore p+sizeof(int) would be the same as &p[4];
-
April 29th, 2004, 04:28 PM
#3
The relation between pointer arithmetic and array index notations
are:
Let's say:
P[i] == *(P + i)
when i = 0:
P[0] == *(P + 0) == *P
thus:
&P[0] == &*P == P
P is a pointer (address). Pointers always point to something, to a type T. If P is a pointer to type T, then P++ will increase the pointer (memory address) by sizeof(T) bytes. You don't need to, and you shouldn't, increase the pointer by sizeof(T) bytes yourself.
-
April 29th, 2004, 04:40 PM
#4
in addition to Assmaster post,
you should keep the address of the allocation's beginning.
if you you want to use arithmetic on pointers, like p++,
then if p is the original one and only pointer the points
that dynamic allocation - make a "backup" pointer.
otherwise, you might encounter memory leak or undefined
behaviour when you free the memory with wrong address.
**** **** **** **** **/**
-
April 29th, 2004, 10:37 PM
#5
that means if i create a pointer like this
-------------------------------------
p-----> |p[0] | p[1] | p[2] | p[3]| p[4]|......so on
-------------------------------------
that means, if i create some memory dynamically by pointer p. then there is two acess rule
rule 1. i can use memory cells by simply array like things.
i.e p[0] = 5; p[1] = 3 // some value i can set in the cell
rule 2. i can use the memory place in other way also. i.e using real pointer arithmetic
i.e *(p+0) = 5; *(p+1) = 3 ....so on.
beisdes these two rule is there any other acess rule for using memory places ?
my problem is i am using the pointer as array index (like p[0],p[1] etc) which is exclusive for array .
i have no objection i write the follwing
int x[20] ;
int * p = new int [20];
p = x;
x[0] = 5, x[1] = 3 // no objection bcoz i am using array
i have objection in below
is it same if i write
p[0] = 5, p[1] = 3 // objection here . p is a pointer . how can i use it like array index !!
does the two are same thing ?
-
April 30th, 2004, 04:21 AM
#6
Yes, but don't do
Code:
int x[20];
int* p = new int[20];
p = x;
because you've just lost the pointer to your allocated memory block and consequently leaked memory. It now points to x. In this example, p is a block of heap memory, size 20 ints (an array of 20 ints), that you need to do
on at some point. x is a block of stack memory, size 20 ints (array of 20 ints), that will be released when x goes out of scope.
p points to the block of heap memory. You could say
Code:
int* pI = p;
pI[2] = 5;
*(pI + 2) = 5;
// or even
pI++;
pI is also a pointer to a block of ints in memory. It's OK to change the value of pI because you can still call
to release the memory later. Equally, you could say
and do the same operations. p and pI are different pointers, but you can set them to the same value, which means they're pointing to the same memory location.
Does that answer the question or are you asking how the [] operator works?
Some cause happiness wherever they go; others, whenever they go.
-
April 30th, 2004, 04:49 AM
#7
No, that was not my question.
int x[20] ;
int * p = new int [20];
p = x;
x[0] = 5, x[1] = 3
as you are saying i am leaking the memory here bcoz i am not using allocated memory . i am using statck memory alloted by int x[20] statically as i have assigned p = x .
so i think its a bad example to use dynamic memory allocation.
but i have seen to use this type of assignment(i.e p=x) and then using array index like above. so, it seems its not the proper usage of dynamic memory allocation. or does it ? as i am really not using the memory that i have created so its not a proper dynamic memory usage. or does it ?
or in other words i am putting my question in simple way. look the code below.
Code:
int * p = new int [20];
p[0] = 5;
p[1] = 3;
AND
Code:
int x[20]
int * p = new int [20];
p = x;
x[0] = 5;
x[1] = 3;
question 1 : are these two code same ? yes/No
probabily u may say No. bcoz they will consume different memory area . is it just for different memory space ? but they are doing the same thing !!!
question 2: which one is a better code between two codes ?
i would like to know ur comments.
thanks
-
April 30th, 2004, 04:56 AM
#8
It's absolutely fine to say
Code:
int x[20];
int *p = x;
x[0] = 5;
x[1] = 3;
Now, even though you haven't done anything with p other than assign it, p[0] == 5 and p[1] == 3.
x and p are now interchangeable. And with the added bonus that you haven't had to leak any memory.
T
Some cause happiness wherever they go; others, whenever they go.
-
April 30th, 2004, 05:49 AM
#9
ohh.. yes i understand now. thanks for the last code. that helped me to distinguish.
thanks
-
May 1st, 2004, 12:04 AM
#10
------------------------------------------------
int x[20] // stack
int * p = new int [20]; // Heap
p = x;
x[0] = 5;
x[1] = 3;
-------------------------------------------------
If u want to use Stack memory allocated by x[20] ,
why u r allocating memory on Heap by new int [20] ?
following is sufficient
----------------------------------------
int x[20] // stack
int * p; // No memory allocation
p = x;
x[0] = 5;
x[1] = 3;
----------------------------------------
Last edited by Guru_Narendra; May 1st, 2004 at 12:10 AM.
-
May 1st, 2004, 01:03 AM
#11
thanks narendra. good explanation.
-
May 2nd, 2004, 08:47 PM
#12
Also Be careful what you add if it is 2,4,8 byte intergers you will be iadding sometimes to much!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|