ISO C++ forbids variable length array 'k' [-Wvla]|

i need a 2d array with lenghts that can change
how can i get around this?
my program compiles but i don't want to leave it with big warnings everywhere.
i saw that you could do a pointer like
char myarr= new char [sizVar][sizeVar2];
but for 2 dimensional arrays it won't let me use the second variable beacause of some reason.
the first size variable is just an integer,and the second size value is the lenght of a string .
1
2
3
char** myarr= new char*[sizVar];
for (int i = 0; i < sizVar; ++i)
    myarr[i] = new char[sizeVar2];

http://www.cplusplus.com/forum/articles/17108/
> i need a 2d array with lenghts that can change

Do yourself a favour: use std::vector<> http://www.mochima.com/tutorials/vectors.html

std::vector< std::vector<char> > myarr( sizVar, std::vector<char>(sizeVar2) ) ;
hi sorry for the late reply
in the article minipaa posted
it pretty much says everything is evil
i am so confused right now..
ill try using the vector approach
do i just stop using arrays completely?
are vectors that good?
Vectors save you from hassle and dangers of manual memory management. They have the same perfomance overhead as array of pointers to arrays (nested new) do, and are fine for most uses.
If I needed minimal overhead and maximum perfomance, I would use 1D mimics (wrapped in a vector or a custom class)
> do i just stop using arrays completely?

No, you need not. Arrays are not evil per se; the standard library which provides a convenient thin wrapper, std::array<>, over it is not evil.

A simple approach that works well in most cases is:
If the number of elements in a sequence is a constant known at compile-time, consider using a c-style array (std::array<>). If not, consider using a std::vector<>
so why use this instead of just standard declaration?
what does it mean to wrap something?
so why use this instead of just standard declaration?
To lessen amount of code you have to write and make it less error-prone.

With nested new you need: allocate outer array, allocate inners arrays in a loop, make sure that all accesses are not out of bounds, deallocate inner arrays, deallocate outer array.
One one-off error with allocation (very common with arrays) and your program behavior is undefined. One error with range checking, and your program is undefined. Mess delete order and your program is undefined. Make a mistake in a loop and your program is undefined.
Dozens of places where something can be wrong.

Vector (or array) can take most of those resposibilities on itself (allways allocation and deallocation and probably range checking if you will say so explicitely). You do not need to fear memory leaks, double delete and other problems which usually goes with manual memory management.
wow alright
i had no idea so many things hid behind arrays.
ill use vectors from now on.
the problem i'm having now is that in my class when i try to compile the program it says vector not declared in this scope even though i have it in the header file.i tried putting it in the .cpp file but it doesn't work either
this is the code thats supposed to use a vector

1
2
3
4
5
6
7
8
std::string simpleTransposition::railFenceCipher( int key,std::string rawText){
const int t = rawText.length();

vector<int> asda;



}




and this is the header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef SIMPLETRANSPOSITION_H
#define SIMPLETRANSPOSITION_H
#include "generalpurpose.h"
#include <iostream>
#include <vector>
class simpleTransposition:public GeneralPurpose
{
    public:
        void setSpacePermission ( bool haveSpaces );
        simpleTransposition();
        virtual ~simpleTransposition();
        std::string railFenceCipher(int key,std::string rawText);
   
    protected:
    private:
    bool haveSpaces_;

};

#endif // SIMPLETRANSPOSITION_H 



can you see any errors?
i sure can't i've been at it for a while.
std::vector<int> asda; And notice that your header does not uses <vector> header, so you can remove it here. But your implementation file does, so it is a good idea to place it here.
Last edited on
Topic archived. No new replies allowed.