LinuxQuestions.org
Visit Jeremy's Blog.
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, 04:58 AM   #1
Gollum21
LQ Newbie
 
Registered: Sep 2022
Location: Urbana, Illinios
Posts: 5

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 01:07 PM. Reason: spammish link moderated
 
Old 11-23-2022, 07:13 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

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

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

Rep: Reputation: 24
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, 07:59 AM   #4
wainamoinen
Member
 
Registered: Sep 2009
Location: Belgium
Distribution: Slackware
Posts: 32

Rep: Reputation: 24
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, 08:51 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,640
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
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 08:54 AM.
 
Old 11-23-2022, 08:52 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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 08:53 AM.
 
Old 11-23-2022, 11:12 AM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

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

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

Rep: Reputation: 469Reputation: 469Reputation: 469Reputation: 469Reputation: 469
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, 12:00 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

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

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

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
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 04: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 03:50 PM
long long long: Too long for GCC Kenny_Strawn Programming 5 09-18-2010 01:14 AM
printf unsigned long long int? blackzone Programming 9 03-04-2008 12:41 PM
invalid types ‘int[int]’ for array subscript medha Programming 16 08-25-2006 08:30 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM

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

All times are GMT -5. The time now is 02:42 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