473,320 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

64-bit puzzle

The computer industry is changing, and 64-bit technology is the next,
inevitable step.
Look at sample.

int main(int, const char **) {
int a = -2;
unsigned b = 1;
int array[5] = { 1, 2, 3, 4, 5 };
int *ptr = array + 3;
ptr = ptr + (a + b); //Access violation on 64-bit platform
printf("%i\n", *ptr);
return 0;
}

Do you know why this code does not work on 64-bit system? Do you
assured, what no similar errors founds in your code? Do you know other
typical errors reduce to down state C++ code on 64-bit platforms? If
is not present then visit site www.Viva64.com.
On this site you can find plenty information, devoted to programming
of 64-bit systems. And also static analyzer Viva64 for detects
multitude 64-bit portability issues.

Feb 3 '07 #1
7 1446
Discrepancy is admitted:
ptr = ptr + (a + b); - Invalid pointer value on 64-bit platform
printf("%i\n", *ptr); - Access violation on 64-bit platform

Feb 3 '07 #2
ka********@gmail.com wrote:
The computer industry is changing, and 64-bit technology is the next,
inevitable step.
Look at sample.

int main(int, const char **) {
int a = -2;
unsigned b = 1;
int array[5] = { 1, 2, 3, 4, 5 };
int *ptr = array + 3;
ptr = ptr + (a + b); //Access violation on 64-bit platform
printf("%i\n", *ptr);
return 0;
}

Do you know why this code does not work on 64-bit system?
For the same reason it does not work on 32 bit systems: the sum a+b is
subject to the conversion rules. Since b is of unsigned type, a is
converted to unsigned type [5/9]. This is undefined behavior for negative
values of a [5/5]. If it appears to work on your platform, then it was just
by coincidence.

Do you assured, what no similar errors founds in your code?
Yes.

Do you know other
typical errors reduce to down state C++ code on 64-bit platforms?
No. However, I would surely hope that the above does not qualify as a
typical mistake. Also, it is a mistake on any platform, not just 64 bit
platforms.
If is not present then visit site www.Viva64.com.
This sentence does not parse.
On this site you can find plenty information, devoted to programming
of 64-bit systems. And also static analyzer Viva64 for detects
multitude 64-bit portability issues.
Oops, are you a spammer by any chance?
Best

Kai-Uwe Bux
Feb 3 '07 #3
Kai-Uwe Bux wrote:
ka********@gmail.com wrote:
>The computer industry is changing, and 64-bit technology is the next,
inevitable step.
Look at sample.

int main(int, const char **) {
int a = -2;
unsigned b = 1;
int array[5] = { 1, 2, 3, 4, 5 };
int *ptr = array + 3;
ptr = ptr + (a + b); //Access violation on 64-bit platform
printf("%i\n", *ptr);
return 0;
}

Do you know why this code does not work on 64-bit system?

For the same reason it does not work on 32 bit systems: the sum a+b is
subject to the conversion rules. Since b is of unsigned type, a is
converted to unsigned type [5/9]. This is undefined behavior for negative
values of a [5/5]. If it appears to work on your platform, then it was
just by coincidence.
Oops, turns out I misidentified the source of undefined behavior: a+b is not
undefined because of [4.7/2] (sometimes there are more guarantees in the
standard than I am used to expect:-). So what should happen is a+b is
evaluated to unsigned(-2) + unsigned(1), which is the same as unsigned(-1).
Then this huge number is added to ptr. The undefined behavior actually
stems from [5.7/5].

>
>Do you assured, what no similar errors founds in your code?

Yes.

>Do you know other
typical errors reduce to down state C++ code on 64-bit platforms?

No. However, I would surely hope that the above does not qualify as a
typical mistake. Also, it is a mistake on any platform, not just 64 bit
platforms.
>If is not present then visit site www.Viva64.com.

This sentence does not parse.
>On this site you can find plenty information, devoted to programming
of 64-bit systems. And also static analyzer Viva64 for detects
multitude 64-bit portability issues.

Oops, are you a spammer by any chance?
Best

Kai-Uwe Bux
Feb 3 '07 #4
* Kai-Uwe Bux:
ka********@gmail.com wrote:
>The computer industry is changing, and 64-bit technology is the next,
inevitable step.
Look at sample.

int main(int, const char **) {
int a = -2;
unsigned b = 1;
int array[5] = { 1, 2, 3, 4, 5 };
int *ptr = array + 3;
ptr = ptr + (a + b); //Access violation on 64-bit platform
printf("%i\n", *ptr);
return 0;
}

Do you know why this code does not work on 64-bit system?

For the same reason it does not work on 32 bit systems: the sum a+b is
subject to the conversion rules. Since b is of unsigned type, a is
converted to unsigned type [5/9]. This is undefined behavior for negative
values of a [5/5].
I'd say "chapter and verse, please", except that seemingly you provided
that -- except that AFAICS the references given don't correspond to
anything in the standard.

Anyway, the signed->unsigned conversion is well-defined, §4.5/1 -§4.7/2.

(a + b) then yields -1 + 2^n, where n is the number of value
representation bits in 'unsigned', and the rhs of the assignment ends up
with an undefined value, which yields Undefined Behavior when assigned
to 'ptr'.

