fork(1) download
  1. #include <iostream>
  2.  
  3. template<class T>
  4. class MyVector
  5. {
  6. public:
  7. MyVector()
  8. {
  9. // Initialize the variables
  10. value = new T[0];
  11. length = 0;
  12. cap = 0;
  13. }
  14.  
  15. void push_back(T val)
  16. {
  17. // Check if the array has enough allocated memory
  18. // to add another element to it.
  19. if((cap) >= (length + 1))
  20. {
  21. // If it does, add 1 on the length variable, and then
  22. // add the element to the array
  23. length++;
  24.  
  25. value[length - 1] = val;
  26. }
  27.  
  28. else
  29. {
  30. // If there isn't enough allocated memory, resize the array
  31. resize(length + 1);
  32. push_back(val); // After the array is resized, call the function again
  33. }
  34.  
  35. //std::cout << "Length : " << length << "\nCapacity : " << cap << "\n";
  36. }
  37.  
  38. void pop_back()
  39. {
  40. // Check if the vector has any elements
  41. if(length > 0)
  42. {
  43. length--; // Decrease the length
  44. T* tmp = value; // Create a temporary array
  45.  
  46. delete[] value; // Free the class array
  47. value = new T[length]; // Allocate a new array with one less element
  48.  
  49. for(unsigned int len = 0; len < length; len++)
  50. {
  51. // Now, copy over the values besides the last
  52. value[len] = tmp[len];
  53. }
  54.  
  55. // delete the temporary value
  56. delete[] tmp;
  57. }
  58.  
  59. else
  60. {
  61. // If the array has no elements, we display a message
  62. std::cout << "Vector is already equal to zero\n";
  63. }
  64. }
  65.  
  66. unsigned int size()
  67. {
  68. return length; // Return the length of the vector
  69. }
  70.  
  71. unsigned int capacity()
  72. {
  73. return cap; // Return the capacity of the vector
  74. }
  75.  
  76. void reserve(unsigned int amount)
  77. {
  78. delete[] value; // Delete the vector
  79. value = new T[amount]; // Reserve the requested amount of memory
  80.  
  81. cap = amount; // Update the capacity as required
  82. }
  83.  
  84. void resize(unsigned int amount)
  85. {
  86. T* tmp = value; // Allocate a pointer that holds the value
  87.  
  88. delete[] value; // Delete the class pointer
  89. value = new T[amount]; // Allocate a new amount
  90.  
  91. cap = amount; // Update
  92.  
  93. for(unsigned int v = 0; v < cap; v++)
  94. {
  95. value[v] = tmp[v]; // Copy the vector elements
  96. }
  97. delete[] tmp;
  98. }
  99.  
  100. // * The [] operator
  101. T &operator[](unsigned int index)
  102. {
  103. if(index > length)
  104. {
  105. // If the index points to an element out of the vector's range...
  106. std::cout << "Vector index out of range.\n";
  107. }
  108.  
  109. else
  110. {
  111. // Else, we return a reference to the array's element
  112. return value[index];
  113. }
  114. }
  115.  
  116. ~MyVector()
  117. {
  118. // In the destructor we free allocated memory
  119. delete[] value;
  120. }
  121.  
  122. private:
  123. T* value; // The pointer that's used as the
  124.  
  125. unsigned int length; // The length of the variable
  126. unsigned int cap; // Capacity of the array
  127. };
  128.  
  129. int main()
  130. {
  131. MyVector<int> v ;
  132. for ( unsigned i=0; i<100; ++i)
  133. v.push_back(i) ;
  134.  
  135. for ( unsigned i=0; i<100; ++i)
  136. std::cout << v[i] << ' ' ;
  137. std::cout << '\n' ;
  138. }
  139.  
Runtime error #stdin #stdout #stderr 0s 4004KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
*** glibc detected *** ./prog: double free or corruption (fasttop): 0x08c52008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x6e3f1)[0xb75a03f1]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x6fc58)[0xb75a1c58]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(cfree+0x6d)[0xb75a4d0d]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb77204bf]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb7548e46]
./prog[0x8048951]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:03 5735362    /home/g01m8t/prog
08049000-0804a000 rw-p 00000000 08:03 5735362    /home/g01m8t/prog
08c52000-08c73000 rw-p 00000000 00:00 0          [heap]
b7400000-b7421000 rw-p 00000000 00:00 0 
b7421000-b7500000 ---p 00000000 00:00 0 
b7530000-b7532000 rw-p 00000000 00:00 0 
b7532000-b7688000 r-xp 00000000 08:03 7014801    /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
b7688000-b7689000 ---p 00156000 08:03 7014801    /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
b7689000-b768b000 r--p 00156000 08:03 7014801    /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
b768b000-b768c000 rw-p 00158000 08:03 7014801    /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
b768c000-b768f000 rw-p 00000000 00:00 0 
b768f000-b76ab000 r-xp 00000000 08:03 6998923    /lib/i386-linux-gnu/libgcc_s.so.1
b76ab000-b76ac000 rw-p 0001b000 08:03 6998923    /lib/i386-linux-gnu/libgcc_s.so.1
b76ac000-b76ad000 rw-p 00000000 00:00 0 
b76ad000-b76d1000 r-xp 00000000 08:03 7014829    /lib/i386-linux-gnu/i686/cmov/libm-2.13.so
b76d1000-b76d2000 r--p 00023000 08:03 7014829    /lib/i386-linux-gnu/i686/cmov/libm-2.13.so
b76d2000-b76d3000 rw-p 00024000 08:03 7014829    /lib/i386-linux-gnu/i686/cmov/libm-2.13.so
b76d3000-b77b3000 r-xp 00000000 08:03 7537416    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b77b3000-b77b7000 r--p 000e0000 08:03 7537416    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b77b7000-b77b8000 rw-p 000e4000 08:03 7537416    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b77b8000-b77bf000 rw-p 00000000 00:00 0 
b77c3000-b77c5000 rw-p 00000000 00:00 0 
b77c5000-b77c6000 r-xp 00000000 00:00 0          [vdso]
b77c6000-b77e2000 r-xp 00000000 08:03 6998900    /lib/i386-linux-gnu/ld-2.13.so
b77e2000-b77e3000 r--p 0001b000 08:03 6998900    /lib/i386-linux-gnu/ld-2.13.so
b77e3000-b77e4000 rw-p 0001c000 08:03 6998900    /lib/i386-linux-gnu/ld-2.13.so
bfd8d000-bfda2000 rw-p 00000000 00:00 0          [stack]