Size in bytes of integer types

I have the following code. The output for the first 2 is 4. The output for the last is 8. Why is a long integer the same size of a normal int (4 bytes)?
I am running a PC with 64-bit Windows 10 OS.


1
2
3
4
5
6
7
8
int intIncrement = 0;
cout << "sizeof(intIncrement): " << sizeof(intIncrement) << endl;

long lngIncrement = 0;
cout << "sizeof(lngIncrement): " << sizeof(lngIncrement) << endl;

long long lnglngIncrement = 0;
cout << "sizeof(lnglngIncrement): " << sizeof(lnglngIncrement) << endl;
The standard only guarantees the following relation:
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

(I can't remember ATM if long long is standard. Perhaps someone could correct me on that.)
See http://en.cppreference.com/w/cpp/language/types
Properties
The following table summarizes all available integer types and their properties:

And
Data models
The choices made by each implementation about the sizes of the fundamental types are collectively known as data model. Four data models found wide acceptance

PC with 64-bit Windows 10 OS: Data model is LLP64 (4/4/8)
The truth is that my question has to do with pointers. Sometimes I need to increment a pointer by 8, sometimes by 4, and sometimes by any value. How can I do this operation safely so my code runs fine in Windows 10 and Unix? I have to do it this way because I am working with a block of memory that has 8-byte integers, 4-byte integers and n-byte data.
Last edited on
closed account (E0p9LyTq)
A pointer will always be the same size no matter what the data type being pointed to happens to be.

1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
   std::cout << sizeof(char) << ", " << sizeof(char*) << "\n"; 
   std::cout << sizeof(short) << ", " << sizeof(short*) << "\n"; 
   std::cout << sizeof(long long) << ", " << sizeof(long long*) << "\n"; 
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstdint>

template < typename T > void portably_increment( const T* ptr )
{
     if(ptr)
     {
         std::uintptr_t ptr_value = reinterpret_cast<std::uintptr_t>(ptr) ;
         std::cout << ptr << ' ' << std::hex << std::showbase << ptr_value << '\n' ;

         ++ptr ; // increment pointer (points to next long value in array)
         ptr_value += sizeof(*ptr) ; // advance numeric value of pointer 

         std::cout << ptr << ' ' << ptr_value << "\n\n" ;
     }
}

int main()
{
    int a[5] {} ;
    portably_increment(a) ;

    long b[10] {} ;
    portably_increment(b) ;

    long long c[15] {} ;
    portably_increment(c) ;
}


LP64: http://coliru.stacked-crooked.com/a/89b4c4f29fd3c8f2
ILP32: http://rextester.com/CTS84514
I really appreciate your feedback, but I am still confused (I am a completely C++ beginner and pointers are driving me crazy, please be patient). I should have described my issue exactly since the beginning.

I have a portion of memory allocated with "malloc" (the use of malloc is a requirement). In this portion I must define the following:
* 8 bytes for an integer.
* 4 bytes for an integer.
* 4 bytes for an integer.
* n bytes for data.

This structure repeats several times in the portion of memory allocated with "malloc".

Initially, the memory block is of type *void:

1
2
void *MemPtr;
MemPtr = malloc(1024);


To set a value (500 for example) for the first 8-byte integer, maybe I can cast MemPtr to (long long) as follows:

 
*((long long*)MemPtr) = 500;


How can I set the value for the second and third 4-byte integers? I guess my question translates to, how can I calculate the address of the second and third 4-byte blocks? or how do I add n-bytes to an address to calculate the next address?

I should "skip" the last "n bytes" of data and start again with the next block of one 8-byte integer, one 4-byte integer and one 4-byte integer, skip the "n bytes" of data and start with the next block, and so on.

Something that I found is that if I cast "MemPtr" to "*bool" and add 1, then I advance byte by byte. It works but, is it valid?

1
2
3
4
5
6
7
8
9
void *MemPtr;
MemPtr = malloc(256);

for (int i = 0; i < 256; i++)
{
    cout << i << " - Address of MemPtr: " << (((bool*)MemPtr) + i) << endl;
}

free(MemPtr);


Regards.
Last edited on
Something like this, perhaps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cstdint>
#include <climits>
#include <cstdlib>

struct memory_block // this takes care of alignment requirements
{
    static_assert( CHAR_BIT == 8, "requires that byte is an octet" ) ;

    std::int64_t first ; // 8 bytes
    std::int32_t second ; // 4 bytes
    std::int32_t third ; // 4 bytes

    static constexpr std::size_t n = 112 ;
    unsigned char rest[n] ; // n bytes
};

void set_values( void* p, std::size_t n ) // invariant: p != nullptr
{
    auto pmb = static_cast<memory_block*>(p) ;

    for( std::size_t i = 0 ; i < n ; ++i )
    {
        memory_block& block = pmb[i] ;

        block.first = i ;
        block.second = i+1 ;
        block.third = i+2 ;
    }
}

int main()
{
    std::size_t n = 100 ;
    void* pv = std::malloc( sizeof(memory_block) * n ) ;
    if(pv) set_values( pv, n ) ;
}
Topic archived. No new replies allowed.