6

I am used to java and php and now I need to write some c++ code. I got difficulties to create a BYTE-array with dynamic size. How to achieve this?

int byteSize = shm.getMemorySize();
BYTE byte[44]; // replace 44 by byteSize
2
  • 1
    GCC supports writing BYTE byte[byteSize]; Commented Mar 28, 2014 at 17:22
  • Depends. What is BYTE? Commented Mar 28, 2014 at 17:23

6 Answers 6

9

You should use std::vector unless you have very specific reason to use arrays. Normally, in the similar context where you were using arrays in other languages in C++ the default choice should be std::vector.

Sign up to request clarification or add additional context in comments.

4 Comments

Last assumption is shared on only dynamic arrays, non the fixed size ones. For fixed size he can use std::array
He can, but it's rarely worth the effort, and pretty much never if you're new.
And the size?? You should mention resize() perhaps.
He can look up how to use it.
7

Never use a naked pointer or it is open door for bugs and memory leaks, instead, here some alternatives :

int len = something;
std::vector<char> buffer(len,0);

or c++11 smart pointer

std::unique_ptr<char[]> buffer{ new char[len] };

or c++14 with make_unique

auto buffen = std::make_unique<char[]>(len);

Comments

3

Use a vector, unless you absolutely need to handle memory yourself. Also, this is more of a preference thing, but I prefer to use uint8_t instead of BYTE. Its a bit more compliant as it doesn't depend on GCC.

#include <vector>
#include <cstdint>
...
std::vector<uint8_t> foo;

or

#include <cstdint>
...
uint8_t* data;
data = new uint8_t[10];
...
delete[] data;

Comments

3

You can use the STL standard container class std::vector to have a dynamic resizable array:

#include <vector> // For std::vector
....

int byteSize = shm.getMemorySize();

// Create a vector of 'byteSize' bytes.
std::vector<BYTE> bytes(byteSize);

You can access vector elements using the usual [] syntax (e.g. bytes[0], bytes[1], ...bytes[i]).

vector's destructor will automatically release the memory allocated by the vector, so you don't have to pay attention to freeing memory.

You can use vector's push_back() method to add items at the end of the vector (with automatic resizing). You can use vector's clear() method to empty it.
And vector's size() method returns the current number of items in the vector.

If you want to learn more about std::vector, you can watch this great introductory video by Stephan T. Lavavej.

Comments

2

One way to do it is to use dynamic memory allocation with new

BYTE* data = new BYTE[byteSize];
data[0] = 0;
delete [] data;

However this approach in the hands of a beginner can lead to memory corruptions, crashes, memory leaks, and all sorts of behaviors that are hard to find and fix.

A better way is to use std::vector that is almost safe against the problems the first approach has:

std::vector<BYTE> data;
data.resize(byteSize);
data[0] = 0;

Comments

0

You can create the dynamic array as follows:

int byteSize = shm.getMemorySize();
BYTE* byte = new BYTE[byteSize];
//Use the byte
delete [] byte;

3 Comments

Without context, nobody can rightfully say this is the wrong solution. But is only has a small niche left...
Without context, you certainly should not present it categorically as the correct solution. That is the far more important way of looking at this.
@tmp: The term "dynamic array" means that the array can be resized, elements removed, etc. How does recommending new[] and delete[] accomplish this? It can't do it -- you have to write all sorts of code to do the basics of what a dynamic array should be able to accomplish. So no, the real answer is to use a container such as std::vector, which has all of tools needed to manipulate a dynamic array properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.