0

I am very new to C++, and for a project I am working on, I am trying to initialize a char array buffer with the following code:

const int nodeCount = pList->getNumNodes();
const size_t bufferSize = sizeof(Node) * nodeCount;
char buffer[bufferSize];

and gets an error saying the size must be constant. I understand that this is because the compiler needs to know the size at compile time. But the size of the buffer in this case depends on the number of nodes in the list, which I have no way of knowing beforehand. How can I get around this restriction? Any help is appreciated, thank you.

I should add that this is for school and the professor does not allow STLs.

BettyQ
  • 37
  • 5
  • 5
    Very simple and easy. Use `std::string`. 'Nuff said. – Thomas Matthews Nov 11 '22 at 18:27
  • 5
    or `std::vector` – Richard Critten Nov 11 '22 at 18:27
  • If you need to change the capacity of an array during runtime, you will need to dynamically allocate a `new` array, copy the data from the old array to the new array; delete the old array. Both `std::string` and `std::vector` handle this for you. – Thomas Matthews Nov 11 '22 at 18:32
  • 1
    Forget that C-style arrays exist in the language and stop using them. Use `std::array` or `std::vector` instead. – Jesper Juhl Nov 11 '22 at 18:40
  • If you've got a reasonable upper bound, you could e.g. do `constexpr size_t MaxSize = ...; const size_t bufferSize = ...; if (bufferSize > MaxSize) { throw std::runtime_error("bufferSize " + std::to_string(bufferSize) + " is too big"); } char buffer[MaxSize]; ...` but this may have unintended consequences, e.g. if there's a function template `tempate void f(char (&)[N]);` the parameter is passed to.... – fabian Nov 11 '22 at 18:47
  • 2
    ***I should add that this is for school and the professor does not allow STLs*** Then you may have to use new[] and delete[] – drescherjm Nov 11 '22 at 18:48
  • Does this answer your question? [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – drescherjm Nov 11 '22 at 18:49
  • Telling your professor that he/she is "doing it wrong" is always an option.. – Jesper Juhl Nov 11 '22 at 18:52
  • 1
    If you are trying to implement a *linked list*, then a `char` buffer (or any buffer) with the size `sizeof(Node) * nodeCount` is *probably* not what you need in the first place. – n. m. could be an AI Nov 11 '22 at 19:14
  • @n.m. I am trying to create a buffer that is the size of the entire LinkedList, copy the data in the list over, and write that buffer to a file – BettyQ Nov 11 '22 at 19:56
  • 2
    Don't. It is not going to work. – n. m. could be an AI Nov 11 '22 at 20:25
  • 1
    Remember you can't save the pointers and restore directly – drescherjm Nov 11 '22 at 20:40

1 Answers1

0

You can create any size buffer in heap memory at run-time

char* buffer = new char[sizeof(Node) * pList->getNumNodes()];
shy45
  • 457
  • 1
  • 3