Velocity Reviews > C++ > Pointer arithmetic questions

# Pointer arithmetic questions

MOvetsky@gmail.com
Guest
Posts: n/a

 04-26-2005
Is the following code ISO C++ standard compliant?
If yes, is it guaranteed that it will not crash on compliant platforms?
If yes, will it print "Pointers are equal" on any compliant platform?
Will answers be the same if p points to local memory or string literal?

char *p = new char[10];
char *p1 = p-1;
p1 = p1 + 1;

if(p1 == p)
{
cout << "Pointers are equal" << endl;
}

Thank you,

Michael

Alvin
Guest
Posts: n/a

 04-26-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Is the following code ISO C++ standard compliant?
> If yes, is it guaranteed that it will not crash on compliant platforms?
> If yes, will it print "Pointers are equal" on any compliant platform?
> Will answers be the same if p points to local memory or string literal?
>
>
> char *p = new char[10];
> char *p1 = p-1;
> p1 = p1 + 1;
>
> if(p1 == p)
> {
> cout << "Pointers are equal" << endl;
> }
>
> Thank you,
>
> Michael

I would say this is standard compliant (without looking it up). That said,
using p1 before incrementing is undefined (obviously).

Someone correct me if I'm wrong but all that has been done is p1 is set to
1*sizeof(char) before p. I say 1*sizeof(char) because it depends on the
size of a char on the system. For most systems, sizeof(char) is 1; so p1 is
intialised to 1 byte before p. If you use int instead of char, then it
would be 4 bytes (on my 32bit machine) before p.

But you don't need to be concerned with the particulars really, That's the
beauty of pointer arithmetic!

--
Alvin

Pete Becker
Guest
Posts: n/a

 04-26-2005
Alvin wrote:
>
> I would say this is standard compliant (without looking it up).

Then you'd better look it up. <g> The rule for pointer arithmetic is
that you can move around within an array or to one position past the
end. You can't go in front of the beginning.

>
> For most systems, sizeof(char) is 1;
>

sizeof(char) is defined to be 1.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Alvin
Guest
Posts: n/a

 04-26-2005
Pete Becker wrote:

> Alvin wrote:
>>
>> I would say this is standard compliant (without looking it up).

>
> Then you'd better look it up. <g> The rule for pointer arithmetic is
> that you can move around within an array or to one position past the
> end. You can't go in front of the beginning.
>
>>
>> For most systems, sizeof(char) is 1;
>>

>
> sizeof(char) is defined to be 1.
>

I guess I didn't make myself clear. By standard compliant I was trying to
say p1 = p-1 was compliant. I didn't intend on saying the result was
compliant.

I guess what you are saying is the act of going in front of the array is
undefined.

--
Alvin

E. Robert Tisdale
Guest
Posts: n/a

 04-26-2005
(E-Mail Removed) wrote:

> Is the following code

> cat main.cc

#include <iostream>

int main(int argc, char* argv[]) {
char* p = new char[10];
char* p1 = p - 1;
p1 = p1 + 1;

if (p1 == p) {
std::cout << "Pointers are equal." << std::endl;
}

return 0;
}

> g++ -Wall -ansi -pedantic -o main main.cc
> ./main

Pointers are equal.

> ISO C++ standard compliant?

No.

> If yes, is it guaranteed that it will not crash on compliant platforms?

Who would guarantee that compliant code will not crash?

> If yes, will it print "Pointers are equal." on any compliant platform?

It *will* print "Pointers are equal." on *every* compliant platform
even though the code itself is non-compliant.
Nobody guarantees that non-compliant code *will* crash either.

> Will answers be the same if p points to local memory or string literal?

> cat main.cc

#include <iostream>

int main(int argc, char* argv[]) {
const
char* p = "Pointers are equal.";
char* p1 = p - 1;
p1 = p1 + 1;

if (p1 == p) {
std::cout << "Pointers are equal." << std::endl;
}

return 0;
}

> g++ -Wall -ansi -pedantic -o main main.cc

main.cc: In function `int main(int, char**)':
main.cc:6: error: \
invalid conversion from `const char*' to `char*'

Why couldn't you perform this simple experiment yourself?
and convince us that you are *not* just another lazy programmer.

James Aguilar
Guest
Posts: n/a

 04-26-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Is the following code ISO C++ standard compliant?

It's not compliant, but I'd honestly be amazed if you could find me a
compiler/runtime environment combination that failed to run it and give you the
output you expect.

- JFA1

BigBrian
Guest
Posts: n/a

 04-26-2005

Pete Becker wrote:
> Then you'd better look it up. <g> The rule for pointer arithmetic is
> that you can move around within an array or to one position past the
> end. You can't go in front of the beginning.
>

Your comment seems to imply that there's something checking the end
points of the array. There is not. You *can* go in front. You can
even do pointer arithmetic on a pointer that's not even pointing to an
array. It compiles, ( but that doesn't mean it doesn't produce
undefined behavior ).

-Brian

Andrew Koenig
Guest
Posts: n/a

 04-26-2005
"James Aguilar" <(E-Mail Removed)> wrote in message
news:d4m6c0\$pc2\$(E-Mail Removed)...
> <(E-Mail Removed)> wrote in message

> It's not compliant, but I'd honestly be amazed if you could find me a
> compiler/runtime environment combination that failed to run it and give
> you the output you expect.

There was an implementation a number of years ago that would terminate such
programs with a diagnostic message. However, I don't think that the company
that produced that implementation is still in business, nor do I know what
other implementations might behave the same way.

MOvetsky@gmail.com
Guest
Posts: n/a

 04-26-2005

E. Robert Tisdale wrote:

> Why couldn't you perform this simple experiment yourself?

Actually I checked it on Visual C++ 6.0 and gcc on Solaris and Linux
and got the expected results.
However, the point of my questions was to figure out if it was a safe
practice to use it on arbitrary platform and what ISO standard said

So from what I hear it is a bad idea to use the code like this for
multi platform development (even though the probability to get in
trouble is low).

Michael

Pete Becker
Guest
Posts: n/a

 04-26-2005
BigBrian wrote:

>
> Your comment seems to imply that there's something checking the end
> points of the array.

Nope.

> There is not. You *can* go in front. You can
> even do pointer arithmetic on a pointer that's not even pointing to an
> array. It compiles,

Not necessarily. The behavior is undefined, and that means you cannot
count on anything in particular. There is nothing in the language
definition that requires this code to compile. Don't use compilers to
test code conformance.

By the way, when most people say "you can't to that" there's an implied
"in conforming code."

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)