fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. //
  8. // YOUR NAME: EVAN LYTTLE
  9. //
  10.  
  11. int main(void)
  12. {
  13. // Fundamental C++ types
  14. // There are several fundamental types in C++ including char, short, int
  15. // long, unsigned, float, double, long double, boolean (bool) and void.
  16. // We can use the sizeof() function to determine how much memory is
  17. // allocated for these various data types to better explore the concepts
  18. // of Chapter 6 and understand how and when memory is allocated (static vs.
  19. // dynamic). Following the example given for char, fill in the code for the
  20. // remaining fundamental types (if possible).
  21.  
  22. cout <<"Fundamental Types\n";
  23. cout <<" Type " << "Size \n";
  24. cout <<" char: " << sizeof(char) << " bytes.\n";
  25. cout <<" int: " << sizeof(int) << " bytes.\n";
  26. cout <<" short: " << sizeof(short) << " bytes.\n";
  27. cout <<" long: " << sizeof(long) << " bytes.\n";
  28. cout <<" float: " << sizeof(float) << " bytes.\n";
  29. cout <<" unsigned: " << sizeof(unsigned) << " bytes.\n";
  30. cout <<" double: " << sizeof(double) << " bytes.\n";
  31. cout <<" long double: " << sizeof(long double) << " bytes.\n";
  32. cout <<" bool: " << sizeof(bool) << " bytes.\n";
  33. cout <<" void: " << sizeof(void) << " bytes.\n";
  34.  
  35. // C++ Pointers
  36. // C++ allows for pointers to, among others, char, long and void data types.
  37. // We can again use the sizeof() function to determine how much memory is
  38. // allocated for these various data types to better explore the concepts
  39. // of Chapter 6 and understand how and when memory is allocated (static vs.
  40. // dynamic). As you did above, fill in the code to determine the memory size
  41. // allocated for these three pointer types.
  42.  
  43. cout <<"\nPointers\n";
  44. cout <<" Type " << "Size \n";
  45.  
  46. // Example for an integer pointer
  47. int i = 455;
  48. int *intPointer = &i;
  49. cout <<" int pointer: " << sizeof(intPointer) << " bytes.\n";
  50.  
  51. //char pointed
  52. char c = 'a';
  53. char *charPointer = &c;
  54. cout <<" char pointer: " << sizeof(charPointer) << " bytes.\n";
  55.  
  56. //long pointer
  57. long l = 455;
  58. long *longPointer = &l;
  59. cout <<" long pointer: " << sizeof(longPointer) << " bytes.\n";
  60.  
  61. //void pointer
  62. void *voidPointer = &i;
  63. cout <<" void pointer: " << sizeof(intPointer) << " bytes.\n";
  64.  
  65. // C++ Strings
  66. // Using the <string> library, we can explore the memory sizes allocated for
  67. // C++ strings using the sizeof() function. You should fill in code below to
  68. // experiment with how C++ allocates memory for various strings. For example,
  69. // you may want to compare setting the value of a string with "josh" and
  70. // getting its sizeof() compared with sizeof("josh"). You should also provide
  71. // code to determine when the memory size of the string is determined and
  72. // whether it can change or not.
  73.  
  74. cout <<"\nStrings\n";
  75. cout <<" Type " << "Size \n";
  76. string s;
  77. cout <<" string s before joshified: " << sizeof(s) << " bytes.\n";
  78. s = "josh";
  79. cout <<" string s after josh: " << sizeof(s) << " bytes.\n";
  80. cout <<" string of direct sizeof(josh): " << sizeof("josh") << " bytes.\n";
  81. s = "joshua";
  82. cout <<" string s after made into joshua: " << sizeof(s) << " bytes.\n";
  83. s = "joshuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  84. cout <<" string s after made much longer: " << sizeof(s) << " bytes.\n";
  85.  
  86. // C++ Enumerations
  87. cout <<"\nEnumerations\n";
  88. cout <<" Type " << "Size \n";
  89.  
  90. enum weekday {monday = 1, tuesday = 2, wednesday = 3, thursday = 4, friday = 5, saturday = 6, sunday = 7};
  91. cout <<" enums: " << sizeof(weekday) << " bytes.\n";
  92. enum january {january=1};
  93. cout <<" enum2: " << sizeof(january) << " bytes.\n";
  94.  
  95. // C++ Arrays
  96. // In C++, you can declare arrays by providing a name and a size of the array.
  97. // The sizeof() function will again provide the amount of memory allocated for
  98. // the array. Experiment with different array sizes and types to see how much memory
  99. // is allocated, and when it is allocated (declaration vs. instantiation).
  100. cout <<"\nArrays\n";
  101. cout <<" Type " << "Size \n";
  102. int intArray[1];
  103. cout <<" int array: " << sizeof(intArray) << " bytes.\n";
  104. long longArray[1];
  105. cout <<" long array: " << sizeof(longArray) << " bytes.\n";
  106. double doubleArray[1];
  107. cout <<" double array: " << sizeof(doubleArray) << " bytes.\n";
  108. char charArray[1];
  109. cout <<" char array: " << sizeof(charArray) << " bytes.\n";
  110. int intierArray[100000];
  111. cout <<" big int array: " << sizeof(intierArray) << " bytes.\n";
  112.  
  113. // C++ Standard Library Vectors
  114. // In C++, you can declare a vector as follows: vector<char> v(10); and similarly
  115. // determine its size using the sizeof() function. Experiment with different
  116. // vector sizes and types so that you can compare the differences bewteen C++
  117. // arrays and vectors.
  118.  
  119. cout <<"\nVectors\n";
  120. cout <<" Type " << "Size \n";
  121. vector<int> intVec(1);
  122. cout << " int vector: " << sizeof(intVec) << " bytes.\n";
  123. vector<long> longVec(1);
  124. cout << " long vector: " << sizeof(longVec) << " bytes.\n";
  125. vector<double> doubleVec(1);
  126. cout << " double vector: " << sizeof(doubleVec) << " bytes.\n";
  127. vector<char> charVec(1);
  128. cout << " char vector: " << sizeof(charVec) << " bytes.\n";
  129. vector<int> iVec(100000);
  130. cout << " intier vector: " << sizeof(iVec) << " bytes.\n";
  131.  
  132. }
Success #stdin #stdout 0s 708KB
stdin
Standard input is empty
stdout
Fundamental Types
    Type  Size 
    char: 1 bytes.
    int: 4 bytes.
    short: 2 bytes.
    long: 8 bytes.
    float: 4 bytes.
    unsigned: 4 bytes.
    double: 8 bytes.
    long double: 16 bytes.
    bool: 1 bytes.
    void: 1 bytes.

Pointers
    Type  Size 
    int pointer: 8 bytes.
    char pointer: 8 bytes.
    long pointer: 8 bytes.
    void pointer: 8 bytes.

Strings
    Type  Size 
    string s before joshified: 32 bytes.
    string s after josh: 32 bytes.
    string of direct sizeof(josh): 5 bytes.
    string s after made into joshua: 32 bytes.
    string s after made much longer: 32 bytes.

Enumerations
    Type  Size 
    enums: 4 bytes.
    enum2: 4 bytes.

Arrays
    Type  Size 
    int array: 4 bytes.
    long array: 8 bytes.
    double array: 8 bytes.
    char array: 1 bytes.
    big int array: 400000 bytes.

Vectors
    Type  Size 
    int vector: 24 bytes.
    long vector: 24 bytes.
    double vector: 24 bytes.
    char vector: 24 bytes.
    intier vector: 24 bytes.