portability of int arrays from 32 to 64 bit systems


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers portability of int arrays from 32 to 64 bit systems
# 1  
Old 07-23-2009
portability of int arrays from 32 to 64 bit systems

Dear All,
I'm porting my scientific program from 32 to 64 bit system
(use gcc, CentOS) and feel myself absolutely confused
after reading general internet advices for that. Could you help me,
please.

The question is:
can I use "old style" on 64 bit:
--------
int * myIntArray;
myIntArray = malloc (sizeof(int)*100000);
for (i=0;i<100000;i++)
myIntArray [i] = i*i;
int j=250;
int b=myIntArray [j];
--------

or should I use a more complicated construction like
--------
for (i=0;i<100000;i++)
myIntArray [(size_t) (i)] = i*i;
int j=250;
int b=myIntArray [(size_t) (j)];
--------
in this simple case throughout the code? Or that all is wrong?
And what should I do with arrays of doubles?

Trivial compilation on 64 bit system of my old code written for 32 bits
gives an executable file which can run, but shows numerical instability, and I'm trying to find the reason...

Thank you very much in advance!
Regards,
German
# 2  
Old 07-23-2009
Quote:
Originally Posted by German1234
Dear All,
I'm porting my scientific program from 32 to 64 bit system
(use gcc, CentOS) and feel myself absolutely confused
after reading general internet advices for that. Could you help me,
please.

The question is:
can I use "old style" on 64 bit:
--------
int * myIntArray;
myIntArray = malloc (sizeof(int)*100000);
for (i=0;i<100000;i++)
myIntArray [i] = i*i;
int j=250;
int b=myIntArray [j];
--------

or should I use a more complicated construction like
--------
for (i=0;i<100000;i++)
myIntArray [(size_t) (i)] = i*i;
int j=250;
int b=myIntArray [(size_t) (j)];
--------
in this simple case throughout the code? Or that all is wrong?
And what should I do with arrays of doubles?

Trivial compilation on 64 bit system of my old code written for 32 bits
gives an executable file which can run, but shows numerical instability, and I'm trying to find the reason...

Thank you very much in advance!
Regards,
German
The 'old' way works fine. There's nothing wrong with integers as array indexes in any architecture C fully supports.

In my experience, a few common things cause problems when porting old code to 64-bit:
  • Undeclared functions or missing headers. On 32-bit, printf("hello\n"); will work in 32-bit even if you don't include stdio.h, since there's no effective difference between assuming a function takes an integer and assuming it takes a pointer. In 64-bit, pointers are 64-bit while integers(except long integers) are still 32-bit, resulting in the compiler shoehorning a 64-bit pointer into a assumed-to-be-32-bit integer parameter. Similarly, your example code will crash without stdlib.h since it will assume malloc returns a 32-bit int.
  • Mismatched headers. This is what made libfaad2 such a bear to port to 64-bit. It had two separate headers defining the same function in different places, one used internally, one used for external linking. One of them had a long parameter, the other had a uint32. On 32-bit they were effectively the same since int and long are the same size there. On 64-bit, it used 32-bit values internally and was exported as using long values, which had suddenly become 64-bit.
  • long values. A lot of code in the last 10 years has been written on the assumption that a long value is 32 bits, the same as an integer. In a 64-bit architecture that's obviously not true.
  • Pointer mangling. Like mismatched headers, but done explicitly. This generates lots of 'conversion of pointer to integer' warnings that C programmers quickly become accustomed to ignoring since putting a 32-bit pointer in a 32-bit integer is perfectly safe. In 64-bit, the integer may still be 32-bit but the pointer is definitely not.
So in summary, hunt for longs, don't get lazy about headers, and treat all warnings as errors unless you know for a fact the conversion in question is safe.

Last edited by Corona688; 07-23-2009 at 07:54 PM..
# 3  
Old 07-23-2009
Dear Corona688,

Thank you very much! That's really helpful!
Best Regards,
Gera
# 4  
Old 07-24-2009
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

2. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

3. Solaris

Convert DWORD to int on solaris for 64-bit

i am converting application from 32-bit to 64-bit on Solaris. How can I convert "DWORD" on Solaris 64-bit to int. Thanks. (1 Reply)
Discussion started by: amit_27
1 Replies

4. Shell Programming and Scripting

Script Portability

Hi, I am writing a BASH shell script for a client. I am using BASH on a Macintosh Powerbook G4 running Leopard. My client will be using BASH on a PC running Ubuntu. My questions all revolve around making my script portable so that it will run on his machine. - Do I need to get any other... (2 Replies)
Discussion started by: msb65
2 Replies

5. Shell Programming and Scripting

portability issue linux(2.6) solaris10

the following simple scripts work fine on linux but fail on solaris: #!/bin/bash eval /usr/bin/time -f \'bt=\"%U + %S\"\' ./JUNK >> ./LOG 2>&1 cp ./LOG ./LOG_joe LC_joe=`cat ./LOG | wc -l` LC_joe=`echo $LC_joe-1|bc` tail -1 ./LOG > ./tmp head -$LC_joe ./LOG_joe > ./LOG rm ./LOG_joe ... (1 Reply)
Discussion started by: joepareti
1 Replies

6. UNIX for Dummies Questions & Answers

Script portability

Hello, May I the right place if I need competencies to migrate DOS script into Unix platforme? Thanks in advance Thierry (9 Replies)
Discussion started by: tsconetti
9 Replies

7. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

8. Programming

difference between int ** func() and int *& func()

What is the difference between int** func() and int*& func(). Can you please explain it with suitable example. Thanks, Devesh. (1 Reply)
Discussion started by: devesh
1 Replies

9. Programming

A C program required for portability

I have to solve a problem for my wife who is engaged in Research in Breast Cancer. 1. She has frequently to search a long single line of alphabetic characters (lower case) for an exact match of a string. e.g.... (1 Reply)
Discussion started by: nmsinghe
1 Replies
Login or Register to Ask a Question