dynamic-arrays

Is it safe to return a VLA?

☆樱花仙子☆ 提交于 2019-12-24 01:08:22
问题 The following code uses the heap: char* getResult(int length) { char* result = new char[length]; // Fill result... return result; } int main(void) { char* result = getResult(100); // Do something... delete result; } So result has to be deleted somewhere, preferably by the owner. The code below, from what I understand, use an extension called VLA, which is part of C99, and not part of the C++ standard (but supported by GCC, and other compilers): char* getResult(int length) { char result[length

How to access array of flexible arrays in cache friendly manner?

两盒软妹~` 提交于 2019-12-23 19:05:38
问题 I have records with flexible array member typedef struct record { unsigned foo; signed bar; double number[]; } record; I have multiple records with the same amount of numbers so I can arrange them in array. I would like to allocate them into one continuous memory space. const unsigned numbers = ...; const unsigned records = ...; const size_t record_size = sizeof(record) + numbers*sizeof(double); record *prec = malloc(records*record_size); So now I know record_size and I can access it but what

Cast between const char* [][3] and std::array< const char*, 3 >*

大憨熊 提交于 2019-12-23 10:54:06
问题 I need a way to cast between these two types of variables: std::array< const char*, 3 >* foo; const char* foo[][3]; Because I need to be able to pass both types to a function. The function can be defined either of these ways, whichever makes conversion easier: void bar( std::array< const char*, 3 >* param ); void bar( const char* param[][3] ); In this question, Jarod42 suggests using the method here. Is there a cleaner way to do this? Edit in response to dyp's link This reinterpret_cast did

Removing elements from dynamic arrays

纵饮孤独 提交于 2019-12-21 19:39:42
问题 So, I have this: #include <stdio.h> #include <stdlib.h> #include <string.h> void remove_element(int* array, int sizeOfArray, int indexToRemove) { int* temp = malloc((sizeOfArray - 1) * sizeof(int*)); // allocate an array with a size 1 less than the current one memcpy(temp, array, indexToRemove - 1); // copy everything BEFORE the index memcpy(temp+(indexToRemove * sizeof(int*)), temp+((indexToRemove+1) * sizeof(int*)), sizeOfArray - indexToRemove); // copy everything AFTER the index free

How to initialize the dynamic array of chars with a string literal in C++?

北慕城南 提交于 2019-12-20 02:52:55
问题 I want to do the following: std::unique_ptr<char[]> buffer = new char[ /* ... */ ] { "/tmp/file-XXXXXX" }; Obviously, it doesn't work because I haven't specified the size of a new array. What is an appropriate way to achieve my goal without counting symbols in a string literal? Usage of std::array is also welcome. Update #1: even if I put the size of array, it won't work either. Update #2: it's vital to have a non-const access to the array as a simple char* pointer. 回答1: Here's a solution

Making a dynamic array that accepts any type in C

眉间皱痕 提交于 2019-12-19 12:24:39
问题 I'm trying to find a way to make a struct to hold a dynamic array that can work with any data type (Including user defined data types), so far this is what I came up with. #define Vector(DATATYPE) struct { DATATYPE* data; size_t size; size_t used; } typedef Vector(int) int_Vector; int main(int argc, char* argv[]){ int_Vector vec; return 0; } While this works I was wondering, is this good practice? Should I be doing something like this or is there a better method? Also is there a method to

dynamic array size determined at runtime in ada

青春壹個敷衍的年華 提交于 2019-12-18 06:06:26
问题 Is it possible to have an array with size that is determined at runtime like so, Procedure prog is type myArray is array(Integer range <>) of Float; arraySize : Integer := 0; theArray : myArray(0..arraySize); Begin -- Get Array size from user. put_line("How big would you like the array?"); get(arraySize); For I in 0..arraySize Loop theArray(I) := 1.2 * I; End Loop; End prog; Is there a way to achieve this result other than using dynamically Linked Lists or another similar structure? Or is

How to increase array size on-the-fly in Fortran?

风格不统一 提交于 2019-12-17 18:41:20
问题 My program is running though 3D array, labelling 'clusters' that it finds and then doing some checks to see if any neighbouring clusters have a label higher than the current cluster. There's a second array that holds the 'proper' cluster label. If it finds that the nth adjoining cluster is labelled correctly, that element is assigned to 0, otherwise is assigns it to the correct label (for instance if the nth site has label 2, and a neighbour is labeled 3, the 3rd element of the labelArray is

Why doesn't C++ support dynamic arrays on the stack? [closed]

我们两清 提交于 2019-12-17 18:27:51
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . In C99 this was legal: void f(size_t sz) { char arr[sz]; // ... } However, this - dynamically sized stack arrays - has been dropped in

NumPy style arrays for C++?

断了今生、忘了曾经 提交于 2019-12-17 17:28:50
问题 Are there any C++ (or C) libs that have NumPy-like arrays with support for slicing, vectorized operations, adding and subtracting contents element-by-element, etc.? 回答1: Here are several free software that may suit your needs. The GNU Scientific Library is a GPL software written in C. Thus, it has a C-like allocation and way of programming (pointers, etc.). With the GSLwrap, you can have a C++ way of programming, while still using the GSL. GSL has a BLAS implementation, but you can use ATLAS