pointer arithmatic

J

junky_fellow

Can I subtract two pointers of same type that are pointing to the two
different location of memory allocated by malloc.

eg.
#include <stdlib.h>
int main(void)
{

unsigned char *c_ptr;
unsigned char *c_ptr1;
unsigned char *c_ptr2;
int diff;

c_ptr = malloc(100 * sizeof(char));
c_ptr1 = c_ptr + 5;
c_ptr2 = c_ptr + 10;
diff = c_ptr2 - c_ptr1; /* Is it valid ? */
}
Is the subtraction ( c_ptr2 - c_ptr1 ) valid ?
Is it guaranteed that "diff" will always be 5 ?
 
R

Robert Gamble

Can I subtract two pointers of same type that are pointing to the two
different location of memory allocated by malloc.

Yes, as long as they both point to some place in the same object
allocated by malloc.
eg.
#include <stdlib.h>
int main(void)
{

unsigned char *c_ptr;
unsigned char *c_ptr1;
unsigned char *c_ptr2;
int diff;

c_ptr = malloc(100 * sizeof(char));
c_ptr1 = c_ptr + 5;
c_ptr2 = c_ptr + 10;
diff = c_ptr2 - c_ptr1; /* Is it valid ? */

Missing return statement here.
}
Is the subtraction ( c_ptr2 - c_ptr1 ) valid ?
Yes.

Is it guaranteed that "diff" will always be 5 ?

Yes.

Robert Gamble
 
B

Bob

Well, at least your answer is concise. Were it but also correct. The
integer difference between two pointers may be meaningless, but pointer
subtraction is always valid.
 
S

S.Tobias

[please don't snip context and attributions]

Bob said:
Well, at least your answer is concise. Were it but also correct. The
integer difference between two pointers may be meaningless, but pointer
subtraction is always valid.

No, not always. Pointer difference is defined only for arrays.

struct s { int a, b; } s;
int *p1, *p2;
p1 = &s.a;
p2 = &s.b;
p2 < p1; //valid, false
p2 - p1; //invalid, UB
 
K

Keith Thompson

CBFalconer said:

That's a correct answer to the question as stated, but the sample code
indicates that he was really asking about two different pointers into
the same malloc()ed object. The answer to the question he *meant* to
ask is yes.
 
K

Kevin D. Quitt

Is it guaranteed that "diff" will always be 5 ?

Given the other conditions noted in this thread, yes, no matter what size object the
pointers are referring to. ptr +5 adds 5 to the pointer if it's char pointer, but would
add 10 is it's a pointer to (16 bit) short. The difference will always reflect the index
values, but the actual difference in memory address.
 
C

CBFalconer

Bob said:
Well, at least your answer is concise. Were it but also correct.
The integer difference between two pointers may be meaningless,
but pointer subtraction is always valid.

Since you didn't bother to quote anything, all I have to go on is
your erroneous statement above. Pointer subtraction is only valid
between two pointers to the same object, or portions of that
object. It always returns an integer, of the signed type ptrdiff_t
as defined in <stddef.h>.

So, in general, pointer subtraction serves no useful purpose.
 
K

Keith Thompson

CBFalconer said:
Since you didn't bother to quote anything, all I have to go on is
your erroneous statement above. Pointer subtraction is only valid
between two pointers to the same object, or portions of that
object. It always returns an integer, of the signed type ptrdiff_t
as defined in <stddef.h>.

So, in general, pointer subtraction serves no useful purpose.

Unfortunately, the phrase "in general" is a remarkably efficient way
to generate ambiguous sentences.

Pointer subtraction is useful in some cases, and is not useful (and in
fact invokes undefined behavior) in others. Do you agree?

(And you should add "or just past the end of the object" above.)
 
G

glen herrmannsfeldt

Can I subtract two pointers of same type that are pointing to the two
different location of memory allocated by malloc.

(snip example)

If they were returned by the same malloc(), or are pointers to the
same static or automatic array, and, if cast, have been cast to the
same type. You can do some interesting things...

char a[1000], *b, *c;
int *i, *j;
b=a+3;
c=a+9;
i=(int*)a;
j=(int*)b;

now, what do you expect for j-i?

(even though it shouldn't matter, assume a processor without
restrictions on alignment.)

-- glen
 
Z

Zoran Cutura

glen herrmannsfeldt said:
Can I subtract two pointers of same type that are pointing to the two
different location of memory allocated by malloc.

(snip example)

If they were returned by the same malloc(), or are pointers to the
same static or automatic array, and, if cast, have been cast to the
same type. You can do some interesting things...

char a[1000], *b, *c;
int *i, *j;
b=a+3;
c=a+9;
i=(int*)a;
j=(int*)b;

now, what do you expect for j-i?

(even though it shouldn't matter, assume a processor without
restrictions on alignment.)

I suppose "3 for sizeof(int) == 2" but I might be wrong.
But what if sizeof(int) is 4? I think the behaviour is undefined.
The result of i-j would not be representable in a ptrdiff_t object and
therefor we'ld get nasal deamons.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top