Newbie needs help with pointer , destructor,copy constructor

I get stuck for this one for 3-4 hours, try various sources but no help, would you please point out what is wrong with my 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

//This is the code for Integer.h

 #ifndef INTEGER_H
#define INTEGER_H
 

class Integer
{
	 public:
	 Integer() {*value = 0;}
	 Integer( int intVal );
	 Integer(const Integer& other);//copy constructor
		
	 ~Integer();//destructor
	 int getInteger() const;
	 void setInteger( int newInteger );
	 Integer& operator=( const Integer& rhInteger );
	  
	 private:
	 int* value; 
};
 
#endif 


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
  // Integer.cpp
#include <cstddef>
#include "Integer.h"
Integer::Integer( int intVal )
{
	*value = intVal;
}

//copy constructor
 Integer::Integer(const Integer& other)
 {
 
 *value=*(other.value);
 
 } 
//deconstructor 
Integer::~Integer(){

if(value != NULL) delete value;

}	
 
 
 
int Integer::getInteger() const
{
	return *value;
}
 
void Integer::setInteger( int newInteger )
{
	*value = newInteger;
}
 
Integer& Integer::operator=( const Integer& rhInt )
{
	 *value = *(rhInt.value);
	 return *this;
}


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
  // IntegerTest.cpp
#include <iostream>
#include <cstdlib>
#include "integer.h"
 
using namespace std;
 
void displayInteger( char* str, Integer intObj )
 {
	cout << str << " is " << intObj.getInteger() << endl;
 }
 
int main( int argc, char* argv[] )
{
	 Integer intVal1();
	 Integer intVal2(10);
	 
	 displayInteger( "intVal1", intVal1 );
	 displayInteger( "intVal2", intVal2 );
	  
	 intVal1 = intVal2;
	 
	 displayInteger( "intVal1", intVal1 );
	 
	 return EXIT_SUCCESS;
}

line 15, in main, Integer intVal1(); remember that we do not include parenthesis when creating a new object using the default constructor
Last edited on
Thanks, I fixed that already, This one is the true error :

"Segmentation fault (core dumped)"

This must be something involving with the pointer but I can not figure out.
not sure about this:
1
2
3
4
5
Integer::~Integer(){

if(value != NULL) delete value;

}

your trying to delete a NOT dynamically allocated variable, you must dynamically allocate first value using the constructor

1
2
3
4
5
Integer::Integer()
{
    value = new int;
    *value = 0;
}


another thing:
1
2
3
4
5
void displayInteger( char* str, Integer intObj )
 {
	cout << str << " is " << intObj.getInteger() << endl;
 }
 

should be:
1
2
3
4
5
void displayInteger( const char* str, Integer intObj )
 {
	cout << *str << " is " << intObj.getInteger() << endl;
 }
 
Last edited on
Thanks nevermore28, I am clueless in how to proper create a destructor. This is my very first assignment to use pointer, constructor, destructor. I also don't understand what u mean by saying "dynamically allocated variable" .
Dynamically allocated variables are those allocated with new, and are the only ones you should be attempting to delete. As shown above, you did not new your pointer in the constructor (bad) and then attempted to delete it in the destructor.
Ok, I have managed to let part of it run. However , problem persist.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef INTEGER_H
#define INTEGER_H
 
class Integer
{
	 public:
	 Integer() {*value = 0;}
	 Integer( int intVal );
	 Integer(const Integer& other);//copy constructor
		
	~Integer(void); //deconstructor
	 int getInteger() const;
	 void setInteger( int newInteger );
	 Integer& operator=( const Integer& rhInteger );
	  
	 private:
		int* value; 
};
 
#endif 


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
Integer::Integer( int intVal )
{
	value = new int;
	*value = intVal;
}
 Integer::Integer(const Integer& other)
 {
 //copy constructor
 *value=*(other.value);
 
 } 
	
 //deconstructor 
Integer::~Integer(){
delete value;
// if(value != NULL) delete value;

}	
 
 
int Integer::getInteger() const
{
	return *value;
}
 
void Integer::setInteger( int newInteger )
{
	*value = newInteger;
}
 
Integer& Integer::operator=( const Integer& rhInt )
{
	 *value = *(rhInt.value);
	 return *this;
}


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
// IntegerTest.cpp
#include <iostream>
#include <cstdlib>
#include "Integer.h"
 
using namespace std;
 
void displayInteger( const char* str, Integer intObj )
 {
	 
	cout << *str << " is " << intObj.getInteger() << endl;
 }
 
int main( int argc, char* argv[] )
{
	 
	 Integer intVal1(0);
	
	 cout<<"intVal1 is "<<intVal1.getInteger()<<endl;
	 Integer intVal2(10);
	 
	 
	 cout<<"intVal2 is "<<intVal2.getInteger()<<endl;
	 //cout<<"in line 24"<<endl;
	 
	// displayInteger( "intVal1", intVal1 );
	 
	// displayInteger( "intVal2", intVal2 );
	  
	 intVal1 = intVal2;
	 cout<<"when intVal1=intVal 2, intVal1 is "<<intVal1.getInteger()<<endl;
	 //displayInteger( "intVal1", intVal1 );
	 
	 return EXIT_SUCCESS;
}


The above will runs perfectly if you let the dlsplayInteger function out. However, you will get an error "Segmentation fault (core dumped)" if they are included.
The problem lies with your copy constructor. You do not initialize/assign your value variable with some memory location, e.g. value = &FOO; or value = new int; (the latter probably being the the correct one based on your code).
Attempting to dereference a pointer to some random location will most likely cause that segmentation fault.
you are assigning 0 to the value pointed by value which is wrong since you don't have any variable to be pointed by value

Still you are deleting a non-dynamically allocated variable.

you can't just do:
1
2
int *value;
*value = 0; // Wrong ! 

this is invalid since value does't point to anything, or any memory address and still you are assigning a value to the value pointed by it. Remember that a pointer must first store an address of a variable:

If you want to assign a value to the value pointed by value, you should either use another variable, OR, dynamically allocate value:
1
2
3
int *value = new int; // new returns a memory address to value causing value to now be valid and can be dereferenced
*value = 0;     // now this is valid since value now has an address stored on it and can now be dereferenced
delete value;  // you should add this in your destructor 


based on this, then your constructor definition should be:
1
2
3
4
5
Integer::Integer()
{
    value = new int;
    *value = 0;
}
Last edited on
Topic archived. No new replies allowed.