What Is the Status on Dynarrays

What is the status on dynarrays?

std::dynarray was intended to go with c++14 at first. However, it was later decided that std::dynarray would be moved to an "Array TS" which may also include some std::array_view class. The main reason is that the committee did not agree on some points concerning heap-allocation versus stack-allocation in some cases (what if you try to allocate an std::dynarray with new). Therefore, it was decided that it would be delayed. There are still some ongoing discussions about what should be done.

The class won't be ready for C++14. It will probably come with the Arrays TS or C++17.

Update: std::dynarray wasn't in C++14, won't be in C++17, the Arrays TS has been discontinued, so there probably won't be anything close to std::dynarray in the standard before long. If I understand the current position of the standard correctly, rather than magical runtime-sized arrays, the committee would rather come up with a general solution for runtime-sized classes, but every suggestion so far has been rejected.

Why both runtime-sized arrays and std::dynarray in C++14?

N3639 proposes to add local runtime-sized arrays with automatic storage duration to C++.

N2648 says that in keeping with C++ practice, std::dynarrays are usable with more than just automatic variables. But to take advantage of the efficiency stack allocation, we wish to make dynarray optimizable when used as an automatic variable.

In short, C11 style runtime-sized arrays are restricted to being stored on the stack. dynarray is not, but can be optimized when stored on the stack to be as efficient as C11 style runtime-sized arrays (or so is the goal).

C11 style runtime-sized arrays can be a useful syntax still, and the cost to increase intercompilability with C isn't high: the mechanism would have to be implemented for efficient automatic dynarray anyhow. In addition, C11 style runtime-sized arrays are first class citizens, and exist regardless of use of std libraries by the programmer.

There are important differences between actual C11 runtime-sized arrays and C++1y C11-style runtime-sized arrays, not the least of which is the runtime sizeof that actual C11 runtime-sized arrays support. But basic use of it may be compatible.

Note that in the end, neither where added in C++14.

Dynamic Array - Finding Capacity

size() returns the number of elements that are held by the container while capacity is how many elements it can hold before more space must be allocated. So capacity can be greater than the size of the vector.

In your implementation, you have to create one more member capacity that will store the actual size of allocated array whereas size will hold the number of elements the container is holding.

How to make dynamic int array

You make some mistakes:

1) int* pointerToArray[] is a pointer to pointer(s). You should use int* pointerToArray.

2) *(*pointerToArray+*count)=*count; is dereferencing pointerToArray two times, you should use *(pointerToArray + *count) = *count;.

3) dynamicArrayis already a pointer, you should not use the &operator to get its address. Then counterFunction(&dynamicArray, &counter);should be converted in counterFunction(dynamicArray, &counter);.

Finally, your code should look like:

#include <stdio.h>
#include <stdlib.h>

static void counterFunction(int * pointerToArray, int * count){

while (*count < 10) {
*(pointerToArray + *count) = *count;
*count += 1;
}
}

static int * writeSortedToArray(){

//I need to store 1000 numbers at this point
int * dynamicArray = malloc(100 * sizeof(int));
int counter = 0;

counterFunction(dynamicArray, &counter);

// as suggested, finally release the array
free(dynamicArray);

return 0;
}

int main(){
writeSortedToArray();
return 0;
}


Related Topics



Leave a reply



Submit