I want to dynamically allocate known size of memory (just memory, not bothering about type) and fill it with exactly the same amount of data but any type (I'm only sure it will be primitive type). Ofc later I'm going to free it.
Is it ok? :
auto dest = new int8_t[n];
std::memcpy(dest, src, n);
delete[] dest;
src
is ptr to an array of size n
(Bytes).
I've ofc chosen int8_t
becuase it's the clearest way to allocate certain amount of memory.
In fact the code above isn't exaclt what it will be. delete[]
will be called on pointer of type which actually it points to.
So for example if src was an array of floats (forget about last line of above code):
float * ptr = dest;
delete[] ptr;
So again. Will it be ok?
C++ supports dynamic allocation and deallocation of objects using the new and delete operators. These operators allocate memory for objects from a pool called the free store (also known as the heap).
Use the new() Operator to Dynamically Allocate Array in C++ Then, we dynamically allocate the char array and assign the corresponding values to its elements in the for loop body. Note that the delete operator must be explicitly called once the memory associated with the arr pointer is no longer needed.
Examples: delete p; delete q; To free the dynamically allocated array pointed by pointer-variable, use the following form of delete: // Release block of memory // pointed by pointer-variable delete[] pointer-variable; Example: // It will free the entire array // pointed by p.
The array is created at compile time while with malloc memory is allocated during run time; unless you grab all the memory with one malloc but then there is no advantage to using malloc.
It is ok, but only if you use a char
array or an unsigned char
array, because of the special alignment guarantees for these types:
5.3.4 New
11 When a new-expression calls an allocation function and that allocation has not been extended, the new- expression passes the amount of space requested to the allocation function as the first argument of type std::size_t. That argument shall be no less than the size of the object being created; it may be greater than the size of the object being created only if the object is an array. For arrays of char and unsigned char, the difference between the result of the new-expression and the address returned by the allocation function shall be an integral multiple of the strictest fundamental alignment requirement (3.11) of any object type whose size is no greater than the size of the array being created. [ Note: Because allocation functions are assumed to return pointers to storage that is appropriately aligned for objects of any type with fundamental alignment, this constraint on array allocation overhead permits the common idiom of allocating character arrays into which objects of other types will later be placed. — end note ]
emphasis by me.
Another requirement is that you only use primitive types or PODs, because you don't call a constructor, but the (trivial) destructor (through delete
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With