简体   繁体   中英

Freeing memory with Pointer Arithmetic

C++ newb here. I'm trying to write my own implementation of an array using only pointers, and I've hit a wall I don't know how to get over.

My constructor throws this error

array.cpp:40:35: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]

When my array initializes I want it to free up all the spaces in the array for ints.

Array::Array(int theSize){
size = theSize;
int *arrayPointer = new int; 
int index = 0;
while(theSize > index){
  *(arrayPointer + index) = new int; //This is the trouble line.
  ++index;
 }
}

What am I doing wrong stackoverflow?

arrayPointer points to a single int , it does not point to an array of int* , which this line would require:

*(arrayPointer + index) = new int;

but the type of *(arrayPointer + index) is an int , hence the compiler error.

To allocate an array of int :

int* arrayPointer = new int[size];

If this is intended to initialise a member variable then:

arrayPointer = new int[size];

otherwise arrayPointer would be local to the constructor. As the class now has a dynamically allocated member you need to either implement both copy constructor and assignment operator or prevent copying (see What is The Rule of Three? ). Remember to delete[] arrayPointer in the destructor.


Just mentioning std::vector<int> , even though this is a learning exercise.

Do as follows:

#include <cstddef>

template <typename T>
class Array
{

public:

    T* const arrayPointer; // arrayPointer can't be reallocated
    const size_t size; // size can't change

    Array(const int theSize) : arrayPointer(new T[theSize]),
                               size(theSize) {}

    ~Array() {
        delete[] arrayPointer;
    }

private:

    Array(const Array& other) {} // forbid copy

    Array& operator= (const Array& other) {} // forbid assignment

} ;
  • Why to use template <typename T> ? So you can have an array of any type.
  • Why to use new T[ theSize ] ? So you can allocate theSize elements at the same time.
  • Why to use : arrayPointer( new T[ theSize ]) ? So in case allocation fails (due to big theSize) the object fails without initialization. It is callled RAII.
  • Why to use delete [] arrayPointer ? Because you used new[] and you have to deallocate the whole array.
  • Why those const 's? To avoid anyone changing the size of the array and making the fields inconsistent.
  • What are these private methods? They are forbiding copies, so no one can make array1 = array2; delete array2; array1 = array2; delete array2; , what would deallocate the arrayPointer of array1.

Usage (it will allocate array of 10 int:)

Array< int > arr( 10 ) ;

Access:

arr.arrayPointer[ 0 ] = 5 ;

Note - you can access arrayPointer at the cells of range 0..9. You can add operator[] to your class in order to avoid using arrayPointer and to use arr[ 0 ] .

The line you mentioned is trying to set a int* into a int var => Dereferencing a int* provide a int :

*(arrayPointer + index) // is a int

Anyway you are moving in memory (and dereferencing memory) you haven't reserved. So you can access a protected memory zone by doing this instruction.

Replace all your constructor with:

Array::Array(int theSize)
{
    size = theSize;
    arrayPointer = new int[theSize]; // Replace your local var with a member one. Else you will lose your array ;)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM