Linked Questions

2 votes
1 answer
4k views

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){ ...
Gaohan's user avatar
  • 31
122 votes
10 answers
21k views

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 ...
Charlie Flowers's user avatar
59 votes
5 answers
126k views

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 ...
chustar's user avatar
  • 12.5k
24 votes
6 answers
25k views

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 ...
Green's user avatar
  • 31k
42 votes
4 answers
29k views

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 ...
Joe Bloggs's user avatar
15 votes
9 answers
3k views

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 ...
Bunkai.Satori's user avatar
19 votes
3 answers
38k views

What are the pros, cons and consequences of using C++11 std::thread vs WinAPI functions (such as CreateThread, _beginthreadex, etc.) ?
KaMyLuS's user avatar
  • 239
20 votes
6 answers
2k views

Suppose I have the following snippet. int main() { int num; int* cost; while(cin >> num) { int sum = 0; if (num == 0) break; // Dynamically ...
Ankit Sharma's user avatar
9 votes
8 answers
3k views

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 ...
Jeet's user avatar
  • 1,040
13 votes
9 answers
4k views

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 ...
BlueRaja - Danny Pflughoeft's user avatar
12 votes
5 answers
8k views

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 ...
Chenna V's user avatar
  • 10.6k
18 votes
4 answers
4k views

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 ...
Rick's user avatar
  • 7,664
12 votes
4 answers
1k views

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 ...
wahab's user avatar
  • 835
6 votes
5 answers
16k views

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 ) { ::...
Loom's user avatar
  • 10k
7 votes
6 answers
1k views

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 ...
Petr's user avatar
  • 1,148

15 30 50 per page
1
2 3 4 5