Standard array with non-const size?

Hi, I’m currently in the process of getting my Particle firmware to compile on Windows so I can write unit tests. I’m basically mocking a bunch of the Particle/Arduino APIs to make it compile. I don’t actually care about making this part work as I’m writing unit tests – not integration tests.

Anyways, as I was compiling in VC++ I came upon some code in a few libraries where arrays were being created like this:

uint8_t buffer[getBufferSize(messageLength)];

I’m confused here. I thought you had to use const to set standard array sizing but these arrays are dynamically sized.

I can easily convert this code to use dynamic arrays but I prefer not to change these libraries:

int bufferSize = getBufferSize(messageLength);
uint8_t* buffer = new uint8_t[bufferSize];

Can someone explain why the Particle compiler doesn’t complain about the first case? Also, is there a compiler flag I can set so VC++ doesn’t care either?

Thanks

Ok, I found Arrays of Variable Length and how VC++ doesn’t support them. I haven’t written C/C++ in a very long time so this was new to me.

you can use <vector>

just be aware that you risk some fragmentation… I’ve used it in a very lightweight fashion in some of my utility libraries as well as in a broader sense, apps (i.e vector of structs), haven’t had any problems so far.

and to answer your question, it’s a gcc thing.

Yeah… the issue is that I don’t want to modify these libraries. This code won’t work with vectors without a huge refactor and would probably be a large perf hit.

I’m trying to get gcc working with visual studio and that will solve the issue.

Thanks though

1 Like