3

i have a class in which it's protected section i need to declare an array with unknown size (the size is given to the constructor as a parameter), so i looked around and found out that the best possible solution is to declare an array of pointers, each element points to an integer:

int* some_array_;

and simply in the constructor i'll use the "new" operator:

some_array_ = new int[size];

and it worked, my question is: can i declare an array in a class without defining the size? and if yes how do i do it, if not then why does it work for pointers and not for a normal array?

EDIT: i know vecotrs will solve the problem but i can't use them on my HW

3
  • You probably don't want protected data - it's almost always A Bad Idea. And use std::vector, not an explicitly dynamically allocated array. Commented Jun 3, 2017 at 14:45
  • 1
    There a 2 ways oft defining an array... 1. With a size you define while compiling int Name[10] then it is on the stack... There you don't need the delete command, because it gets automatic deleted when you go out of the space. 2. An dynamic array int* Name = new int[n], then it is on the heap. So it'll stay until you say delete[] Name Commented Jun 3, 2017 at 14:52
  • 1
    some_array_ is not an array of pointers, it's one pointer which points to the first element of an array. An array of pointers would be int* x[size]; a pointer to an array int (*x)[size]. Commented Jun 3, 2017 at 15:47

3 Answers 3

1

You have to think about how this works from the compiler's perspective. A pointer uses a specific amount of space (usually 4 bytes) and you request more space with the new operator. But how much space does an empty array use? It can't be 0 bytes and the compiler has no way of knowing what space to allocate for an array without any elements and therefore it is not allowed.

answered Jun 3, 2017 at 14:55
Sign up to request clarification or add additional context in comments.

Comments

0

You could always use a vector. To do this, add this line of code: #include <vector> at the top of your code, and then define the vector as follows:

vector<int> vectorName;

Keep in mind that vectors are not arrays and should not be treated as such. For example, in a loop, you would want to retrieve an element of a vector like this: vectorName.at(index) and not like this: vectorName[index]

answered Jun 3, 2017 at 14:46

1 Comment

ye i know, vectors easily solve that problem, problem is i can't use them on my assignment :(
0

Lets say that you have an integer array of size 2. So you have Array[0,1] Arrays are continuous byte of memery, so if you declare one and then you want to add one or more elements to end of that array, the exact next position (in this case :at index 2(or the 3rd integer) ) has a high chance of being already allocated so in that case you just cant do it. A solution is to create a new array (in this case of 3 elements) , copy the initial array into the new and in the last position add the new integer. Obviously this has a high cost so we dont do it.

A solution to this problem in C++ is Vector and in Java are ArrayLists.

answered Jun 3, 2017 at 14:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.