terminate called after throwing an instance of 'std::bad_alloc'

Hi all,

I searched the forum and the web about the mentioned error and all the answers are related to memory size and I think it is not true in my case. I'm working on a simulator in my research which create a memory system with one rank object and 8 banks & bankStates vector in the rank constructor which fires the below error due to adding 2 bankstates (nextRead_H &nextWrite_H) in the header and BankState Class as below

Rank *r = new Rank(dramsim_log);

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
//Rank Contructor
Rank::Rank(ostream &dramsim_log_) :
	id(-1),
	dramsim_log(dramsim_log_),
	isPowerDown(false),
	refreshWaiting(false),
	readReturnCountdown(0),
	readReturnCountdown_H(0),
	banks(NUM_BANKS, Bank(dramsim_log_)),
	bankStates(NUM_BANKS, BankState(dramsim_log_)) // Fires the error in      this statement

{
	DEBUG("== Enter the RANK object == ");
	DEBUG("== id '"<<id<<"'== ");
	memoryController = NULL;
	outgoingDataPacket = NULL;outgoingDataPacket_H = NULL;
	dataCyclesLeft = 0;dataCyclesLeft_H = 0;
	currentClockCycle = 0;

#ifndef NO_STORAGE
#endif

	DEBUG("== before Exit the RANK object == ");

}


terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//BankState.h
class BankState
{
	ostream &dramsim_log; 
public:
	//Fields
	CurrentBankState currentBankState;
	unsigned openRowAddress;
	uint64_t nextRead;
	uint64_t nextWrite;
        uint64_t nextRead_H; //new state
	uint64_t nextWrite_H; //new state
	uint64_t nextActivate;
	uint64_t nextPrecharge;
	uint64_t nextPowerUp;

	BusPacketType lastCommand;
	unsigned stateChangeCountdown;

	//Functions
	BankState(ostream &dramsim_log_);
	void print();
};
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
BankState.cpp
using namespace std;
using namespace DRAMSim;

//All banks start precharged
BankState::BankState(ostream &dramsim_log_):
		dramsim_log(dramsim_log_),
		currentBankState(Idle),
		openRowAddress(0),
		nextRead(0),
                nextRead_H(0), // new state
		nextWrite(0),
                nextWrite_H(0),// new state
		nextActivate(0),
		nextPrecharge(0),
		nextPowerUp(0),
		lastCommand(READ),
		stateChangeCountdown(0)
{}

