LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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
  Search this Thread
Old 11-23-2022, 05:58 AM   #1
Gollum21
LQ Newbie
 
Registered: Sep 2022
Location: Urbana, Illinios
Posts: 4

Rep: Reputation: 0
What is the size of the int and long types specified in the C++ standard?


I'm seeking for specific information on the sizes of basic C++ types. I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler.
But are there any C++ standards?
On a 32-bit architecture, I'm using Visual Studio 2008. This is what I get:
I looked for solid information on the sizes of char, short, int, long, double, float (and additional kinds I didn't think of) under different architectures and compilers, but had little luck and only got this [link moderated].

Last edited by astrogeek; 11-23-2022 at 02:07 PM. Reason: spammish link moderated
 
Old 11-23-2022, 08:13 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,736
Blog Entries: 13

Rep: Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784
The article you cited answers your question perfectly.

Sizeof on the system you are using.
 
Old 11-23-2022, 08:45 AM   #3
wainamoinen
LQ Newbie
 
Registered: Sep 2009
Location: Belgium
Distribution: Slackware
Posts: 25

Rep: Reputation: 13
The C and C++ standards don't define type sizes. Type sizes are compiler/architecture/OS/Data-model dependent. Take a look at this C data types and 64-bit_data_models

Now, for Visual C++, Microsoft has fixed sizes, probably to maximize compatibility between Windows versions. See Sizes of built-in types:

Most built-in types have implementation-defined sizes. The following table lists the amount of storage required for built-in types in Microsoft C++. In particular, long is 4 bytes even on 64-bit operating systems.

Code:
Type                                                             | Size
--------------------------------------------------------------------------
bool, char, char8_t, unsigned char, signed char, __int8          | 1 byte
char16_t, __int16, short, unsigned short, wchar_t, __wchar_t     | 2 bytes
char32_t, float, __int32, int, unsigned int, long, unsigned long | 4 bytes
double, __int64, long double, long long, unsigned long long      | 8 bytes
As mentioned before, just check the size of the type you are interested with:
Code:
printf("Size of int = %i\n", sizeof(int));
 
Old 11-23-2022, 08:59 AM   #4
wainamoinen
LQ Newbie
 
Registered: Sep 2009
Location: Belgium
Distribution: Slackware
Posts: 25

Rep: Reputation: 13
Sorry, I forgot to say, if you want an integer of an specific size you have to include the <stdint.h> header, there there are definitions for:
Code:
int8_t
uint8_t
int16_t
uint16_t
int32_t
uint32_t
int64_t
uint64_t
For float point values, almost all architectures/compilers use IEEE 754 standard
Code:
float   4 bytes
double  8 bytes
 
1 members found this post helpful.
Old 11-23-2022, 09:51 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,007
Blog Entries: 4

Rep: Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607
And to complete @wainamoinen's thought, you should use these definitions if you actually care about the "size of" an integer and want your code to be portable. (This also makes your intentions clear to the human reader ...)

In the case of struct, you also need to be aware of the possibility of padding. Unless you specify that the struct is to be "packed" into the minimal amount of space – and I don't believe that there is a standard way to do this (unlike Pascal) – the compiler might insert extra bytes in order to align the various struct members most advantageously for the hardware in question. (For example, highly-pipelined 64-bit architectures "like" for things to sit on 64-bit address boundaries because the pipeline works better that way.) Unless you specify that it mustn't do this, the compiler presumes that it is free to do so.

Last edited by sundialsvcs; 11-23-2022 at 09:54 AM.
 
Old 11-23-2022, 09:52 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,387
Blog Entries: 1

Rep: Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665
Note: In Windows, both of types WPARAM and LPARAM are 32/64 bit long (32 bit on x86, 64 bit on amd64), the first is unsigned, the latter is signed.

Last edited by NevemTeve; 11-23-2022 at 09:53 AM.
 
Old 11-23-2022, 12:12 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,701

Rep: Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062
The first three entries here should cover it:

https://c-faq.com/decl/index.html
 
Old 11-23-2022, 12:59 PM   #8
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 894

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
Optimizing the sizes of integer variables does not have much benefit on modern hardware.

I recommend using "long" by default (rather than "int") to preclude the possibility of overflow on 64-bit machines. In practice, "long" will never overflow on 64-bit machines.

The shorter types are useful only in specific circumstances:

* 32-bit int for pixel coordinates, RGBA values, and Unicode characters
* 16-bit short for audio data
* 8-bit char for strings

Ed
 
Old 11-23-2022, 01:00 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,701

Rep: Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062
And floats for GPUs
 
Old 11-23-2022, 01:02 PM   #10
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 894

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
Quote:
Originally Posted by dugan View Post
And floats for GPUs
Yes. Everything else should use "double".
Ed
 
Old 11-23-2022, 05:05 PM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,007
Blog Entries: 4

Rep: Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607
Probably the only situation where this would actually become important is when you need to match some externally-defined struct ... and you somehow don't have access to a public source-library that already does it for you, which is unlikely.

Vendor-defined type definitions such as "WPARAM" or "LPARAM" should always be followed when available, because their libraries make use of the same definitions, and they control those definitions. Above all, in your source code you should make your intentions crystal clear to the human reader: "say what you mean." (And if possible ... and this depends on the language you are using ... "have the compiler be able to catch your stpiud misteaks.") "Strong type-checking is a virtue."

Last edited by sundialsvcs; 11-23-2022 at 05:19 PM.
 
  


Reply

Tags
c++


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Question about long long int in C smag Programming 2 10-18-2011 04:50 PM
long long long: Too long for GCC Kenny_Strawn Programming 5 09-18-2010 02:14 AM
printf unsigned long long int? blackzone Programming 9 03-04-2008 01:41 PM
invalid types ‘int[int]’ for array subscript medha Programming 16 08-25-2006 09:30 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 10:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:51 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration