Pointer Increment

R

ranjeet.gupta

Dear All,

Which is correct ?

Let suppose we allocate the memory for the 100 bytes,

int *PtrMemoryBlock = (int *)malloc(100);

If I do

PtrMemoryBlock = PtrMemoryBlock + 10;

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?

Thanks in advance
Ranjeet
 
R

Richard Bos

Which is correct ?

Neither, as usual.
Let suppose we allocate the memory for the 100 bytes,

int *PtrMemoryBlock = (int *)malloc(100);

This is not the right way to call malloc().
If I do

PtrMemoryBlock = PtrMemoryBlock + 10;

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?

No. It will point to the 10th int.

Richard
 
R

Robben

Dear All,

Which is correct ?

Let suppose we allocate the memory for the 100 bytes,

int *PtrMemoryBlock = (int *)malloc(100);

If I do

PtrMemoryBlock = PtrMemoryBlock + 10;

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?
It will point to 10th integer location.
int *PtrMemoryBlock = (int *)malloc(100); Assume PtrMemoryBlock = 0.
PtrMemoryBlock = PtrMemoryBlock + 10;
After the above operation PtrMemoryBlock will be 40.
10 * 4 = 40th byte location.
4 is the size of the integer.
10 is the increment value
 
K

kernelxu

(e-mail address removed) write:
will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?
neither ,
it points to memory (PtrMemoryBlock just points to) loaction of Nth
byte,
and, N = 10 * sizeof(int).
pointer "PtrMemoryBlock" is declared to point to int type, so while
doing an addition of 10 to the pointer , 10 * sizeof(int) is added in
fact.
 
R

ranjeet.gupta

Robben said:
It will point to 10th integer location.
After the above operation PtrMemoryBlock will be 40.
10 * 4 = 40th byte location.
4 is the size of the integer.
10 is the increment value

Here is the code

int main ()
{
int *MemoryBlockPtr = malloc(100);
printf("%p\n", MemoryBlockPtr);
MemoryBlockPtr = MemoryBlockPtr + 10;
printf("%p\n", MemoryBlockPtr);
return 0;
}

Thanks for your explanation as I was also wondering about the above
40, to figure out mathematically, I was able to conclude the above
conclusion, (Which you to stated)

Now as you have said and seems to me that you are confirm, then,
please let me know why multiplication is done with the size ?

Now If the normal pointer of the array is incremented then there
is no such multiplication.

Anyway Looks to me that I am missing some thing ? Please clarify.

Can we fix and point to the 10th int what is the correction factor,
Is this Universially Accepted (Correction factor is there) ????

Regards
Ranjeet
 
S

Suman

Here is the code

#include said:
int main ()
{
int *MemoryBlockPtr = malloc(100);

If you are trying to allocate room for say 100 integers, say so:
int *MemoryBlockPtr = malloc( 100 * sizeof *MemoryBlockPtr );
printf("%p\n", MemoryBlockPtr);

%p requires you to pass a pointer to void, so:
printf("%p\n", (void *)MemoryBlockPtr);
MemoryBlockPtr = MemoryBlockPtr + 10;
printf("%p\n", MemoryBlockPtr);

printf("%p\n", (void *)MemoryBlockPtr);
return 0;
}

Thanks for your explanation as I was also wondering about the above
40, to figure out mathematically, I was able to conclude the above
conclusion, (Which you to stated)

Now as you have said and seems to me that you are confirm, then,
please let me know why multiplication is done with the size ?

For a type T,
T array[HUGE];
T *pt = array;
pt = pt + int_offset;
is read by the compiler as
pt = pt + int_offset * sizeof *pt;
Now If the normal pointer of the array is incremented then there
is no such multiplication.

What is a normal pointer?
 
R

Robben

Here is the code

int main ()
{
int *MemoryBlockPtr = malloc(100);
printf("%p\n", MemoryBlockPtr);
MemoryBlockPtr = MemoryBlockPtr + 10;
printf("%p\n", MemoryBlockPtr);
return 0;
}

Thanks for your explanation as I was also wondering about the above
40, to figure out mathematically, I was able to conclude the above
conclusion, (Which you to stated)

Now as you have said and seems to me that you are confirm, then,
please let me know why multiplication is done with the size ?

Now If the normal pointer of the array is incremented then there
is no such multiplication.
It will be the same... as array also holds location the pointer
location.
Try out!!
It completely depends on what array is holding
For char/unsigned char there will not be any multiplication as it char
size is 1.
for short it will be multiplied by 2 and so on..

