getting a seg fault, first time with classes and pointers

Mar 12, 2013 at 5:32am
my code is seg faulting and im not sure whats causing it. i thought it was the pointers not being deleted but i think i did that correctly.

my header
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


#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

class Posting
{
        public:
                Posting();
                ~Posting();

                void Create_Posting(Posting *post_ptr, Posting *type, Posting *title, Posting *description,Posting *email);  //create a new
                                                                                //posting of this title.
                void Display_Posting();
                bool is_type(Posting *post_ptr, Posting *type);  //is the posting a job, housing, item for sale, or free stuff

        private:
                int cost;
                char *description;
                char *title;
                char *type;
                int sq_feet;
                int rooms;
                int rent;
                char *location;
                char *email;
                int pay;
};


my post.cpp file
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "main.h"

using namespace std;

Posting::Posting()
{	
	 int size(0);
         int cost(0);
         description = new char[size];
         title = new char[size];
         type = new char[size];
         int sq_feet(0);
         int rooms(0);
         int rent(0);
         location = new char[size];
         email = new char[size];
         int pay(0);
	cout <<" objects are being created" << endl;
}

void Posting::Create_Posting(Posting *post_ptr, Posting *type, Posting *title, Posting *description, Posting *email)
{
        char again('y');
        char temp[1000];

        cout << "Would you like to create a new posting? (y/n): " << endl;
        cin >> again;

        while (again == 'y' || again == 'Y')
        {
                if(is_type(post_ptr, type))
                {

                        cout << "Create a title: " << endl;
                        cin.getline(temp, 1000);
                        cin.ignore(1001,'\n');
                        post_ptr -> title = new char[strlen (temp) + 1];
                        strcpy(post_ptr -> title, temp);

                        cout << "add a description: " <<  endl;
                        cin.getline(temp, 1000);
                        cin.ignore(1001, '\n');
                        post_ptr -> description = new char[strlen (temp) + 1];
                        strcpy (post_ptr -> title, temp);
                }
        }

}

bool Posting::is_type( Posting *post_ptr, Posting *type)
{
        char temp[100];

        cout << "What type of  post are you creating? (job, housing, for sale, free stuff): " << endl;
        cin >> temp;

        post_ptr -> type = new char[strlen (temp) + 1];
        strcpy( post_ptr -> type, temp);

        if (strcmp (post_ptr -> type, "job") || strcmp (post_ptr -> type, "housing") || strcmp (post_ptr -> type, "for sale") || strcmp (post_ptr -> type, "free stuff"))
                return true;

   return false;
}


Posting::~Posting()
{
	delete [] description;
	delete [] title;
	delete [] type;
	delete [] location;
	delete [] email;
	cout << "objects are being deleted" << endl;
}


and then my main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "main.h"
using namespace std;


int main()
{
	Posting *title(NULL);
	Posting *type(NULL);
	Posting *description(NULL);
	Posting *email(NULL);
	Posting *post_ptr(NULL);
	Posting post_dat;

	post_dat.Create_Posting( post_ptr, type, title, description, email);
	

        return 0;
}

Mar 12, 2013 at 6:49am
1
2
3
4
5
6
7
8
9
10
11
12
//main:
//...
Posting *post_ptr(NULL);

post_dat.Create_Posting( post_ptr, type, title, description, email);
//...

//post.cpp
void Posting::Create_Posting(Posting *post_ptr, Posting *type, Posting *title, Posting *description, Posting *email)
//...
post_ptr -> title = new char[strlen (temp) + 1];
//... 


You trying to dereference null pointer.
Mar 12, 2013 at 6:50am
You would have to be more specific. What area of our code is the compiler complaining about?
Mar 12, 2013 at 7:11am
In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Posting::Posting()
{	
	 int size(0);
         int cost(0);
         description = new char[size];
         title = new char[size];
         type = new char[size];
         int sq_feet(0);
         int rooms(0);
         int rent(0);
         location = new char[size];
         email = new char[size];
         int pay(0);
	cout <<" objects are being created" << endl;
}


size, cost, sq_feet, rooms, rent and pay are local variables that hide the class data members of the same names.

Aside from that, the local variable size is set to 0. And then you allocate 0 elements for all of your arrays. Using any elements of any of those arrays will result in undefined behavior, as those arrays have no elements.

In both is_type and Create_Posting you leak memory that was previously allocated (great consistency in naming styles there, btw.)

I would very strongly suggest rereading whatever text you're learning from on classes and manual memory management.

Mar 12, 2013 at 6:43pm
OK, you mentioned that my functions is_type and create_posting leak memory. but im using the destructor at the end of the program. do i need to call the destructor at the end of each function?

and with regards to size being set to 0. i was attempting to create dynamically allocated arrays in the class but my compiler gave me an error when i left the brackets blank so i put a variable in it
Mar 12, 2013 at 6:46pm
also, as the program was when posted it ran. through all of the functions but it seg faulted before completion
Mar 12, 2013 at 7:01pm
to handle memory leaks.
Part of your code:
1
2
3
        cin >> temp;

        post_ptr -> type = new char[strlen (temp) + 1];

fix:
1
2
3
        cin >> temp;
        delete[] (post_ptr -> type);
        post_ptr -> type = new char[strlen (temp) + 1];
Make similar changes everywhere you allocate new memory

I have wrote about segfault in my previous post.
Last edited on Mar 12, 2013 at 7:02pm
Mar 12, 2013 at 7:06pm
i dont do that in the constructor though. just the functions right?
Mar 12, 2013 at 7:08pm
and thank you so much for that fix :) i was trying to delete them all wrong
Topic archived. No new replies allowed.