0

If it is possible, please tell me how to add elements to a non-fixed size array!
This is my code:

#include <iostream>
#include "../Headers/print.h"
#include <string>

using namespace std;

int cells[] = {0};


I want to add more elements to cells, but I don't find the answer, and everything I've tried doesn't work...
Thank you!

DarkerBogg
  • 31
  • 4
  • Does this answer your question? [Create an array when the size is a variable not a constant](https://stackoverflow.com/questions/57367473/create-an-array-when-the-size-is-a-variable-not-a-constant) –  Jan 25 '20 at 07:38
  • 1
    All arrays are fixed size. Even though it doesn't look like you gave this array a size, you did. The size is inferred from the initializer. `cells` is an `int[1]`, and you cannot change its size because that would change its type. Use something that isn't an array. – HTNW Jan 25 '20 at 07:39
  • 1
    The C++ solution for this problem is std::vector. If you want to use a plain C array for some reason, use a pointer and realloc. – Uri Raz Jan 25 '20 at 08:28
  • Does this answer your question? [How to create a dynamic array of integers](https://stackoverflow.com/questions/4029870/how-to-create-a-dynamic-array-of-integers) – Daniel Langr Jan 25 '20 at 09:29

2 Answers2

1

How can I add elements to a non-fixed size array in C++?

First of all, the size of the array is fixed. You can just skip specifying the size when you initialize an array like this,

int cells[] = {0};

The size of the array is inferred automatically by the compiler.

If you want dynamic size arrays, use vector. See the code below for an example,

#include <iostream>
#include <vector>
int main(){
    int cells1[] = {0}, cells2[] = {0,1,2,3,4,5};
    std::cout<<"Size of cells1 array: "<<sizeof(cells1)/sizeof(cells1[0])<<std::endl; // 1
    std::cout<<"Size of cells2 array: "<<sizeof(cells2)/sizeof(cells2[0])<<std::endl; // 6
    std::vector<int> cells3{0}; // Vector - a dynamic array
    std::cout<<"Size of cells3 vector: "<<cells3.size()<<std::endl; // 1
    cells3.push_back(1); // Append an element to the end of vector
    std::cout<<"Size of cells3 vector: "<<cells3.size()<<std::endl; // 2
    return 0;
}

Output

Size of cells1 array: 1
Size of cells2 array: 6
Size of cells3 vector: 1
Size of cells3 vector: 2
Ajay Dabas
  • 1,404
  • 1
  • 5
  • 15
0

You can get referrence to the first element &cells[0] add to this no of elements multiplied by their size + 1 * sizeof(int) so you will get address of first byte after your array. Then you can allocate memory at this address using: https://stackoverflow.com/a/19103230/11685627 Allocate sizeof(int)*no of elements. Than you should be able to use your array as it was extended in size. Remember to "free" the memory after your array goes out of the block scope. https://learn.microsoft.com/en-gb/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc?redirectedfrom=MSDN https://learn.microsoft.com/en-gb/windows/win32/api/memoryapi/nf-memoryapi-virtualfree Ofc this is the explicitly hard way approach.

Gen0me
  • 115
  • 8