LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-16-2007, 09:49 PM   #1
TruongAn
Member
 
Registered: Dec 2004
Location: Vietnam (Việt Nam)
Distribution: Gentoo (desktop), Arch linux (laptop)
Posts: 717

Rep: Reputation: 33
Anyone please show me the different between the code on a 64-bit system and 32-bit ?


Upto now the only different I can notice is the vary of the size of data type. At home I have a 64-bit CPU, running a 64-bit linux OS, my school equip with an also 64-bit CPU, but 32-bit Window version.
When I use dev-C++ on school's PC, runing the code
Code:
#include <iostream>
using namespace std;

int main () {
    cout << "size of char " << sizeof (char) << endl;
    cout << "size of int " << sizeof (int) << endl;
    cout << "size of short " << sizeof (short) << endl;
    cout << "size of long " << sizeof (long) << endl;
    cout << "size of float " << sizeof (float) << endl;
    cout << "size of double " << sizeof (double) << endl;
    cout << "size of long double " << sizeof (long double) << endl;
    cout << "size of wchar_t " << sizeof (wchar_t) << endl;
    cout << "size of int* " << sizeof (int *) << endl;
    int a;
}
I get:

Code:
size of char 1
size of int 4
size of short 2
size of long 4
size of float 4
size of double 8
size of long double 12
size of wchar_t 2
size of int* 4
On my home PC with GCC 4 and Fedora 8 64-bit, things are different.
I can remember that
size of int is still 4, but size of long is 8, size of long double is 16 and size of int* is 8.
I will post the details output here when I come home. And If I have time, I may post the output produce by Turbo C 3.0 running on ntvdm (nt virtual dos machine) as well.

But anyone please explain why do we get that different? how can we cope with it, I mean do I have to memorize all the size on every system?
Is there any more different that I have to notice?
 
Old 12-17-2007, 01:30 AM   #2
Simon Bridge
Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu 10.04
Posts: 9,196

Rep: Reputation: 190Reputation: 190
Quote:
the only different I can notice is the vary of the size of data type
But that's a pretty big difference. For instance, the address-space is bigger.

The only place you will really feel the difference is in machine code... which is as it should be.

http://en.wikipedia.org/wiki/64-bit

Technically you can have 64-bit instructions too... increasing the theoretical number of machine instructions available.
 
Old 12-17-2007, 01:40 AM   #3
violettheconqueror
Member
 
Registered: Sep 2005
Location: California
Distribution: Fedora 7
Posts: 44

Rep: Reputation: 15
There are all kinds of sneaky things this difference can bring about. From the various things i've picked up on my days browsing the gentoo AMD64 forums, the biggest improvements come in database access and encryption. This makes sense because these all crunch huge amounts of numbers.

As for C and C++ (I only know C at the moment) there are some ways to programm for any environment. For example, don't bother memorizing the length of a str on each system. Instead always use sizeof() this way if you need to port your system to different environment it is easy. If your worried about the processor overhead of calling the function everytime (which I imagine is nominal) you could set the double size or int size as a constant using sizeof(). The places where 32-64bit comes in are when you start bitshifting or masking. As it is if you need to use a single int for multiple purposes (like holding several different values) which I've done in the past, 64bit will allow you more room. sneaky sneaky
 
Old 12-17-2007, 04:46 AM   #4
TruongAn
Member
 
Registered: Dec 2004
Location: Vietnam (Việt Nam)
Distribution: Gentoo (desktop), Arch linux (laptop)
Posts: 717

Original Poster
Rep: Reputation: 33
Here is the size of some data types on my 64-bit system at home.
Compile the above code with g++
Target: x86_64-redhat-linux
gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)
Code:
size of char 1
size of int 4
size of short 2
size of long 8
size of float 4
size of double 8
size of long double 16
size of wchar_t 4
size of int* 8
so the question is why on my 64-bit box size of int is still 4, I think it sould be twice the size of int on 32-bit box?
Moreover, on the 32-bit box size of int and size of long is equal?

And if we looking forward, Visual C++ 6.0 compile that code and out put different from Dev-C++, on VC 6.0 size of long double is 8 (equal to double).
And if I use Turbo C on NT Virtual Dos machine, I get:

