Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::array equivalent in C

Tags:

arrays

c

vector

I'm new to C and C++, and I've read that at least in C++ it's preferable to use std::array or std::vector when using vectors and arrays, specially when passing these into a function.

In my research I found the following, which makes sense. I suppose using std::vector would fix the problem of indexing outside of the variable's scope.

void foo(int arr[10]) { arr[9] = 0; }

void bar() {
    int data[] = {1, 2};
    foo(data);
}

The above code is wrong but the compiler thinks everything is fine and issues no warning about the buffer overrun.

Instead use std::array or std::vector, which have consistent value semantics and lack any 'special' behavior that produces errors like the above.

(answer from bames53, thanks btw!)

What I want to code is

float foo(int X, int Y, int l){
    // X and Y are arrays of length l
    float z[l];
    for (int i = 0; i < l; i ++){
        z[i] = X[i]+Y[i];
    }
    return z;
}

int bar(){
    int l = 100;
    int X[l];
    int Y[l];
    float z[l];
    z = foo(X,Y,l);
    return 0;
}

I want this to be coded in C, so my question is is there a std::vector construct for C? I couldn't find anything on that.

Thanks in advance, also please excuse my coding (I'm green as grass in C and C++)

like image 865
LDN Avatar asked Mar 26 '26 05:03

LDN


1 Answers

Standard C has nothing like std::vector or other container structures. All you get is built-in arrays and malloc.

I suppose using std::vector would fix the problem of indexing outside of the variable's scope.

You might think so, but you'd be wrong: Indexing outside of the bounds of a std::vector is just as bad as with a built-in array. The operator[] of std::vector doesn't do any bounds checking either (or at least it is not guaranteed to). If you want your index operations checked, you need to use arr.at(i) instead of arr[i].

Also note that code like

float z[l];
...
return z;

is wrong because there are no array values in C (or C++, for that matter). When you try to get the value of an array, you actually get a pointer to its first element. But that first element (and all other elements, and the whole array) is destroyed when the function returns, so this is a classic use-after-free bug: The caller gets a dangling pointer to an object that doesn't exist anymore.

The customary C solution is to have the caller deal with memory allocation and pass an output parameter that the function just writes to:

void foo(float *z, const int *X, const int *Y, int l){
    // X and Y are arrays of length l
    for (int i = 0; i < l; i ++){
        z[i] = X[i]+Y[i];
    }
}

That said, there are some libraries that provide dynamic data structures for C, but they necessarily look and feel very different from C++ and std::vector (e.g. I know about GLib).

like image 114
melpomene Avatar answered Mar 27 '26 20:03

melpomene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!