Vector declaration giving problem

Why is it that this throws a SIGSEGV signal when debugging?

Here is my .h file showing the declaration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef PAYMENT_H
#define	PAYMENT_H

#include "Item.h"
#include <vector>

class Payment {
public:
    Payment();
    void ProcessItem();

private:
    vector<Item*> items;

};

#endif	/* PAYMENT_H */ 


Here is my driver class

1
2
3
4
5
6
7
8
9
10
11
12
#include "Payment.h"

using namespace std;

int main(int argc, char** argv)
{
    Payment* myPayment;

    myPayment->ProcessItem(/*Item that is irrelevant*/);

    return 0;
}


And here is my Payment.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Payment.h"
#include "Item.h"

Payment::Payment()
{
    this->items.clear();
}

void Payment::ProcessItem()
{
    Item* myItem = new Item("Car", 99999.00);

    this->items.push_back(myItem);
}


On line 13 in Payment.cpp the SIGSEGV signal is thrown and I cannot figure out why this is. I believe it has something to do with my vector declaration but it does not make any sense to me.
To be thorough, this is the content of the signal window that pops up:
===========================================================================
Signal received: SIGSEGV (?) with sigcode ? (?)
From process: ?
For program vector_test, pid -1

You may discard the signal or forward it and you may continue or pause the
process
To control which signals are caught or ignored use Debug->Dbx Configure
===========================================================================
closed account (zb0S216C)
Do not add dynamically allocated objects into a std::vector unless it's required for polymorphic purposes. The std::vector allocates a large block of memory, enough to hold well over 1000 objects of type T. A simple call to std::vector::push_back() would suffice.

Wazzak
@Wazzak

That does not address the problem. Maybe this might make the problem a little clearer. Check this out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Payment.h"
#include "Item.h"

Payment::Payment()
{
    this->items.clear();
}

void Payment::ProcessItem()
{
    Item* myItem = new Item("Car", 99999.00);

    vector<Item*> localItems;
    localItems.push_back(myItem);  // This works fine

    this->items.push_back(myItem);  // While this still has a problem
}
Payment* myPayment; //invalid pointer

The std::vector allocates a large block of memory, enough to hold well over 1000 objects of type T.
¿where?
Last edited on
@ne555

The payment pointer points to payment object that has no attributes.

It is the same as:
Payment* myPayment = new Payment();
Last edited on
TankedTom wrote:
Why is it that this throws a SIGSEGV signal when debugging?

You're calling ProcessItem() on a non-existent Payment.

Edit: grammar.
Last edited on
@ne555 and @Caligulaminus

Wow. This is the first time I have ever came across this kinda problem. I went ahead and added the = new Payment() to the Payment* myPayment and it worked fine! That is strange. Thanks
closed account (zb0S216C)
@TankedTom: You're not using std::vector the way it's supposed to be used, that's why I said what I said. The fact that you're manipulating raw pointers is a segment violation invitation. Since std::vector::clear() will not release the memory you pushed into the vector (lines 14 & 16), you'll create a memory leak. Also

1
2
3
4
Payment::Payment()
{
    this->items.clear();
}

Why are you clearing an empty std::vector?

If you insist on pushing dynamically allocated memory into a std::vector, look into smart, unique, and shared pointers, they'll handle the memory for you.

ne555 wrote:
"where?"

Internally to the std::vector. Everyone should know this.

Wazzak
¿Do you come from java?
If you want to create an object, then create an object
1
2
    Payment myPayment; //creating an object. It will use the default constructor in this case
    myPayment.ProcessItem(/*Item that is irrelevant*/); //sending a message to the object 


If you create the object dynamically (with new) you need to release them (with delete)
@Framework

I used the .clear() in the constructor to try to solve the problem on my own while debugging before I posted this. I just forgot to delete it.
@ne555

Yeah I know about the delete for dynamic memory. I was counting on the Payment* myPayment process utilizing the default constructor.
@Framework: maybe I should have asked "when".
There is a capacity() method that tells you how many objects the vector can hold without reallocating.
The starting capacity is implementation defined, but > 1000 is overkill.
Topic archived. No new replies allowed.