Example Code (C++ and Computer Architecture)#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream>
int main(int, char **)
{
std::cout << "unit is byte" << std::endl;
std::cout << "sizeof(char): " << sizeof(char) << std::endl;
std::cout << "sizeof(short): " << sizeof(short) << std::endl;
std::cout << "sizeof(int): " << sizeof(int) << std::endl;
std::cout << "sizeof(long): " << sizeof(long) << std::endl;
std::cout << "sizeof(long long): " << sizeof(long long) << std::endl;
std::cout << "sizeof(unsigned char): " << sizeof(unsigned char) << std::endl;
std::cout << "sizeof(unsigned short): " << sizeof(unsigned short) << std::endl;
std::cout << "sizeof(unsigned int): " << sizeof(unsigned int) << std::endl;
std::cout << "sizeof(unsigned long): " << sizeof(unsigned long) << std::endl;
std::cout << "sizeof(unsigned long long): " << sizeof(unsigned long long) << std::endl;
return 0;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream>
#include <cstdint>
int main(int, char **)
{
std::cout << "unit is byte" << std::endl;
std::cout << "sizeof(int8_t): " << sizeof(int8_t) << std::endl;
std::cout << "sizeof(uint8_t): " << sizeof(uint8_t) << std::endl;
std::cout << "sizeof(int16_t): " << sizeof(int16_t) << std::endl;
std::cout << "sizeof(uint16_t): " << sizeof(uint16_t) << std::endl;
std::cout << "sizeof(int32_t): " << sizeof(int32_t) << std::endl;
std::cout << "sizeof(uint32_t): " << sizeof(uint32_t) << std::endl;
std::cout << "sizeof(int64_t): " << sizeof(int64_t) << std::endl;
std::cout << "sizeof(uint64_t): " << sizeof(uint64_t) << std::endl;
return 0;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream>
#include <cstdint>
int main(int, char **)
{
// C-style POD array.
int32_t data[100];
// Make a pointer to the head address of the array.
int32_t * pdata = data;
// Make another pointer to the 50-th element from the head of the array.
int32_t * odata = pdata + 50;
// Initialize the array.
for (size_t it=0; it<100; ++it) { data[it] = it + 5000; }
std::cout << "data[10]: " << data[10] << std::endl;
std::cout << "pdata[10]: " << pdata[10] << std::endl;
std::cout << "*(data+20): " << *(data+20) << std::endl;
std::cout << "*(pdata+20): " << *(pdata+20) << std::endl;
std::cout << "data[50]: " << data[50] << std::endl;
std::cout << "odata[0]: " << odata[0] << std::endl;
std::cout << "data[40]: " << data[40] << std::endl;
std::cout << "odata[-10]: " << odata[-10] << std::endl;
std::cout << "*(data+40): " << *(data+40) << std::endl;
std::cout << "*(odata-10): " << *(odata-10) << std::endl;
return 0;
}
|
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 38 | #include <iostream>
class PointClass
{
float m_x, m_y; // by default private.
public:
// Accessors.
float getX() const { return m_x; }
float getY() const { return m_y; }
void setX(float v) { m_x = v; }
void setY(float v) { m_y = v; }
}; /* end class PointClass */
struct PointStruct
{
float m_x, m_y; // by default public.
}; /* end class PointStruct */
int main(int, char **)
{
PointClass pntc;
PointStruct pnts;
float x=1000, y=2000;
pntc.setX(x+2); pntc.setY(y+4);
pnts.m_x = x+7; pnts.m_y = x+9;
std::cout << "PointClass and PointStruct has the same size: "
<< (sizeof(PointClass) == sizeof(PointStruct) ? "true" : "false")
<< std::endl;
std::cout << "pntc.getX() = " << pntc.getX() << ", pntc.getY() = " << pntc.getY() << std::endl;
std::cout << "pnts.m_x = " << pnts.m_x << ", pnts.m_y = " << pnts.m_y << std::endl;
return 0;
}
|
$ g++ class.cpp -o class --std=c++11
$ ./class
PointClass and PointStruct has the same size: true
pntc.getX() = 1002, pntc.getY() = 2004
pnts.m_x = 1007, pnts.m_y = 1009