If it appears to work on your platform, then it was just
by coincidence.
Yep.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 3 '07 #5
Alf P. Steinbach wrote:
* Kai-Uwe Bux:
>ka********@gmail.com wrote:
>>The computer industry is changing, and 64-bit technology is the next,
inevitable step.
Look at sample.

int main(int, const char **) {
int a = -2;
unsigned b = 1;
int array[5] = { 1, 2, 3, 4, 5 };
int *ptr = array + 3;
ptr = ptr + (a + b); //Access violation on 64-bit platform
printf("%i\n", *ptr);
return 0;
}

Do you know why this code does not work on 64-bit system?

For the same reason it does not work on 32 bit systems: the sum a+b is
subject to the conversion rules. Since b is of unsigned type, a is
converted to unsigned type [5/9]. This is undefined behavior for negative
values of a [5/5].

I'd say "chapter and verse, please", except that seemingly you provided
that -- except that AFAICS the references given don't correspond to
anything in the standard.
They do:

[5/9]:
Many binary operators that expect operands of arithmetic or enumeration type
cause conversions and yield result types in a similar way. The purpose is
to yield a common type, which is also the type of the result. This pattern
is called the usual arithmetic conversions, which are defined as follows:
[...]
? Otherwise, if either operand is unsigned, the other shall be converted to
unsigned.

[5/5]:
If during the evaluation of an expression, the result is not mathematically
defined or not in the range of representable values for its type, the
behavior is undefined, unless such an expression is a constant expression
(5.19), in which case the program is ill-formed.
Probably the trickyness is that all of these come even before [5.1].
Anyway, the signed->unsigned conversion is well-defined, §4.5/1 -§4.7/2.
You are correct on that one, see my self-correction elsethread.
(a + b) then yields -1 + 2^n, where n is the number of value
representation bits in 'unsigned', and the rhs of the assignment ends up
with an undefined value, which yields Undefined Behavior when assigned
to 'ptr'.
True, I also realized that afterwards.

>If it appears to work on your platform, then it was just
by coincidence.

Yep.

Thanks

Kai-Uwe Bux
Feb 3 '07 #6
Pan
ka********@gmail.com wrote:
ptr = ptr + (a + b); //Access violation on 64-bit platform
printf("%i\n", *ptr);
I found the same problem on the code I'm working on.
It's because (a+b) is unsigned and usually they're still 32 bits wide,
but the pointers are 64 bits wide. You're actually ADDING a big
(>4billions) number rather than subtracting a small amount.
This worked just by coincidence on most older architectures because
pointers and unsigned were the same bits wide (they're usually both 32).

--
Marco
Feb 3 '07 #7
By the way, the similar example uses at lectures Kang Su Gatlin. Kang
Su Gatlin is a program manager on the Visual C++ team and recently
spoke as part of the Route 64 Training Tour. Here it is possible to
look its small performance: http://channel9.msdn.com/ShowPost.aspx?PostID=53939

Feb 6 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marc Poinot | last post by:
Did anybody use numarray on a 64 bits platform ? (e.g. SGI/Irix) The package works on our 64 bits platforms... using 32 bits mode, but not using 64 mode. Big crash in _ufunc... Is there a flag to...
1
by: Hugo | last post by:
I have a dual boot machine, runs Win XP pro and Win XP Pro 64, XP boots from C and XP 64 boots from D. They both have VS 2005 Beta 2 installed. I have a webapp developed in Win XP (32) which...
3
by: Odd Bjørn Andersen | last post by:
I was asked to install db2 udb workgroup edition (version 7.2) 64-bits on AIX. But I cannot find that we have a CD with that software. Only Enterprise Edition. Is it correct that you have to...
56
by: Dave Vandervies | last post by:
I just fixed a bug that some of the correctness pedants around here may find useful as ammunition. The problem was that some code would, very occasionally, die with a segmentation violation...
0
by: Hugo | last post by:
I have a dual boot machine, runs Win XP pro and Win XP Pro 64, Win XP boots from C and Win XP 64 boot from D. They both have VS 2005 Beta 2 installed. I have a webapp developed in Win XP (32)...
3
by: some one | last post by:
I have kind of wired problem, I using httpwebrequest to post form data to server , in the GetResponse stage a WebException occurred, after tracing the actual error that occurs on the server, I...
1
by: mel_apiso | last post by:
Hi, we have an AIX 5.3 OS, and we purchased DB2 UDB version 8 Workgroup Edition. We want to install 64 bits version, but the source CD's that we have say: WORKGROUP SERVER EDITION Version...
13
by: Mary Lei | last post by:
Does anyone know the link to obtain the tarball for db2 8.1 for solaris running on AMD 64 bit ? This is the entire db2 installation on a new system that does not have db2. Thanks.
10
by: krunalb | last post by:
Hi, I am trying to shift unsigned long long value by 64 bits and this is what i get #include <stdio.h> int main() { unsigned short shiftby= 64;
1
by: =?Utf-8?B?UGF1bCBQaGlsbGlwcw==?= | last post by:
I have read many things about this but I haven't got a clear vision on what to do if anything about this. I have a system that tries to find holes in my web site. One of the things it has...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.