-1

I'm having a hard time with an error that keeps popping up. It's a homework assignment and a large part of it is included in a separate .h file, so I won't post all the code to keep things intact. Here are the relevant parts:

//In the .h:

 class array_list
{
 private:   
unsigned int *  m_storage;
// points to current position in the list

// in documentation # marks the current position in the list
// ie. if l={#1,2,3} the 1 is the current position in the list
// if l={1,2,#3} the 3 is the current position in the list
// the # is ONLY for documentation purposes
unsigned int    m_current; 
unsigned int    m_size;
unsigned int    m_capacity;

// etc.

//Guide:

// Construct a new, empty list.
// 
// Pre-conditions:
//  none
// Post-conditions:
//  m_storage is initialized to point to an array of size INIT_SIZE 
//  m_capacity is initialized to INIT_SIZE
//  m_current is set to -1
//  m_size is set to 0

//What I've written:

array_list::array_list()
{
int arrayOf[INIT_SIZE];
m_storage = arrayOf[];  /* <---THE PROBLEM LINE */
m_capacity = INIT_SIZE;
m_current = -1;
m_size = 0;
}

For some reason, I get the error that the compiler expected a primary-expression before the ']' token on the line indicated. I've been through my notes and done some Googling and it really does seem like this is the way to declare an array and point to it with a pre-defined pointer, no? Can anyone help me fix this? Thank you.

Mock
  • 309
  • 1
  • 3
  • 8
  • 1
    `m_storage = (unsigned int*)arrayOf`. Why do you feel you need this empty pair of brackets for? It's not valid syntax. And make `arrayOf` an array of `unsigned int` (or else `m_storage` an `int*`), so you don't need the cast. Oh, and `arrayOf` is going to be destroyed when the constructor returns, leaving `m_storage` a dangling pointer. – Igor Tandetnik Oct 12 '14 at 22:12
  • Why are you creating a local array anyway? Shouldn't you just `m_storage = new unsigned int [INIT_SIZE];` – Jerry Jeremiah Oct 12 '14 at 22:20

2 Answers2

0
m_storage = arrayOf[];

Is invalid syntax.

m_storage = arrayOf;

would be starting down the right track (int [] decays to an int*) but there are still problems because m_storage is defined as:

unsigned int *  m_storage;

and so any data pointed to by m_storage should be unsigned which either requires a cast:

m_storage = reinterpret_cast<unsigned int *>( arrayOf );  

or (better solution) you define the array as an array of unsigned int:

unsigned int arrayOf[INIT_SIZE];

Of course there are still problems here.
This is because you are creating the array on the stack (of the function), and then letting it go out of scope, invalidating the pointer.
There are again two methods of fixing this:

Initialise the buffer in the object:

In the header file (class definition):

class array_list
{
private: 
    unsigned int  m_storage[INIT_SIZE];
    unsigned int  m_current  = -1; 
    unsigned int  m_size     =  0;
    unsigned int  m_capacity = INIT_SIZE;
    //...
}

This sets the defaults for when array_list is constructed.

The alternative is maybe closer to what you intended (I'm not 100% sure) and involves allocating the memory on the stack:

array_list::array_list()
{
    m_storage = new unsigned int[INIT_SIZE];
    m_capacity = INIT_SIZE;
    m_current = -1;
    m_size = 0;
}

Just bear in mind that you need to write the destructor for the class now, using delete[] to unalocate the new'd memory:

array_list::~array_list()
{
    delete[] m_storage;
}

And if you do this, you should go the whole way and implement the rule of three (or five).

Community
  • 1
  • 1
Baldrickk
  • 3,827
  • 1
  • 12
  • 25
-1

Rewrite this statement

m_storage = arrayOf[];  

as

m_storage = reinterpret_cast<unsigned int *>( arrayOf );  

Though it looks strange that m_storage has type unsigned int * and you are trying to assign an object of type int * to it.

And as hvd has pointed out you are assigning an address of a local array to data member m_storage. So the fun ction is wrong in whole because the array will be destroyed after exiting the function and the pointer will be invalid.

Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
  • 2
    `m_storage` will point to a local array which gets destroyed after the constructor ends, so this is not good enough to actually make it work. –  Oct 12 '14 at 22:14