Most Common Mistake done/seen in C++ [duplicate] - c++

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What C++ pitfalls should I avoid ?
What is the most common mistake in C/C++programming that you keep committing or see most of the people do? Being aware of it atleast subconsciously will increase my or anyone's chances of committing it.

Wrong memory management in all kinds of ways - new without delete, delete on the wrong pointer, unclear ownership of a pointer with resulting memory allocation / deallocation problems etc.

Related

C++ vector: difference between clear() and resize() [duplicate]

This question already has answers here:
Vector clear vs. resize
(4 answers)
Closed 4 years ago.
for a vector
std::vector<int> vec;
what's the difference between vec.clear() and vec.resize(0) ?
And if I want to clean a vector, what might be the best practice? (mainly for performance and efficiency concern)
A C++ standard library is allowed to implement vec.clear() as vec.resize(0) so they may well not be distinguishable. Note that neither function is allowed to reduce the capacity.
Personally I'd use clear() as that ever-so-slightly better signals your intent.

C++ struct vs Class in terms of memory [duplicate]

This question already has answers here:
C/C++ Struct vs Class
(7 answers)
Closed 6 years ago.
I have recently come across a question in one of my interview.
What is the difference between C++ struct & class in terms of memory ??
I know they are same in all aspects except access specifiers while inheriting & in case of member variables.
Apart from this is there a real difference in terms of memory (may be memory allocation or destruction or memory management whatever) ????
Edit:
I am not pretty sure why did the interviewer asks this question when there is no difference. I have found similar question here see the 2nd comment down to that link, he is asking a same question but no answer. I think there should be a diff.
Thanks In Advance.
In C++, a class and a struct are completly identical except for the facts that structs default to public access and inheritance whereas class defaults to private.
As far as memory layout there is no difference what-so-ever.

Memory leaks in C++ using FindFile and file handles [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing an app that is moving files on windows and I have some memory leaks. I don't have access to the code now, but I know I forgot to close file handles used to iterate over files with FindFirstFile and FindNextFile. Can this cause memory leaks, or does something else have to be wrong with my code? I cannot post any fragments now, but I am wondering, if this me cause the problem.
Memory leaks are just a special case of resource leaks. File handles are resources, too. So you definitely leak resources of various kinds.
BTW, how do you check for memory leaks?
Your problem is not writing C++ code, but C code that's then compiled using a C++ compiler. WINAPI is not C++. To realistically use it from C++, you must wrap all resources in classes that implement RAII so that they can be safely used from your C++ code without worries about resource leaks. You should also be using the smart pointers (std::unique_ptr and std::shared_ptr) to manage your memory.

Why do we use pointers instead of local variables? [duplicate]

This question already has answers here:
Why use pointers? [closed]
(17 answers)
Closed 9 years ago.
I don't understand the difference between using a pointer and using a normal variable. I'm learning linked lists in class so the use of pointers seems more straightforward since pointers are used to go to the next node in the list, but I don't understand its more basic uses and I'm feeling stressed as it is something I should already understand but don't.
The pointer is in itself just a "normal" variable that just so happens to store a level of indirection to another variable.

Why is the heap (as in where we put objects) called that? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
What is the origin of the term “heap” for the free store?
Why is the heap called the heap? I mean the bit of memory which we dynamically allocate bits of.
I don't have a citation for this, but I've always believed it derives from "a place to put stuff; a bit like the stack only less organised".
A stack has strict rules about where you add and remove things, but a heap doesn't.