core dump stack indicates SIGSEGV due to vector<vector> usage
up vote
2
down vote
favorite
I have a code snippet that is behaving weirdly. The code is simply aiming to implement radix and bucket sort. When I comment in the main one of either sort and run it works perfectly. But when I enable both of them i am getting a core dump. And the weird part is core dump as indicated by the stack is crossing over into the stl_vector.h.
The code reference is here:- https://rextester.com/RUUDP10453
When i enable only one of the sorts like below in main it works fine.
//doRadixSort(arr, size);
doBucketSort(arr, size);
or
doRadixSort(arr, size);
//doBucketSort(arr, size);
But when both are enabled there is segmentation fault after both sorts are completed as indicated by the
cout << "i am here at exit" << endl;
The core dump stack indicates some reference/hint at vector of vector buckets. But i have properly allocated and reserved it the required memory. so why this is happening i need some expertise to dig out. I have tried debugging this in eclipse CDT C++ for about 2 hrs with no lead.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 _int_free (av=0x7f66d702eb00 <main_arena>, p=0xf98020, have_lock=0) at malloc.c:3976
3976 >= ((char *) av->top + chunksize(av->top)), 0))
(gdb) where
#0 _int_free (av=0x7f66d702eb00 <main_arena>, p=0xf98020, have_lock=0) at malloc.c:3976
#1 0x00007f66d6cf33dc in __GI___libc_free (mem=<optimized out>) at malloc.c:2966
#2 0x00000000004030fa in __gnu_cxx::new_allocator<std::vector<int, std::allocator<int> > >::deallocate (this=0x7fffc6ffa060, __p=0xf98030) at /usr/include/c++/6.3.1/ext/new_allocator.h:110
#3 0x0000000000402d23 in std::allocator_traits<std::allocator<std::vector<int, std::allocator<int> > > >::deallocate (__a=..., __p=0xf98030, __n=10) at /usr/include/c++/6.3.1/bits/alloc_traits.h:442
#4 0x00000000004027ac in std::_Vector_base<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::_M_deallocate (this=0x7fffc6ffa060, __p=0xf98030, __n=10)
at /usr/include/c++/6.3.1/bits/stl_vector.h:178
#5 0x00000000004025e4 in std::_Vector_base<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::~_Vector_base (this=0x7fffc6ffa060, __in_chrg=<optimized out>)
at /usr/include/c++/6.3.1/bits/stl_vector.h:160
#6 0x000000000040211d in std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::~vector (this=0x7fffc6ffa060, __in_chrg=<optimized out>)
at /usr/include/c++/6.3.1/bits/stl_vector.h:427
#7 0x0000000000401d4b in doBucketSort (arr=0x7fffc6ffa100, size=@0x7fffc6ffa0f8: 12) at tako.cpp:97
#8 0x0000000000401e29 in main (argc=1, argv=0x7fffc6ffa218) at tako.cpp:141
(gdb)
c++11 coredump bucket-sort
|
show 1 more comment
up vote
2
down vote
favorite
I have a code snippet that is behaving weirdly. The code is simply aiming to implement radix and bucket sort. When I comment in the main one of either sort and run it works perfectly. But when I enable both of them i am getting a core dump. And the weird part is core dump as indicated by the stack is crossing over into the stl_vector.h.
The code reference is here:- https://rextester.com/RUUDP10453
When i enable only one of the sorts like below in main it works fine.
//doRadixSort(arr, size);
doBucketSort(arr, size);
or
doRadixSort(arr, size);
//doBucketSort(arr, size);
But when both are enabled there is segmentation fault after both sorts are completed as indicated by the
cout << "i am here at exit" << endl;
The core dump stack indicates some reference/hint at vector of vector buckets. But i have properly allocated and reserved it the required memory. so why this is happening i need some expertise to dig out. I have tried debugging this in eclipse CDT C++ for about 2 hrs with no lead.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 _int_free (av=0x7f66d702eb00 <main_arena>, p=0xf98020, have_lock=0) at malloc.c:3976
3976 >= ((char *) av->top + chunksize(av->top)), 0))
(gdb) where
#0 _int_free (av=0x7f66d702eb00 <main_arena>, p=0xf98020, have_lock=0) at malloc.c:3976
#1 0x00007f66d6cf33dc in __GI___libc_free (mem=<optimized out>) at malloc.c:2966
#2 0x00000000004030fa in __gnu_cxx::new_allocator<std::vector<int, std::allocator<int> > >::deallocate (this=0x7fffc6ffa060, __p=0xf98030) at /usr/include/c++/6.3.1/ext/new_allocator.h:110
#3 0x0000000000402d23 in std::allocator_traits<std::allocator<std::vector<int, std::allocator<int> > > >::deallocate (__a=..., __p=0xf98030, __n=10) at /usr/include/c++/6.3.1/bits/alloc_traits.h:442
#4 0x00000000004027ac in std::_Vector_base<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::_M_deallocate (this=0x7fffc6ffa060, __p=0xf98030, __n=10)
at /usr/include/c++/6.3.1/bits/stl_vector.h:178
#5 0x00000000004025e4 in std::_Vector_base<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::~_Vector_base (this=0x7fffc6ffa060, __in_chrg=<optimized out>)
at /usr/include/c++/6.3.1/bits/stl_vector.h:160
#6 0x000000000040211d in std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::~vector (this=0x7fffc6ffa060, __in_chrg=<optimized out>)
at /usr/include/c++/6.3.1/bits/stl_vector.h:427
#7 0x0000000000401d4b in doBucketSort (arr=0x7fffc6ffa100, size=@0x7fffc6ffa0f8: 12) at tako.cpp:97
#8 0x0000000000401e29 in main (argc=1, argv=0x7fffc6ffa218) at tako.cpp:141
(gdb)
c++11 coredump bucket-sort
1
In both functions you did the same mistake -reserve
was used instead ofresize
.reserve
changes the capacity of vector, size is untouched. So before and after callingreserve
size ofbuckets
vector is 0, then you calloperator
for empty vector it leads to UB.
– rafix07
Nov 10 at 14:59
I got your point , it works now!!! It was all due to only memory being reserved and actually proper vector interface not being used to initialize the vectors inside the vector. Now i have below code. // Create the 10 buckets as indicated below. vector<vector<int>> buckets; vector<int> emptyVec = { }; buckets.reserve(10); for(unsigned int i=0; i<=9; ++i) { buckets[i].reserve(size); buckets.push_back(emptyVec); }
– Deepti Kulkarni
Nov 11 at 0:17
can you upvote the question so that it can help me build my StackOverflow points? I could not see this question hence asked.
– Deepti Kulkarni
Nov 11 at 0:22
I am very sorry rafix07, I jumped in excitement bit too early. Could you please take a look at my code modifications at rextester.com/RUUDP10453 , After a few more runs its again back to dumping the core. I am still unclear on which piece i am missing in the cleanup. I do vector clear which removes all items and then shrink to fit which releases the memory. still it is dumping the core and failing to run. Your help is appreciated.
– Deepti Kulkarni
Nov 11 at 9:22
1
Callbuckets.resize(10);
instead ofbuckets.reserve(10);
. So should bevector<vector<int>> buckets;
,buckets.resize(10);
,for(unsigned int i=0; i<=9; ++i) buckets[i].reserve(size);
in both functions.
– rafix07
Nov 11 at 9:26
|
show 1 more comment
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a code snippet that is behaving weirdly. The code is simply aiming to implement radix and bucket sort. When I comment in the main one of either sort and run it works perfectly. But when I enable both of them i am getting a core dump. And the weird part is core dump as indicated by the stack is crossing over into the stl_vector.h.
The code reference is here:- https://rextester.com/RUUDP10453
When i enable only one of the sorts like below in main it works fine.
//doRadixSort(arr, size);
doBucketSort(arr, size);
or
doRadixSort(arr, size);
//doBucketSort(arr, size);
But when both are enabled there is segmentation fault after both sorts are completed as indicated by the
cout << "i am here at exit" << endl;
The core dump stack indicates some reference/hint at vector of vector buckets. But i have properly allocated and reserved it the required memory. so why this is happening i need some expertise to dig out. I have tried debugging this in eclipse CDT C++ for about 2 hrs with no lead.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 _int_free (av=0x7f66d702eb00 <main_arena>, p=0xf98020, have_lock=0) at malloc.c:3976
3976 >= ((char *) av->top + chunksize(av->top)), 0))
(gdb) where
#0 _int_free (av=0x7f66d702eb00 <main_arena>, p=0xf98020, have_lock=0) at malloc.c:3976
#1 0x00007f66d6cf33dc in __GI___libc_free (mem=<optimized out>) at malloc.c:2966
#2 0x00000000004030fa in __gnu_cxx::new_allocator<std::vector<int, std::allocator<int> > >::deallocate (this=0x7fffc6ffa060, __p=0xf98030) at /usr/include/c++/6.3.1/ext/new_allocator.h:110
#3 0x0000000000402d23 in std::allocator_traits<std::allocator<std::vector<int, std::allocator<int> > > >::deallocate (__a=..., __p=0xf98030, __n=10) at /usr/include/c++/6.3.1/bits/alloc_traits.h:442
#4 0x00000000004027ac in std::_Vector_base<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::_M_deallocate (this=0x7fffc6ffa060, __p=0xf98030, __n=10)
at /usr/include/c++/6.3.1/bits/stl_vector.h:178
#5 0x00000000004025e4 in std::_Vector_base<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::~_Vector_base (this=0x7fffc6ffa060, __in_chrg=<optimized out>)
at /usr/include/c++/6.3.1/bits/stl_vector.h:160
#6 0x000000000040211d in std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::~vector (this=0x7fffc6ffa060, __in_chrg=<optimized out>)
at /usr/include/c++/6.3.1/bits/stl_vector.h:427
#7 0x0000000000401d4b in doBucketSort (arr=0x7fffc6ffa100, size=@0x7fffc6ffa0f8: 12) at tako.cpp:97
#8 0x0000000000401e29 in main (argc=1, argv=0x7fffc6ffa218) at tako.cpp:141
(gdb)
c++11 coredump bucket-sort
I have a code snippet that is behaving weirdly. The code is simply aiming to implement radix and bucket sort. When I comment in the main one of either sort and run it works perfectly. But when I enable both of them i am getting a core dump. And the weird part is core dump as indicated by the stack is crossing over into the stl_vector.h.
The code reference is here:- https://rextester.com/RUUDP10453
When i enable only one of the sorts like below in main it works fine.
//doRadixSort(arr, size);
doBucketSort(arr, size);
or
doRadixSort(arr, size);
//doBucketSort(arr, size);
But when both are enabled there is segmentation fault after both sorts are completed as indicated by the
cout << "i am here at exit" << endl;
The core dump stack indicates some reference/hint at vector of vector buckets. But i have properly allocated and reserved it the required memory. so why this is happening i need some expertise to dig out. I have tried debugging this in eclipse CDT C++ for about 2 hrs with no lead.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 _int_free (av=0x7f66d702eb00 <main_arena>, p=0xf98020, have_lock=0) at malloc.c:3976
3976 >= ((char *) av->top + chunksize(av->top)), 0))
(gdb) where
#0 _int_free (av=0x7f66d702eb00 <main_arena>, p=0xf98020, have_lock=0) at malloc.c:3976
#1 0x00007f66d6cf33dc in __GI___libc_free (mem=<optimized out>) at malloc.c:2966
#2 0x00000000004030fa in __gnu_cxx::new_allocator<std::vector<int, std::allocator<int> > >::deallocate (this=0x7fffc6ffa060, __p=0xf98030) at /usr/include/c++/6.3.1/ext/new_allocator.h:110
#3 0x0000000000402d23 in std::allocator_traits<std::allocator<std::vector<int, std::allocator<int> > > >::deallocate (__a=..., __p=0xf98030, __n=10) at /usr/include/c++/6.3.1/bits/alloc_traits.h:442
#4 0x00000000004027ac in std::_Vector_base<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::_M_deallocate (this=0x7fffc6ffa060, __p=0xf98030, __n=10)
at /usr/include/c++/6.3.1/bits/stl_vector.h:178
#5 0x00000000004025e4 in std::_Vector_base<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::~_Vector_base (this=0x7fffc6ffa060, __in_chrg=<optimized out>)
at /usr/include/c++/6.3.1/bits/stl_vector.h:160
#6 0x000000000040211d in std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::~vector (this=0x7fffc6ffa060, __in_chrg=<optimized out>)
at /usr/include/c++/6.3.1/bits/stl_vector.h:427
#7 0x0000000000401d4b in doBucketSort (arr=0x7fffc6ffa100, size=@0x7fffc6ffa0f8: 12) at tako.cpp:97
#8 0x0000000000401e29 in main (argc=1, argv=0x7fffc6ffa218) at tako.cpp:141
(gdb)
c++11 coredump bucket-sort
c++11 coredump bucket-sort
asked Nov 10 at 13:16
Deepti Kulkarni
364
364
1
In both functions you did the same mistake -reserve
was used instead ofresize
.reserve
changes the capacity of vector, size is untouched. So before and after callingreserve
size ofbuckets
vector is 0, then you calloperator
for empty vector it leads to UB.
– rafix07
Nov 10 at 14:59
I got your point , it works now!!! It was all due to only memory being reserved and actually proper vector interface not being used to initialize the vectors inside the vector. Now i have below code. // Create the 10 buckets as indicated below. vector<vector<int>> buckets; vector<int> emptyVec = { }; buckets.reserve(10); for(unsigned int i=0; i<=9; ++i) { buckets[i].reserve(size); buckets.push_back(emptyVec); }
– Deepti Kulkarni
Nov 11 at 0:17
can you upvote the question so that it can help me build my StackOverflow points? I could not see this question hence asked.
– Deepti Kulkarni
Nov 11 at 0:22
I am very sorry rafix07, I jumped in excitement bit too early. Could you please take a look at my code modifications at rextester.com/RUUDP10453 , After a few more runs its again back to dumping the core. I am still unclear on which piece i am missing in the cleanup. I do vector clear which removes all items and then shrink to fit which releases the memory. still it is dumping the core and failing to run. Your help is appreciated.
– Deepti Kulkarni
Nov 11 at 9:22
1
Callbuckets.resize(10);
instead ofbuckets.reserve(10);
. So should bevector<vector<int>> buckets;
,buckets.resize(10);
,for(unsigned int i=0; i<=9; ++i) buckets[i].reserve(size);
in both functions.
– rafix07
Nov 11 at 9:26
|
show 1 more comment
1
In both functions you did the same mistake -reserve
was used instead ofresize
.reserve
changes the capacity of vector, size is untouched. So before and after callingreserve
size ofbuckets
vector is 0, then you calloperator
for empty vector it leads to UB.
– rafix07
Nov 10 at 14:59
I got your point , it works now!!! It was all due to only memory being reserved and actually proper vector interface not being used to initialize the vectors inside the vector. Now i have below code. // Create the 10 buckets as indicated below. vector<vector<int>> buckets; vector<int> emptyVec = { }; buckets.reserve(10); for(unsigned int i=0; i<=9; ++i) { buckets[i].reserve(size); buckets.push_back(emptyVec); }
– Deepti Kulkarni
Nov 11 at 0:17
can you upvote the question so that it can help me build my StackOverflow points? I could not see this question hence asked.
– Deepti Kulkarni
Nov 11 at 0:22
I am very sorry rafix07, I jumped in excitement bit too early. Could you please take a look at my code modifications at rextester.com/RUUDP10453 , After a few more runs its again back to dumping the core. I am still unclear on which piece i am missing in the cleanup. I do vector clear which removes all items and then shrink to fit which releases the memory. still it is dumping the core and failing to run. Your help is appreciated.
– Deepti Kulkarni
Nov 11 at 9:22
1
Callbuckets.resize(10);
instead ofbuckets.reserve(10);
. So should bevector<vector<int>> buckets;
,buckets.resize(10);
,for(unsigned int i=0; i<=9; ++i) buckets[i].reserve(size);
in both functions.
– rafix07
Nov 11 at 9:26
1
1
In both functions you did the same mistake -
reserve
was used instead of resize
. reserve
changes the capacity of vector, size is untouched. So before and after calling reserve
size of buckets
vector is 0, then you call operator
for empty vector it leads to UB.– rafix07
Nov 10 at 14:59
In both functions you did the same mistake -
reserve
was used instead of resize
. reserve
changes the capacity of vector, size is untouched. So before and after calling reserve
size of buckets
vector is 0, then you call operator
for empty vector it leads to UB.– rafix07
Nov 10 at 14:59
I got your point , it works now!!! It was all due to only memory being reserved and actually proper vector interface not being used to initialize the vectors inside the vector. Now i have below code. // Create the 10 buckets as indicated below. vector<vector<int>> buckets; vector<int> emptyVec = { }; buckets.reserve(10); for(unsigned int i=0; i<=9; ++i) { buckets[i].reserve(size); buckets.push_back(emptyVec); }
– Deepti Kulkarni
Nov 11 at 0:17
I got your point , it works now!!! It was all due to only memory being reserved and actually proper vector interface not being used to initialize the vectors inside the vector. Now i have below code. // Create the 10 buckets as indicated below. vector<vector<int>> buckets; vector<int> emptyVec = { }; buckets.reserve(10); for(unsigned int i=0; i<=9; ++i) { buckets[i].reserve(size); buckets.push_back(emptyVec); }
– Deepti Kulkarni
Nov 11 at 0:17
can you upvote the question so that it can help me build my StackOverflow points? I could not see this question hence asked.
– Deepti Kulkarni
Nov 11 at 0:22
can you upvote the question so that it can help me build my StackOverflow points? I could not see this question hence asked.
– Deepti Kulkarni
Nov 11 at 0:22
I am very sorry rafix07, I jumped in excitement bit too early. Could you please take a look at my code modifications at rextester.com/RUUDP10453 , After a few more runs its again back to dumping the core. I am still unclear on which piece i am missing in the cleanup. I do vector clear which removes all items and then shrink to fit which releases the memory. still it is dumping the core and failing to run. Your help is appreciated.
– Deepti Kulkarni
Nov 11 at 9:22
I am very sorry rafix07, I jumped in excitement bit too early. Could you please take a look at my code modifications at rextester.com/RUUDP10453 , After a few more runs its again back to dumping the core. I am still unclear on which piece i am missing in the cleanup. I do vector clear which removes all items and then shrink to fit which releases the memory. still it is dumping the core and failing to run. Your help is appreciated.
– Deepti Kulkarni
Nov 11 at 9:22
1
1
Call
buckets.resize(10);
instead of buckets.reserve(10);
. So should be vector<vector<int>> buckets;
, buckets.resize(10);
, for(unsigned int i=0; i<=9; ++i) buckets[i].reserve(size);
in both functions.– rafix07
Nov 11 at 9:26
Call
buckets.resize(10);
instead of buckets.reserve(10);
. So should be vector<vector<int>> buckets;
, buckets.resize(10);
, for(unsigned int i=0; i<=9; ++i) buckets[i].reserve(size);
in both functions.– rafix07
Nov 11 at 9:26
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
Alternatively, I found the below also works which is equivalent to the resize function.
vector<vector<int>> buckets;
constexpr size_t size=10, bucketSize=10;
buckets.reserve(bucketSize);
for(unsigned int i=0; i<=bucketSize; ++i)
buckets.push_back({ });
for(unsigned int i=0; i<=bucketSize; ++i)
buckets[i].reserve(size);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Alternatively, I found the below also works which is equivalent to the resize function.
vector<vector<int>> buckets;
constexpr size_t size=10, bucketSize=10;
buckets.reserve(bucketSize);
for(unsigned int i=0; i<=bucketSize; ++i)
buckets.push_back({ });
for(unsigned int i=0; i<=bucketSize; ++i)
buckets[i].reserve(size);
add a comment |
up vote
0
down vote
Alternatively, I found the below also works which is equivalent to the resize function.
vector<vector<int>> buckets;
constexpr size_t size=10, bucketSize=10;
buckets.reserve(bucketSize);
for(unsigned int i=0; i<=bucketSize; ++i)
buckets.push_back({ });
for(unsigned int i=0; i<=bucketSize; ++i)
buckets[i].reserve(size);
add a comment |
up vote
0
down vote
up vote
0
down vote
Alternatively, I found the below also works which is equivalent to the resize function.
vector<vector<int>> buckets;
constexpr size_t size=10, bucketSize=10;
buckets.reserve(bucketSize);
for(unsigned int i=0; i<=bucketSize; ++i)
buckets.push_back({ });
for(unsigned int i=0; i<=bucketSize; ++i)
buckets[i].reserve(size);
Alternatively, I found the below also works which is equivalent to the resize function.
vector<vector<int>> buckets;
constexpr size_t size=10, bucketSize=10;
buckets.reserve(bucketSize);
for(unsigned int i=0; i<=bucketSize; ++i)
buckets.push_back({ });
for(unsigned int i=0; i<=bucketSize; ++i)
buckets[i].reserve(size);
answered Nov 14 at 12:05
Deepti Kulkarni
364
364
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239325%2fcore-dump-stack-indicates-sigsegv-due-to-vectorvectorint-usage%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
In both functions you did the same mistake -
reserve
was used instead ofresize
.reserve
changes the capacity of vector, size is untouched. So before and after callingreserve
size ofbuckets
vector is 0, then you calloperator
for empty vector it leads to UB.– rafix07
Nov 10 at 14:59
I got your point , it works now!!! It was all due to only memory being reserved and actually proper vector interface not being used to initialize the vectors inside the vector. Now i have below code. // Create the 10 buckets as indicated below. vector<vector<int>> buckets; vector<int> emptyVec = { }; buckets.reserve(10); for(unsigned int i=0; i<=9; ++i) { buckets[i].reserve(size); buckets.push_back(emptyVec); }
– Deepti Kulkarni
Nov 11 at 0:17
can you upvote the question so that it can help me build my StackOverflow points? I could not see this question hence asked.
– Deepti Kulkarni
Nov 11 at 0:22
I am very sorry rafix07, I jumped in excitement bit too early. Could you please take a look at my code modifications at rextester.com/RUUDP10453 , After a few more runs its again back to dumping the core. I am still unclear on which piece i am missing in the cleanup. I do vector clear which removes all items and then shrink to fit which releases the memory. still it is dumping the core and failing to run. Your help is appreciated.
– Deepti Kulkarni
Nov 11 at 9:22
1
Call
buckets.resize(10);
instead ofbuckets.reserve(10);
. So should bevector<vector<int>> buckets;
,buckets.resize(10);
,for(unsigned int i=0; i<=9; ++i) buckets[i].reserve(size);
in both functions.– rafix07
Nov 11 at 9:26