Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocate n bytes by new[] and fill it with any type?

Tags:

c++

memory

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?

like image 579
Criss Avatar asked Sep 23 '16 19:09

Criss


People also ask

What is deallocation in C++?

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).

How do you dynamically allocate a character array in C++?

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.

How you can use new and delete keyword for dynamic memory allocation?

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.

Is malloc same as array?

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.


1 Answers

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).

like image 181
alain Avatar answered Nov 04 '22 22:11

alain