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 ? This is the first time I am using a pointer.This one is the error : "Segmentation fault (core dumped)" when compiling

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

---------------------------------------------

//Integer.h

#ifndef INTEGER_H
#define INTEGER_H

class Integer
{
     public:
     Integer() {*value = 0;}
     Integer( int intVal );
     Integer(const Integer& other);//copy constructor

     ~Integer(void);
     int getInteger() const;
     void setInteger( int newInteger );
     Integer& operator=( const Integer& rhInteger );

     private:
     int* value; 
};

#endif

-----------------------------------------

// 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;
}

Recommended Answers

All 4 Replies

You are getting core dump becuse the pointer has never been allocated any memory. Lines 6 and 11 attempt to store an integer into a pointer that contains an invalid address. Is it a requirement of the assignment to use a pointer?

@Dragon : Yes, I have to use pointer for this one. If you have any hints please help. What is wrong with line 6 and 11. I thought " * value " is the number the pointer "value" points to and I want it to equal "intVal"

I thought * value is the number the pointer value points to

Yes, but in order for a pointer to point to something, you need to allocate some memory and set the pointer to point to that memory. Before you initialize a pointer, it just has some arbitrary value and thus, points to some random memory location, which is why you get a seg-fault when you try to read/write that memory (which is most likely not part of the memory dedicated to your program).

To allocate memory, you should do something like this:

value = new int;

after which the pointer value will point to one integer. After that point, you can write its value with a statement like *value = intVal;. So, each of your constructors need to make such an allocation with new before it proceeds to set the value.

For a more comprehensive tutorial on writing all those functions when holding a resource, like dynamically allocated memory, refer to here.

@mike : Thanks a lot. Your hint is helpful. I think I get it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.