ArrayList Class Throws Segfault on Copy Ctor Call

I don't see any obvious problems with this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef ARRAYLIST_H
#define ARRAYLIST_H

#include <iostream>
#include <string>
#include "ListExceptions.h"

template <class T>
class ArrayList
{
protected:
    //data
    T* data;
    unsigned int size;
    unsigned int space;
    const static unsigned int EXPFACT = 32;

/*...*/

template <class T>
ArrayList<T>::ArrayList(const ArrayList<T>& c)
{
    if(size > 1)
    {
        delete[] data;
    }
    else if(data != nullptr)
    {
        delete data;
    }

    data = new T[c.getSize()];

    for(int i = 0; i < c.getSize(); i++)
        data[i] = c[i];

    size = c.getSize();
    space = size;
}

/*...*/

#endif 


How could this throw a SISGEV?
Last edited on
Remember that is a constructor so size, space and data has not been initialized yet.
Topic archived. No new replies allowed.