[Array Class]:Why is there no segmentation fault with my assignment operator?

...Not that I wish it did, but I want to understand why!

Hello together,

I'm new to this forum because since two weeks I am trying to learn c++. Up to now the c++ reference of this website was a huge help for me, but now I don't know whom to ask. Maybe you can help me with this problem.

My code is a simple array class with standard constructor and assignment operator:
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
#include <stdio.h>
#include <iostream>

class Array {
    public:
        // Constructors
        Array() {
		size = 0; 
		array = new double[size]; 
		fill(0);
	}
        Array(int x) {
		size = x; 
		array = new double[size]; 
		fill(0);
	}	
        //assignment  Operator 
	Array& operator= (const Array & B) {
		size = B.size;
		std::copy(B.array, B.array+size, array); 
		return *this;
	}
        //initialize the whole array with a constant value
        void fill(const double & in)
        { 
	for(int i = 0; i < size; i++) array[i] = in;
	}

    	double* array;   
   	int size; 
};

Now what I don't understand is outlined here:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
	int * ip[0];
	cout << ip[1]; //may cause a segfault
	
	Array a;
	Array b(2); 
	a = b;
	cout << endl << a(1); // may not cause a segfault?
	return 1;
}


Can you please explain to me why this is so?

As I found in the reference, std::copy does something like this:
1
2
3
4
5
6
template<class InputIterator, class OutputIterator>
  OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )
{
  while (first!=last) *result++ = *first++;
  return result;
}


Does this not imply that std::copy writes something to anywhere and might cause a segmentation fault? If so, how can I solve this problem, if not so, why not?

I am completely lost and would really appreciate any help!!

Best,
wedge
Last edited on
Usually you get a segfault when trying to access memory that is not used by the program.

1
2
int * ip[0];
cout << ip[1]; //may cause a segfault 
The chances that this generates a segfault is small because the array is allocated on the stack and the bytes after the array will most likely also belong to the stack.

1
2
3
Array a;
Array b(2); 
a = b;
This may generate a segfault. If you increase the size of b it will more likely give you a segfault.

cout << endl << a(1); // may not cause a segfault
This doesn't even compile.

Last edited on
Hello Peter87,

thank you for your reply. Do you have an idea about how I could construct my assignment operator, such that simething like
1
2
3
Array a;
Array b(2); 
a = b;

becomes possible?

And sorry for the compile error, I meant:
 
cout << endl << a.array[1];

which for me does not look far from my int * ip.
I found a solution for my problem: I just need to use an empty default constructor and pointers for this job:

1
2
3
4
Array* a;
Array b(2);
Array c(b);
a = &c;


Thank you for your reply!
wedge
@wedgeCountry
That makes not too much sense. You have to allocate the appropriate memory:
1
2
3
4
5
6
7
Array& operator= (const Array & B) {
		size = B.size;
		delete[] array;
		array = new double[size];
		std::copy(B.array, B.array+size, array); 
		return *this;
	}
Topic archived. No new replies allowed.