fork download
  1. // If you are not sure what some lines of code do, try looking back at
  2. // previous example programs, notes, or ask a question.
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int strlen(const char* str);
  9. int strcpy(char* dest, const char* src);
  10. char* getNewStr(int size);
  11.  
  12. int main() {
  13.  
  14. // Declaring an array on the stack vs. on the heap
  15. char stackStr[50];
  16. char* heapStr = new char[50];
  17.  
  18. // Both arrays can be used in the same ways
  19. for(int i = 0; i < 50; i++) {
  20. stackStr[i] = 'a';
  21. heapStr[i] = 'a';
  22. }
  23.  
  24. // However, the string on the heap must be deleted when
  25. // you are done with it--if you never deleted it is considered
  26. // leaked memory.
  27. delete[] heapStr;
  28. heapStr = NULL;
  29.  
  30. // Here, you can see how important is to delete your memory
  31. // before you lose access to it, or else this loop would
  32. // leak a ton of memory.
  33. for(int i = 0; i < 50; i++) {
  34. heapStr = new char[100];
  35.  
  36. // If the memory was not deleted, a new set of memory
  37. // would be assigned to "heapStr," and the previous
  38. // memory would be leaked.
  39. delete[] heapStr;
  40. heapStr = NULL;
  41. }
  42.  
  43. // Input a buffered string
  44. char buffer[100];
  45. cout << endl << "Enter a string: ";
  46. cin >> buffer;
  47.  
  48. // Allocate the exact number of characters necessary.
  49. heapStr = new char[strlen(buffer) + 1];
  50.  
  51. // Copy the string
  52. strcpy(heapStr,buffer);
  53.  
  54. cout << endl << "Exactly sized word: " << heapStr << endl << endl;
  55.  
  56. // Delete the string
  57. delete[] heapStr;
  58. heapStr = NULL;
  59.  
  60. // Finally, here is a function that will return dynamic memory.
  61. char* newStr = getNewStr(10);
  62.  
  63. // the memory originally created in the function can be deleted
  64. // here, at the end of main
  65. delete[] newStr;
  66.  
  67.  
  68. system("pause");
  69. return 0;
  70. }
  71.  
  72. char* getNewStr(int size) {
  73.  
  74. // Create a new dynamically allocated character array and return it.
  75. // The calling function will still have access to the memory, so it
  76. // does not need to be deleted here.
  77. return new char[size];
  78. }
  79.  
  80. int strlen(const char* str) {
  81. int counter = 0;
  82.  
  83. // Loop until you find a value that equals 0, the null char
  84. // (which marks the end of the string)
  85. while(str[counter])
  86. counter++;
  87.  
  88. return counter;
  89. }
  90.  
  91. int strcpy(char* dest, const char* src) {
  92.  
  93. int counter;
  94.  
  95. // Loop along the length of the source string,
  96. // and copy each character
  97. for(counter = 0; counter < strlen(src); counter++)
  98. dest[counter] = src[counter];
  99.  
  100. // Remember to add a null character at the end
  101. // of the destination string
  102. dest[counter] = '\0';
  103. }
  104.  
Success #stdin #stdout #stderr 0s 3472KB
stdin
hello!
stdout
Enter a string: 
Exactly sized word: hello!

stderr
sh: 1: pause: not found