How to check deallocation of memory
In few words: you can't.
Check out tools like Valgrind to help you debugging memory leaks issues.
Some other things you should consider:
- Use smart pointers so that you don't have do think about memory management,
- Set your pointers to 0 after you free them, so that a further
delete
has no effect, - Use standard classes (
vector
, ...) instead of rolling your own, - Finally, don't use pointers (actually you almost can)
Check for proper memory deallocation in C
Your best friend for memory leaks is a good guide to C programming. There are lots of those available.
After that consider some tools like valgrind or efence (linux),
http://valgrind.org/
efence comes with some linux distributions.
Windows has tools for heap analysis too, for example on XP:
http://support.microsoft.com/kb/268343
How to find the size of the memory deallocated
In general, the answer is no, because memory managers hide that kind of implementation-dependent information from you. Also, C++ doesn't provide any standard way of tracking how much memory is actually used/freed. There might be functions specific to a certain platform/operating system, but nothing that is 100% portable.
Deallocation of memory in arrays
Your initial call to malloc
is wrong. You allocate space for x
characters, not for pointers to char
.
Your second call inside the loop is wrong to, as you don't allocate space for the terminator.
Lastly, and unrelated to the problems you ask about, but if the fread
calls inside the loop fails, you will have memory leaks.
How memory is deallocated in shared_ptr via the operator=?
2.the last remaining shared_ptr owning the object is assigned another pointer via operator= or reset().
Yes.
After "P2=P1" the memory allocated to P1 has to be deallocated right?
No. It could happen to P2
if it pointed to something. Not P1
.
The logic behind the rule (2) is that the assignment overwrites the value of the first operand, so the first operand will no longer point to what it used to. If it was the last pointer to something, then nothing will point to that anymore, and it can be deleted.
Memory Allocation/Deallocation?
The Memory Model
The C++ standard has a memory model. It attempts to model the memory in a computer system in a generic way. The standard defines that a byte is a storage unit in the memory model and that memory is made up of bytes (§1.7):
The fundamental storage unit in the C++ memory model is the byte. [...] The memory available to a C++ program consists of one or more sequences of contiguous bytes.
The Object Model
The standard always provides an object model. This specifies that an object is a region of storage (so it is made up of bytes and resides in memory) (§1.8):
The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is a region of storage.
So there we go. Memory is where objects are stored. To store an object in memory, the required region of storage must be allocated.
Allocation and Deallocation Functions
The standard provides two implicitly declared global scope allocation functions:
void* operator new(std::size_t);
void* operator new[](std::size_t);
How these are implemented is not the standard's concern. All that matters is that they should return a pointer to some region of storage with the number of bytes corresponding to the argument passed (§3.7.4.1):
The allocation function attempts to allocate the requested amount of storage. If it is successful, it shall return the address of the start of a block of storage whose length in bytes shall be at least as large as the requested size. There are no constraints on the contents of the allocated storage on return from the allocation function.
It also defines two corresponding deallocation functions:
void operator delete(void*);
void operator delete[](void*);
Which are defined to deallocate storage that has previously been allocated (§3.7.4.2):
If the argument given to a deallocation function in the standard library is a pointer that is not the null pointer value (4.10), the deallocation function shall deallocate the storage referenced by the pointer, rendering invalid all pointers referring to any part of the deallocated storage.
new
and delete
Typically, you should not need to use the allocation and deallocation functions directly because they only give you uninitialised memory. Instead, in C++ you should be using new
and delete
to dynamically allocate objects. A new-expression obtains storage for the requested type by using one of the above allocation functions and then initialises that object in some way. For example new int()
will allocate space for an int
object and then initialise it to 0. See §5.3.4:
A new-expression obtains storage for the object by calling an allocation function (3.7.4.1).
[...]
A new-expression that creates an object of type T initializes that object [...]
In the opposite direction, delete
will call the destructor of an object (if any) and then deallocate the storage (§5.3.5):
If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted.
[...]
If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will call a deallocation function (3.7.4.2).
Other Allocations
However, these are not the only ways that storage is allocated or deallocated. Many constructs of the language implicitly require allocation of storage. For example, giving an object definition, like int a;
, also requires storage (§7):
A definition causes the appropriate amount of storage to be reserved and any appropriate initialization (8.5) to be done.
C standard library: malloc
and free
In addition, the <cstdlib>
header brings in the contents of the stdlib.h
C standard library, which includes the malloc
and free
functions. They are also defined, by the C standard, to allocate and deallocate memory, much like the allocation and deallocation functions defined by the C++ standard. Here's the definition of malloc
(C99 §7.20.3.3):
void *malloc(size_t size);
Description
Themalloc
function allocates space for an object whose size is specified bysize
and
whose value is indeterminate.
Returns
Themalloc
function returns either a null pointer or a pointer to the allocated space.
And the definition of free
(C99 §7.20.3.2):
void free(void *ptr);
Description
Thefree
function causes the space pointed to byptr
to be deallocated, that is, made
available for further allocation. Ifptr
is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by thecalloc
,malloc
, orrealloc
function, or if the space has been deallocated by a call tofree
orrealloc
,
the behavior is undefined.
However, there's never a good excuse to be using malloc
and free
in C++. As described before, C++ has its own alternatives.
Answers to Questions
So to answer your questions directly:
Where is the "memory" that is being allocated?
The C++ standard doesn't care. It simply says that the program has some memory which is made up of bytes. This memory can be allocated.
What is this "memory"? Space in an array? Or something else?
As far as the standard is concerned, the memory is just a sequence of bytes. This is purposefully very generic, as the standard only tries to model typical computer systems. You can, for the most part, think of it as a model of the RAM of your computer.
What happens exactly when this "memory" gets allocated?
Allocating memory makes some region of storage available for use by the program. Objects are initialized in allocated memory. All you need to know is that you can allocate memory. The actual allocation of physical memory to your process tends to be done by the operating system.
What happens exactly when the memory gets deallocated?
Deallocating some previously allocated memory causes that memory to be unavailable to the program. It becomes deallocated storage.
It would also really help me if someone could answer what malloc does in these C++ lines:
char* x;
x = (char*) malloc (8);Here,
malloc
is simply allocating 8 bytes of memory. The pointer it returns is being cast to achar*
and stored inx
.
Related Topics
Pointer Comparisons ">" with One Before the First Element of an Array Object
How to Use Gpu::Stream in Opencv
How to Order the Members of a C++ Class
Why C++11 Compiler Support Still Requires a Flag
Capture _Line_ and _File_ Without #Define
Parallel for VS Omp Simd: When to Use Each
Error: 'Null' Was Not Declared in This Scope
Programming Slim C++ Programs (Like Utorrent) for Windows
Generate Dependencies for a Makefile for a Project in C/C++
"_Gfortran_Pow_C8_I4" Error When Linking .O Files from G++ and Gfortran Using G++
How to Run a Bash Script from C++ Program
What Are the Stages of Compilation of a C++ Program