base on size of "char **" total size of array must be 8*4*409113 = 13 MB but size of allocated memory in task manager is 29 MB!
if I comment "char **row = new char*[4];" , size of allocated memory in task manager is 1.6 MB.
What's wrong ?!
It is not wrong. There will be at least as much memory allocated as it is needed to hold the data. But it is very much possible that more memory is used for internal purpose (header data).
Valgrind can show exactly how much memory has been requested by the code because it is designed to do just that. Task Manager will only show how much memory has been requested by the process. A process may request more memory that the programmer's code has requested. Code itself resides in memory and needs to be placed somewhere. The stack also resides in memory and needs to be placed somewhere. All these things use some memory not directly related to computing the result of the program.
clang++ -std=c++14 -stdlib=libc++ -O2 -Wall -Wextra -pedantic-errors main.cpp && ./a.out 2>&1 | grep 'in use\|#'
echo -e '\n--------------------\n'
clang++ -DNOT_TRIVIALLY_DESTRUCTIBLE -std=c++14 -stdlib=libc++ -O2 -Wall -Wextra -pedantic-errors main.cpp && ./a.out 2>&1 | grep 'in use\|#'
#require at least 35 bytes
#alloc request for 35 bytes; return pointer 0x959010
#array allocated at address: 0x959010
in use bytes = 48
in use bytes = 48
#deallocate memory at 0x959010
in use bytes = 0
in use bytes = 0
--------------------
# *** NOT_TRIVIALLY_DESTRUCTIBLE ***
#require at least 35 bytes
#alloc request for 43 bytes; return pointer 0x227b010
#array allocated at address: 0x227b018
in use bytes = 64
in use bytes = 64
#deallocate memory at 0x227b010
in use bytes = 0
in use bytes = 0
In this implementation, we can see that 48 bytes are allocated to service an allocation request for 35 bytes.
When the object is not trivially destructible, there is an overhead for array allocations (space for a magic cookie is also allocated). The array size is still 35 bytes, the allocation request is for 43 bytes (an eight byte magic cookie, possibly holds a pointer), and 64 bytes are allocated in all.