Linked Questions
74 questions linked to/from RAII and smart pointers in C++
2
votes
1
answer
4k
views
deallocate an object directly in C++ [duplicate]
I am new to C++, I was trying to deallocate an object with different ways. Here is my code:
class foo{
public:
int* bar;
foo(int N){
bar = new int[N];
}
~foo(void){
...
122
votes
10
answers
21k
views
Understanding the meaning of the term and the concept - RAII (Resource Acquisition is Initialization)
Could you C++ developers please give us a good description of what RAII is, why it is important, and whether or not it might have any relevance to other languages?
I do know a little bit. I believe ...
59
votes
5
answers
126k
views
How do I create an array of pointers?
I am trying to create an array of pointers. These pointers will point to a Student object that I created. How do I do it?
What I have now is:
Student * db = new Student[5];
But each element in that ...
24
votes
6
answers
25k
views
What does delete command really do for memory, for pointers in C++? [duplicate]
Possible Duplicate:
C++ delete - It deletes my objects but I can still access the data?
Can a local variable's memory be accessed outside its scope?
I do not understand what delete really ...
42
votes
4
answers
29k
views
RAII tutorial for C++ [closed]
I'd like to learn how to use RAII in c++. I think I know what it is, but have no idea how to implement it in my programs. A quick google search did not show any nice tutorials.
Does any one have ...
15
votes
9
answers
3k
views
Usage of Smart Pointers as a Programming Standard?
More and more I hear, that I should use smart pointers instead of naked pointers, despite I have effective memory leak system implemented.
What is the correct programming approach on using smart ...
19
votes
3
answers
38k
views
Consequences, pros, cons using std::thread vs windows CreateThread [closed]
What are the pros, cons and consequences of using C++11 std::thread vs WinAPI functions (such as CreateThread, _beginthreadex, etc.) ?
20
votes
6
answers
2k
views
What if I delete an array once in C++, but allocate it multiple times?
Suppose I have the following snippet.
int main()
{
int num;
int* cost;
while(cin >> num)
{
int sum = 0;
if (num == 0)
break;
// Dynamically ...
9
votes
8
answers
3k
views
Whats the right approach for error handling in C++
One is to use C++ exceptions: try catch blocks. But freeing dynamic memory will be an issue when an exception is raised.
Second is to use C style: errno variable
Third is just to return -1 on error ...
13
votes
9
answers
4k
views
How to spot undefined behavior
Is there any way to know if you program has undefined behavior in C++ (or even C), short of memorizing the entire spec?
The reason I ask is that I've noticed a lot of cases of programs working in ...
12
votes
5
answers
8k
views
Dangling reference. Alternatives for dangling pointers and references?
The following code produces dangling references:
int main()
{
int *myArray = new int[2]{ 100, 200 };
int &ref = myArray[0];
delete[] myArray;
cout << ref; // Use of dangling ...
18
votes
4
answers
4k
views
Why and when do I need to supply my own deleter?
Why and when do I need to supply my own deleter? Isn't keyword delete sufficient enough?
If you use a smart pointer to manage a resource other than memory
allocated by new, remember to pass a ...
12
votes
4
answers
1k
views
Memory Allocation Exception in Constructor
I have a constructor that allocates several blocks of memory using the new operator.
X::X() {
a = new int[100];
b = new char[100];
c = new float[100];
}
My question is, if the allocation ...
6
votes
5
answers
16k
views
Proper way close WinAPI HANDLEs (avoiding of repeated closing)
I have some handle and I need to close it. There is some places in code, where handle may be closed. So, is this a right way to close handle?
HANDLE h;
....
if ( h != INVALID_HANDLE_VALUE ) {
::...
7
votes
6
answers
1k
views
C++ Safe Exception Handling
Let's have a piece of code (fstream is just an example, we could be talking about dynamic memory allocation...):
fstream f;
try {
f.open("xxx");
...
f.close();
} catch (...) {
...
}
When ...