And NUM_BANKS == 8?
yes
You can probably find the cause by using a debugger like gdb or valgrind.
I used gdb and get below (_n=18446744073709547290) after statement
return size_type(this->_M_impl._M_finish - this->_M_impl._M_start)
although in normal state the (__n=2048). Do you know why ?
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
(gdb)
__gnu_cxx::new_allocator<DRAMSim::Bank>::new_allocator (this=0x653598) at /usr/include/c++/4.7/ext/new_allocator.h:71
71            new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { }
(gdb)
std::_Vector_base<DRAMSim::Bank, std::allocator<DRAMSim::Bank> >::_Vector_impl::_Vector_impl (this=0x653598, __a=...) at /usr/include/c++/4.7/bits/stl_vector.h:93
93              { }
(gdb)
std::_Vector_base<DRAMSim::Bank, std::allocator<DRAMSim::Bank> >::_Vector_base (this=0x653598, __n=8, __a=...) at /usr/include/c++/4.7/bits/stl_vector.h:137
137           { _M_create_storage(__n); }
(gdb)
std::_Vector_base<DRAMSim::Bank, std::allocator<DRAMSim::Bank> >::_M_create_storage (this=0x653598, __n=8) at /usr/include/c++/4.7/bits/stl_vector.h:182
182             this->_M_impl._M_start = this->_M_allocate(__n);
(gdb)
std::_Vector_base<DRAMSim::Bank, std::allocator<DRAMSim::Bank> >::_M_allocate (this=0x653598, __n=8) at /usr/include/c++/4.7/bits/stl_vector.h:169
169           { return __n != 0 ? _M_impl.allocate(__n) : 0; } //here _n !=0 so go to allocate 
(gdb)
__gnu_cxx::new_allocator<DRAMSim::Bank>::allocate (this=0x653598, __n=8) at /usr/include/c++/4.7/ext/new_allocator.h:91
91              if (__n > this->max_size()) //How to get the max_size () ??
(gdb)
__gnu_cxx::new_allocator<DRAMSim::Bank>::max_size (this=0x653598) at /usr/include/c++/4.7/ext/new_allocator.h:104
104           { return size_t(-1) / sizeof(_Tp); }
(gdb)
__gnu_cxx::new_allocator<DRAMSim::Bank>::allocate (this=0x653598, __n=8) at /usr/include/c++/4.7/ext/new_allocator.h:94
94              return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); //from here it detrmines that the if statement return no and create new object n* size of (_Tp)
(gdb)
95            }
(gdb)
std::_Vector_base<DRAMSim::Bank, std::allocator<DRAMSim::Bank> >::_M_create_storage (this=0x653598, __n=8) at /usr/include/c++/4.7/bits/stl_vector.h:183
183             this->_M_impl._M_finish = this->_M_impl._M_start;
(gdb)
184             this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
(gdb)
185           }
(gdb)
std::vector<DRAMSim::Bank, std::allocator<DRAMSim::Bank> >::vector (this=0x653598, __n=8, __value=..., __a=...) at /usr/include/c++/4.7/bits/stl_vector.h:295
295           { _M_fill_initialize(__n, __value); }
(gdb)
std::vector<DRAMSim::Bank, std::allocator<DRAMSim::Bank> >::_M_fill_initialize (this=0x653598, __n=8, __value=...) at /usr/include/c++/4.7/bits/stl_vector.h:1181
1181            std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,_M_get_Tp_allocator());
(gdb)
std::_Vector_base<DRAMSim::Bank, std::allocator<DRAMSim::Bank> >::_M_get_Tp_allocator (this=0x653598) at /usr/include/c++/4.7/bits/stl_vector.h:115
115           { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
(gdb)
std::__uninitialized_fill_n_a<DRAMSim::Bank*, unsigned long, DRAMSim::Bank, DRAMSim::Bank> (__first=0x6535d0, __n=8, __x=...)
    at /usr/include/c++/4.7/bits/stl_uninitialized.h:336
336         { std::uninitialized_fill_n(__first, __n, __x); }
(gdb)
std::uninitialized_fill_n<DRAMSim::Bank*, unsigned long, DRAMSim::Bank> (__first=0x6535d0, __n=8, __x=...) at /usr/include/c++/4.7/bits/stl_uninitialized.h:225
225           std::__uninitialized_fill_n<__is_trivial(_ValueType)>::
(gdb)

std::__uninitialized_fill_n<false>::__uninit_fill_n<DRAMSim::Bank*, unsigned long, DRAMSim::Bank> (__first=0x6535d0, __n=8, __x=...)
    at /usr/include/c++/4.7/bits/stl_uninitialized.h:185
185               _ForwardIterator __cur = __first;
(gdb)
188                   for (; __n > 0; --__n, ++__cur)
(gdb)
189                     std::_Construct(std::__addressof(*__cur), __x);

(gdb)
std::__addressof<DRAMSim::Bank> (__r=...) at /usr/include/c++/4.7/bits/move.h:50

50              (&const_cast<char&>(reinterpret_cast<const volatile char&>(__r)));
(gdb)
51          }
(gdb)
std::_Construct<DRAMSim::Bank, DRAMSim::Bank> (__p=0x6535d0, __value=...) at /usr/include/c++/4.7/bits/stl_construct.h:85
85            ::new(static_cast<void*>(__p)) _T1(__value);
(gdb)
operator new (__p=0x6535d0) at /usr/include/c++/4.7/new:112
112     { return __p; }
(gdb)
DRAMSim::Bank::Bank (this=0x6535d0) at Bank.h:54
54      class Bank
(gdb)
std::vector<DRAMSim::Bank::_DataStruct*, std::allocator<DRAMSim::Bank::_DataStruct*> >::vector (this=0x653620, __x=...) at /usr/include/c++/4.7/bits/stl_vector.h:309
309             _Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()))
(gdb)
std::_Vector_base<DRAMSim::Bank::_DataStruct*, std::allocator<DRAMSim::Bank::_DataStruct*> >::_M_get_Tp_allocator (this=0x7fffffffdf50)
    at /usr/include/c++/4.7/bits/stl_vector.h:119