Code:
size of char 1
size of int 2
size of short 2
size of long 4
size of float 4
size of double 8
size of long double 10
//Turbo C doesn't support wchar_t
size of int* 2
Does it exist an explanation for all these above question? Do I have to memorize all these size on every system? And would I cope with that problem, may anything happen if I code something at home, it run fine, but when my teacher test it at school and things go wrong?
 
Old 12-17-2007, 04:13 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 1,421

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by violettheconqueror View Post
If your worried about the processor overhead of calling the function everytime (which I imagine is nominal) you could set the double size or int size as a constant using sizeof().
sizeof() is not a function, the compiler will replace sizeof() with the appropriate value when compiling.

Quote:
Originally Posted by TruongAn
Does it exist an explanation for all these above question? Do I have to memorize all these size on every system? And would I cope with that problem, may anything happen if I code something at home, it run fine, but when my teacher test it at school and things go wrong?
See http://en.wikipedia.org/wiki/C_varia...larations#Size
You should write your programs so that it doesn't matter what the exact size of each type is. You should always test it at school before your teacher marks it.
 
Old 12-17-2007, 05:05 PM   #6
Peatmoss
Member
 
Registered: Nov 2007
Location: Vancouver
Distribution: Ubuntu 7.10
Posts: 43

Rep: Reputation: 15
[QUOTE=TruongAn;2993155]Here is the size of some data types on my 64-bit system at home.
Compile the above code with g++
Target: x86_64-redhat-linux
gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)
Code:
size of char 1
size of int 4
size of short 2
size of long 8
size of float 4
size of double 8
size of long double 16
size of wchar_t 4
size of int* 8
so the question is why on my 64-bit box size of int is still 4, I think it sould be twice the size of int on 32-bit box?
Moreover, on the 32-bit box size of int and size of long is equal?
QUOTE]

Hi

I believe that there is no prescribed size for an integer in the C programming language, and I've always maintained that the size of an INT is processor-dependent. Whether your impression is correct or not doesn't really matter; a far better approach to any problem is to define your own types either as:

typedef int32 int,

or//

#define int32 int

And these definitions, along with others (e.g. you may have a variety of unsigned and signed integer types such as:

int8, int16, int32
uint8, uint16, uint32

and maybe even a bool that only takes 8 bits of storage instead of the usual 32. (Compiler dependent)

If you follow this approach you should put these definitions into a header file and establish a convention that EVERY source file must include this header file at a minimum. That way, your code becomes more portable; during testing, if you discover that int32 is no longer an int on your machine/compiler - i.e. maybe it's suddenly 8 bytes instead of 4, then you only need to rewrite your header file and then recompile the whole system. That should fix the problem.

Never depend upon the compiler's built-in types in "C" if it matters to your solution that the size of a variable must be 1, 2, 4 or 8 bytes.

My $.02

Regards

Peatmoss/Alleria
 
Old 12-17-2007, 07:43 PM   #7
Simon Bridge
Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu 10.04
Posts: 9,196

Rep: Reputation: 190Reputation: 190
TruongAn: what you are saying is that different compilers have different default data sizes. Why should this not be the case? You'll notice that wheel size is different on different cars...
 
Old 12-18-2007, 12:16 AM   #8
violettheconqueror
Member
 
Registered: Sep 2005
Location: California
Distribution: Fedora 7
Posts: 44

Rep: Reputation: 15
Quote:
sizeof() is not a function, the compiler will replace sizeof() with the appropriate value when compiling.
Thanks ntubski that is good to know.
 
Old 12-18-2007, 08:19 AM   #9
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Here is a good article which shows the problems and how to overcome them when coding for 32 and 64 bit systems. See the link to the author's site for more of the same.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I build 32-bit LFS on a 64-bit AMD system? dhave Linux From Scratch 4 07-23-2007 07:00 PM
JDK 1.6.0 32-bit and 64-bit on Same System (FC6)? sancho Fedora 3 01-23-2007 06:20 AM
no sound in 32 bit chroot on 64 bit system <solved> otchie1 Linux - Software 0 11-28-2006 05:03 PM
Getting Konqueror to load 32 bit plugins on a 64 bit system slantoflight Linux - Software 1 05-28-2006 05:17 PM
LXer: 32-bit browsing in a 64-bit system LXer Syndicated Linux News 1 05-26-2006 10:44 PM


All times are GMT -5. The time now is 02:52 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration