This question already has answers here:
Valgrind reporting a segment overflow
(5 answers)
Closed 6 years ago.
I wonder what this message implies:
==18151== brk segment overflow in thread #1: can't grow to 0x4a26000
Note that the code runs just fine and the output is correct. Should I just ignore this message? And what does it mean?
I think you can ignore it. I got the message in a new allocation in some code that seemed to work perfectly and I also get the message it in the following code:
#include <vector>
struct Something
{
Something() : a1(0), b1(0) { }
unsigned short a1;
unsigned short b1;
};
const int allocsize = 10000;
struct Tester
{
Tester()
{
for (int u = 0; u < allocsize; ++u)
data.push_back(new Something[519]);
}
~Tester()
{
for (int u = 0; u < allocsize; ++u)
delete[] (data[u]);
}
std::vector<Something*> data;
};
void test()
{
Tester t;
// while (true) {;}
}
int main()
{
test();
return 0;
}
I also noticed that others experience the same issue:
Valgrind reporting a segment overflow
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am using g++ 4.8 to write a c++11 program. I'm trying to convert a single threaded program to multi-threaded one. The join in the threaded version ends up throwing a compilation error. Could you please let me know where I'm going wrong?
Single Threaded(works well):
Note that all arguments are pass by reference.
for (const auto& i : vec_clients)
{
i->startSim(vec_masters, vec_trace1, vec_trace2, vec_trace3);
}
Multi-Threaded version:
std::vector<std::thread> vec_thr;
for (const auto& i : vec_clients)
{
std::thread t1(&Client::startSim, std::move(i), std::move(vec_trace1), std::move(vec_trace2), std::move(vec_trace3));
vec_thr.push_back(std::move(t1));
}
for (unsigned int i=0; i<vec_thr.size(); ++i)
{
// if (i.joinable())
vec_thr.at(i).join();
}
Modified(simpler example):
class Test
{
private:
public:
void testme(const std::string& _str)
{
std::cout << "Hello "+_str << std::endl;
}
};
int main(const int argc, const char **argv)
{
std::string str = "there";
Test t1;
std::vector<std::thread> vec_thr;
std::thread thr1(&Test::testme, std::move(t1), std::cref(str));
vec_thr.push_back(thr1);
return 0;
}
Integer i does not have a joinable member function (or any member function as it is a primitive type). It should be:
for (unsigned int i=0; i<vec_thr.size(); ++i)
{
if (vec_thr[i].joinable())
vec_thr.at(i).join();
}
Or just use join on the thread. I see no particular reason to do joinable test here as you are not detaching any of the threads.
In case anyone is still interested, this is the answer:
class Test
{
private:
public:
void testme(const std::string& _str)
{
std::cout << "Hello "+_str << std::endl;
}
void testme2(const std::string& _str)
{
std::cout << "Hello to "+_str << std::endl;
}
};
int main(const int argc, const char **argv)
{
std::string str = "there";
std::unique_ptr<Test> up1(new Test());
std::unique_ptr<Test> up2(new Test());
std::vector<std::thread> vec_thr;
std::thread thr1(&Test::testme, std::move(up1), std::ref(str));
std::thread thr2(&Test::testme2, std::move(up2), std::ref(str));
vec_thr.push_back(std::move(thr1));
vec_thr.push_back(std::move(thr2));
for (unsigned int i=0; i<vec_thr.size(); ++i)
{
vec_thr.at(i).join();
}
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am having a very tough time understanding exception handling after watching online tutorials and reading up on it. I am trying to pass test driven development, and I can't. What I have come up with so far is this. I am supposed to use this struct
struct ArrayException
{
ArrayException(string newMessage = "error") :message(newMessage)
{
}
string message;
};
The first try.
int sum(int* theArray, unsigned int arraySize)
{
try
{
if (theArray = NULL)
{
throw ArrayException("NULL ARRAY REFERENCE");
}
}
catch (int* param)
{
cout << "you can't have " << param << " as an array size";
}
int sum = 0;
for (int i = 1; i < arraySize; i++)
{
sum += theArray[i];
}
return sum;
}
I also tried doing it this way.
int sum(int* theArray, unsigned int arraySize)
{
if (theArray = NULL)
{
throw ArrayException("NULL ARRAY REFERENCE");
}
else
{
int sum = 0;
for (int i = 1; i < arraySize; i++)
{
sum += theArray[i];
}
return sum;
}
}
While the post does not specifically mention it, I take it that the question is why exception is not caught? The answer is simple - because exception thrown is of type ArrayException, and catch is done with the type int*.
The best way to get a grip on this stuff is as πάντα ῥεῖ recommended: get a good book. Here's a good place to start selecting books: The Definitive C++ Book Guide and List
The rest is a code block with comments where I figured they were needed.
#include <iostream>
// removed the using namespace std;
struct ArrayException
{
ArrayException(std::string newMessage = "error") :
message(newMessage)
{
}
std::string message;
};
int sum(int* theArray, size_t arraySize) // change made here:
// size_t better suited than unsigned int for indexes
{
//throw std::out_of_range("BOOM!"); //uncomment to trigger a std::exception
//throw 42; // uncomment to trigger the unknown exception
if (theArray == NULL)
{
throw ArrayException("NULL ARRAY REFERENCE"); //perfect!
}
else
{
int sum = 0;
for (size_t i = 0; i < arraySize; i++) // changes made here:
// size_t not int to get rid of signed/unsigned conflict
// starting with array index 0, not 1
{
sum += theArray[i];
}
return sum;
}
}
int main()
{
try
{
sum (NULL, 10); // NULL address to force the exception
}
catch (ArrayException & param) // catch the custom exception
// catch by reference where possible
{
std::cout << "This bad stuff happened: " << param.message << std::endl;
}
// extra stuff to show what can also be done
catch (std::exception & stdexcpt) // catch any standard exceptions and
// print their message
{
std::cout << "Standard exception thrown: " << stdexcpt.what() << std::endl;
}
catch (...) // catch anything else that was thrown and doesn't
// correspond to any expectation
{
std::cout << "No idea what happened." << std::endl;
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
Suppose I have a C++ program with the following code:
#include <iostream>
void func() {
//code for doing some stuffs
}
int main() {
int a; //line 8
cin>>a;
if(a==5) {
func();
} //line 12
}
In the main() function, after calling the function func() it runs well. But after finishing executing the codes from func() it returns to line 12. But how can I return to line 8 after executing func()? I mean after executing a function, I want the main() function to run from the beginning.
Put everything in your main() in a while(true) loop then your program will loop round to line 8 after finishing func().
Int main() {
while(1){
int a; //line 8
cin>>a;
if(a==5) {
func();
} //line 12
}
}
Here is a basic way, set the amount of times you want the LOOP to run...
#include<iostream>
void func()
{
}
const int LOOP = 10 //for example
int main()
{
for (int i = 0; i < LOOP; i++)
{
int a;
std::cin >> a;
if (a == 5)
func();
}
return 0;
}
This question already has answers here:
Why doesn't my program crash when I write past the end of an array?
(9 answers)
Closed 9 years ago.
class A
{
public:
static void * operator new (size_t,void *p)
{
return p;
}
int i;
};
int main()
{
void *p = malloc(sizeof(A));
cout<<p<<endl;
A *a= new (p) A;
a->i = 10;
cout<<a<<endl;
cout<<a->i<<endl;
a->i = 100;
cout<<a->i<<endl;
}
output:
0x1e0e010
0x1e0e010
10
100
But I change the code of operator new to
static void * operator new (size_t,void *p)
{
return p+1024;
}
it doesn't crash and its output is:
0x25c4010
0x25c4410
10
100
I am using ubuntu13.10 and gcc4.8.1
Thanks
The short answer: Undefined behavior is undefined.
I have seen many posts for this error. But I'm not reserving memory dynamically or doing anything in destructor:
This program is SSJF algorithm for selecting cylinder in operating system.
I have a simple class called IO:
class IO
{
public:
IO();
IO(int,int);
void setIO(int,int);
~IO();
int trackNo;
int arrival;
int start;
int end;
bool finished;
};
Here is the implementation of the class::
IO::IO(int arr, int tNum)
{
this->arrival = arr;
this->trackNo = tNum;
this->start = 0;
this->end = 0;
}
IO::IO()
{
}
IO::~IO()
{
}
void IO::setIO(int t1, int t2)
{
this->trackNo = t1;
this->arrival = t2;
}
And finally here is part of main program:
list<IO> myList;
....
myList.push_back(tmpIO); //Add to the list
...
list<IO> wt_list;
And later I'm trying to do some operations. I have deleted some of the part which is not related.
//list<IO>::iterator itMin;
while(myList.size()>0)
{
//If it is the first input just get it
if(f)
{
IO selected = myList.front();
curr_time += selected.arrival + selected.trackNo;
f=false;
cout << selected.arrival<<endl;
lastPos = selected.trackNo;
myList.pop_front();
}
//Check if there is any item to add to queue
while(myList.front().arrival < curr_time)
{
wt_list.push_back(myList.front());
myList.pop_front(); //Error is coming from this line
}
while(wt_list.size()>0)
{
}
Error message:
malloc: * error for object 0x10f68b3e0: pointer being freed was not allocated
* set a breakpoint in malloc_error_break to debug
Anyone can help me and explain why I get this error and how can I skip it?
The simplest code I can come up with to reproduce this error looks like this:
#include <list>
int main()
{
std::list<int> mylist;
mylist.pop_front();
}
I can prevent the error by doing:
#include <list>
int main()
{
std::list<int> mylist;
if (!mylist.empty())
{
mylist.pop_front();
}
}
You're calling:
myList.pop_front();
...within a while-loop, which in turn is within a while-loop that also calls myList.pop_front().
I can only suggest that you debug your code to see how many times pop_front() is invoked for mylist. My money is on it being more than mylist.size() times, hence my question in the comments (with new emphasis):
How many items are in myList when the error is thrown?
Perhaps the simplest fix will be to replace...
//Check if there is any item to add to queue
while(myList.front().arrival < curr_time)
{
wt_list.push_back(myList.front());
myList.pop_front(); //Error is coming from this line
}
while(wt_list.size()>0)
{
}
...with...
while (!mylist.empty() && myList.front().arrival < curr_time)
{
wt_list.push_back(myList.front());
myList.pop_front();
}
while (!wt_list.empty())
{
}
...but it's hard to tell from the snippet you've provided.