119           { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
(gdb)
__gnu_cxx::__alloc_traits<std::allocator<DRAMSim::Bank::_DataStruct*> >::_S_select_on_copy (__a=...) at /usr/include/c++/4.7/ext/alloc_traits.h:211
211         static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; }
(gdb)
std::vector<DRAMSim::Bank::_DataStruct*, std::allocator<DRAMSim::Bank::_DataStruct*> >::size (this=0x7fffffffdf50) at /usr/include/c++/4.7/bits/stl_vector.h:626
626           { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
(gdb)
std::_Vector_base<DRAMSim::Bank::_DataStruct*, std::allocator<DRAMSim::Bank::_DataStruct*> >::_Vector_base (this=0x653620, __n=18446744073709547290, __a=...)
    at /usr/include/c++/4.7/bits/stl_vector.h:136
136           : _M_impl(__a)
(gdb)
std::_Vector_base<DRAMSim::Bank::_DataStruct*, std::allocator<DRAMSim::Bank::_DataStruct*> >::_Vector_impl::_Vector_impl (this=0x653620, __a=...)
    at /usr/include/c++/4.7/bits/stl_vector.h:92
92              : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
(gdb)
std::allocator<DRAMSim::Bank::_DataStruct*>::allocator (this=0x653620, __a=...) at /usr/include/c++/4.7/bits/allocator.h:107
107           : __glibcxx_base_allocator<_Tp>(__a) { }
(gdb)
__gnu_cxx::new_allocator<DRAMSim::Bank::_DataStruct*>::new_allocator (this=0x653620) at /usr/include/c++/4.7/ext/new_allocator.h:71
71            new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { }
(gdb)
std::_Vector_base<DRAMSim::Bank::_DataStruct*, std::allocator<DRAMSim::Bank::_DataStruct*> >::_Vector_impl::_Vector_impl (this=0x653620, __a=...)
    at /usr/include/c++/4.7/bits/stl_vector.h:93
93              { }
(gdb)
std::_Vector_base<DRAMSim::Bank::_DataStruct*, std::allocator<DRAMSim::Bank::_DataStruct*> >::_Vector_base (this=0x653620, __n=18446744073709547290, __a=...)
    at /usr/include/c++/4.7/bits/stl_vector.h:137
137           { _M_create_storage(__n); }
(gdb)
std::_Vector_base<DRAMSim::Bank::_DataStruct*, std::allocator<DRAMSim::Bank::_DataStruct*> >::_M_create_storage (this=0x653620, __n=18446744073709547290)
    at /usr/include/c++/4.7/bits/stl_vector.h:182
182             this->_M_impl._M_start = this->_M_allocate(__n);
(gdb)
std::_Vector_base<DRAMSim::Bank::_DataStruct*, std::allocator<DRAMSim::Bank::_DataStruct*> >::_M_allocate (this=0x653620, __n=18446744073709547290)
    at /usr/include/c++/4.7/bits/stl_vector.h:169
169           { return __n != 0 ? _M_impl.allocate(__n) : 0; } wont return zero ofcourse 
(gdb)
__gnu_cxx::new_allocator<DRAMSim::Bank::_DataStruct*>::allocate (this=0x653620, __n=18446744073709547290) at /usr/include/c++/4.7/ext/new_allocator.h:91
91              if (__n > this->max_size()) // greater ya ???? __n=18446744073709547290
(gdb)
__gnu_cxx::new_allocator<DRAMSim::Bank::_DataStruct*>::max_size (this=0x653620) at /usr/include/c++/4.7/ext/new_allocator.h:104
104           { return size_t(-1) / sizeof(_Tp); }
(gdb)
__gnu_cxx::new_allocator<DRAMSim::Bank::_DataStruct*>::allocate (this=0x653620, __n=18446744073709547290) at /usr/include/c++/4.7/ext/new_allocator.h:92
92                std::__throw_bad_alloc();c
Last edited on
Topic archived. No new replies allowed.