I am writing a function which requires an array to be created at runtime. The array will be of small size so I am not worried about unsafe code, however, I want to write 'proper' code. As such I am considering three alternatives:
Using Compiler Explorer to compare them with -O3. The results were as such:
Am I missing an optimisation for std::vector<> or is the 'proper' c++ way slower or have I entirely missed a way of coding this?
edit: I forgot to delete the heap-allocated array
Test code:
code 1:
#include <string.h>
void populate_array(char* arr);
int compute_result(char* arr);
int str_to_arr(const char* str)
{
auto len = strlen(str);
char array[len];
populate_array(array);
return compute_result(array);
}
code 2:
#include <string.h>
void populate_array(char* arr);
int compute_result(char* arr);
int str_to_arr(const char* str)
{
auto len = strlen(str);
char* array = new char[len];
populate_array(array);
auto result = compute_result(array);
delete[] array;
return result;
}
code 3:
#include <string.h>
#include <vector>
void populate_array(std::vector<char> arr);
int compute_result(std::vector<char> arr);
int str_to_arr(const char* str)
{
auto len = strlen(str);
std::vector<char> array(len);
populate_array(array);
return compute_result(array);
}
Safe and useful variable length arrays It's convenient and useful because it makes some expressions simpler. It's safe because there's no arbitrary stack allocation. Pointers to arrays are a rare sight in C code, whether variable length or not.
In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at run time (instead of at compile time). In C, the VLA is said to have a variably modified type that depends on a value (see Dependent type).
Variable sized arrays are data structures whose length is determined at runtime rather than compile time. These arrays are useful in simplifying numerical algorithm programming. The C99 is a C programming standard that allows variable sized arrays.
A variable length array, which is a C99 feature, is an array of automatic storage duration whose length is determined at run time.
There are a few issues in the code, that may be leading you astray in the comparison.
new char(len)
allocates a single char, initialized with the value len
. You'd be after new char[len]
to allocate len
chars. There should be a matching delete []
, too.std::vector<char>
object is passed to populate_array
by value, making a copy (and consequently not actually populating the array you want), and similarly for compute_result
. These copies will engender new allocations. Passing by reference would be appropriate here.std::vector
will value-initialize all its elements. Effectively, it means that every element in this vector is set to zero. This is not performed by new char[len]
.VLAs are not part of C++, but may be provided as an extension. While in this instance, for small len
, the compiler has the option of allocating the space for the array on the stack, they are probably best avoided because of their non-standard nature; even in C, they are not required to be supported.
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