C
C++Liliput
Hi,
I was looking at the implementation of operator new and operator
new[] in gcc source code and found that the implementation is exactly
the same. The only difference is that the size_t argument passed to
the operators is calculated correctly during runtime and passed in.
Inside the implementation both operators (new and new[]) do a simple
malloc(). Ditto for operator delete/delete[]. I have two questions:
1) Who passes in the size_t argument at runtime?
2) If the operator implementations are virtually the same (at least
for gcc), can we assume that mismatched new[]/delete and new/delete[]
calls will not result in any problem? I ask this because I was doing
an exercise of finding memory leaks inside some C++ code. When I
executed the code with Valgrind, I got a bunch of mismatched new[]/
delete errors. However there were no reported memory leaks as such.
However I assumed that doing a new[] and then a delete would most
probably leak memory because delete would probably delete only the
first object of the array (since the implementation of operator delete
will not know about the array size). So I wrote a small C++ program
where I do a lot of new[]'s and delete's on my Linux box and then
monitored the memory footprint using the top command. However contrary
to my expectations, there were no memory leaks. That is when I started
looking into the implementation of operator new/new[] and operator
delete/delete[] in gcc source code and found out that there is no
difference in the internal implementation of the scalar and the
corresponding vector operator.
I was looking at the implementation of operator new and operator
new[] in gcc source code and found that the implementation is exactly
the same. The only difference is that the size_t argument passed to
the operators is calculated correctly during runtime and passed in.
Inside the implementation both operators (new and new[]) do a simple
malloc(). Ditto for operator delete/delete[]. I have two questions:
1) Who passes in the size_t argument at runtime?
2) If the operator implementations are virtually the same (at least
for gcc), can we assume that mismatched new[]/delete and new/delete[]
calls will not result in any problem? I ask this because I was doing
an exercise of finding memory leaks inside some C++ code. When I
executed the code with Valgrind, I got a bunch of mismatched new[]/
delete errors. However there were no reported memory leaks as such.
However I assumed that doing a new[] and then a delete would most
probably leak memory because delete would probably delete only the
first object of the array (since the implementation of operator delete
will not know about the array size). So I wrote a small C++ program
where I do a lot of new[]'s and delete's on my Linux box and then
monitored the memory footprint using the top command. However contrary
to my expectations, there were no memory leaks. That is when I started
looking into the implementation of operator new/new[] and operator
delete/delete[] in gcc source code and found out that there is no
difference in the internal implementation of the scalar and the
corresponding vector operator.