-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocate_str.cpp
76 lines (66 loc) · 2.37 KB
/
allocate_str.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* STRING MEMORY ALLOCATION - C & C++ COMPATIBLE TECHNIQUES
*
* This program demonstrates various correct (and one incorrect) ways to allocate and manage
* memory for strings in a C/C++ environment. These methods showcase how strings can be
* handled safely across C and C++ paradigms.
*
* Scenarios covered:
* 1. Traditional C-style allocation using malloc + strcpy
* 2. Modern C++ using std::unique_ptr for RAII
* 3. Using std::string (C++ standard container)
* 4. Incorrect usage: returning a pointer to local stack memory
*
* Example Output:
* Hello my friend (C)
* Hello my friend (unique_ptr)
* Hello my friend (std::string)
* Undefined behavior (likely garbage or crash)
*/
#include <cstring>
#include <iostream>
#include <memory>
#include <string>
#include <cstdlib>
// Correct Traditional C Way - Manual memory management
char* allocateMemoryC(const char* str) {
char* pointer = static_cast<char*>(malloc(strlen(str) + 1));
if (pointer) {
strcpy(pointer, str);
}
return pointer;
}
// Correct C++ Way using std::unique_ptr
std::unique_ptr<char[]> allocateMemoryUniquePtr(const std::string& str) {
auto pointer = std::make_unique<char[]>(str.length() + 1);
strcpy(pointer.get(), str.c_str());
return pointer;
}
// Correct C++ Way using std::string
std::string allocateMemoryStdString(const std::string& str) {
return str; // Simple copy constructor
}
// Incorrect Way - returning pointer to stack memory
char* allocateMemoryWrong() {
char str[20] = "Hello world";
return str; // ❌ Undefined behavior: stack-allocated memory
}
int main() {
// Correct Traditional C Way
char* cPointer = allocateMemoryC("Hello my friend (C)");
if (cPointer) {
std::cout << "\n " << cPointer << " \n";
free(cPointer); // Important: free manually allocated memory
}
// Correct C++ Way using std::unique_ptr
auto uniquePointer = allocateMemoryUniquePtr("Hello my friend (unique_ptr)");
std::cout << "\n " << uniquePointer.get() << " \n";
// Memory automatically managed
// Correct C++ Way using std::string
std::string str = allocateMemoryStdString("Hello my friend (std::string)");
std::cout << "\n " << str << " \n";
// Incorrect usage demonstration
char* wrongPointer = allocateMemoryWrong();
std::cout << "\n " << wrongPointer << " \n"; // ❗ May crash or print garbage
return 0;
}