Please look into Sumans comments as there is something wrong in the
sample code provided
 
A

ahmed.mubin

It will point to
(initial_address)+(num_times_to_increment)*(size_of_data_type)
 
R

ranjeet.gupta

(e-mail address removed) write:
neither ,
it points to memory (PtrMemoryBlock just points to) loaction of Nth
byte,
and, N = 10 * sizeof(int).
pointer "PtrMemoryBlock" is declared to point to int type, so while
doing an addition of 10 to the pointer , 10 * sizeof(int) is added in
fact.

Thanks to all for your quick Response, Well I cleared doubts
while going thourgh the threads, Was bit confused, but I am clear :)
Well in one slot, I asked some REALLY Stupid Question :)

Reagrds
Ranjeet
 
F

Flash Gordon

It will point to
(initial_address)+(num_times_to_increment)*(size_of_data_type)

What will? I suggest you read the posts that are made almost daily to
this group or read CBFalconer's sig which is on all his posts and has
instructions. Had you actually read a few days posts as you are meant to
with ALL groups before posting you would have seen these instructions.
 
J

Joe Wright

Dear All,

Which is correct ?

Let suppose we allocate the memory for the 100 bytes,

int *PtrMemoryBlock = (int *)malloc(100);

If I do

PtrMemoryBlock = PtrMemoryBlock + 10;

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?

Thanks in advance
Ranjeet
I've read the responses to this post until now and I don't think they
answer your question. Here's my try. This is C and use of malloc()
requires..

#include <stdlib.h> /* malloc is prototyped here */

int *ip = malloc(100);

We don't cast malloc in C. Also, on my system an int has 4 bytes so I
would more naturally write this like..

int *ip = malloc(25 * sizeof (int));

Now you have allocated 100 bytes and later you may be required to 'free'
them with 'free(ip);' for example. When you do..

ip = ip + 10;

...two ugly things happen: You have overwritten the value of ip and so
you cannot free it later. The second thing, you have incremented ip by
10 ints or 40 bytes, not 10 bytes.

I only provide Clue here. I get paid for Solution. :)
 
D

Denis Kasak

Robben said:
>
It will point to 10th integer location.
Correct.

Assume PtrMemoryBlock = 0.

I would just like to remind that zero in pointer context is a symbolic
constant used to initialize pointers in such way that they are
guaranteed not to point to anything. Therefore, zero cannot be a valid
address of an object.
After the above operation PtrMemoryBlock will be 40.
10 * 4 = 40th byte location.
4 is the size of the integer.

Incorrect. The size of 'int' type in bytes is implementation defined and
may or may not be four. In any case, it is not safe to assume any value
for sizeof int.
10 is the increment value

-- Denis
 
D

Denis Kasak

Here is the code

int main ()

This should be int main(void).
{
int *MemoryBlockPtr = malloc(100);
printf("%p\n", MemoryBlockPtr);

You need to cast your pointer to (void*) before passing it to printf.
You also did not #include said:
MemoryBlockPtr = MemoryBlockPtr + 10;
printf("%p\n", MemoryBlockPtr);

Same as above.
return 0;
}

Thanks for your explanation as I was also wondering about the above
40, to figure out mathematically, I was able to conclude the above
conclusion, (Which you to stated)

Now as you have said and seems to me that you are confirm, then,
please let me know why multiplication is done with the size ?

Because of how pointer arithmetics is defined in C. Let's assume we have
an imaginary type 'foo'. A foo* pointer points to the first element of
an array of foo objects. If we wanted to advance the pointer to point to
the next object of the array, we would increment it by 1. But we want
the pointer to point to the next foo object, so we can't advance it by
just 1 byte, rather 1 * sizeof foo bytes.
Now If the normal pointer of the array is incremented then there
is no such multiplication.

What is a 'normal pointer' ? See the explanation above.
Anyway Looks to me that I am missing some thing ? Please clarify.

Can we fix and point to the 10th int what is the correction factor,
Is this Universially Accepted (Correction factor is there) ????

You do not need to fix anything. If you want to advance an int pointer
to point to the 10th int of an array, just increment it by 10.

-- Denis
 
N

Neil Kurzman

Robben said:
It will point to 10th integer location.
After the above operation PtrMemoryBlock will be 40.
10 * 4 = 40th byte location.
4 is the size of the integer.
10 is the increment value

Assuming ints are 4 bytes. Which is a poor assumption.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top