Segfault on releasing resources [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I keep getting a seg fault on the delete trainArray in my code. I'm trying to get all the pointers to the Train objects and delete them. What am I doing wrong here?
pthread_t tidArray[NUM_TRAINS];
Train* trainArray[NUM_TRAINS];
for (int i = 0; i < NUM_TRAINS; i++){
trainArray[i] = new Train(TRAIN_NAME_ARRAY[i],TRAIN_TRANSIT_TIME_MSECS[i]);
}
int trainInd = 0;
for(trainInd = 0;trainInd<NUM_NORTHERNLY_TRAINS;trainInd++){
pthread_create(&tidArray[trainInd],NULL,initiallyNorthernly,(void*)trainArray[trainInd]);
for(trainInd = NUM_SOUTHERNLY_TRAINS;trainInd<NUM_TRAINS;trainInd++){
pthread_create(&tidArray[trainInd],NULL,initiallySouthernly,(void*)trainArray[trainInd]);
}
for (int i = 0; i < NUM_TRAINS; i++){
trainArray[i] = NULL;
pthread_join(tidArray[i],(void**)&trainInd);
delete trainArray[NUM_TRAINS];
}
return(EXIT_SUCCESS);
}

When you wrote this:
delete trainArray[NUM_TRAINS];
I'm pretty sure you meant this:
delete trainArray[i];
…especially seeing as the first one refers to an array element that does not exist.
I suggest you use a nice std::vector instead so that you can't make silly mistakes like this.

Related

Incorrect sum in for loop Qt c++ [closed]

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 5 years ago.
Improve this question
Following c++ code in qt is used by me to add values to an integer(initial value 10) and store it in a QStringList. But when I print the values of the StringList on to 3 labels it prints 10 on all lables though it should be 20,30 and 40 since I increment a by 10 through the for loop!
void MainWindow::on_pushButton_clicked()
{
QStringList array;
int a =10;
for(int i=0;i<10;i++){
a=+10;
array<<QString::number(a);
}
ui->label->setText(array[0]);
ui->label_2->setText(array[1]);
ui->label_2->setText(array[2]);
}
How can I correct this?
You are using
a =+ 10;
// equivalent to
a = +10;
That means you are assigning 10 to a.
You need to use the form below to increment it by 10:
a += 10;

Run-Time Check Failure #3 - The variable 'result' is being used without being initialized [closed]

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 4 years ago.
Improve this question
Having issues with my code... the program compiles, but then it gives the following statement "Run-Time Check Failure #3 - The variable 'result' is being used without being initialized." It then ends the program at that point. Everything I have after trying to run the function is ignored. What should I do?
double result;
for (int i=0; i<nRows; i++)
{
absum.push_back(vector<double>());
for (int j=0; j<nColumns; j++)
{
double temp;
temp = matrixa[i][j]+matrixb[i][j];
absum[i].push_back(temp);
cout << temp << '\t';
}
cout << endl;
}
return result;
At the top of your code you have:
double result;
At the moment it's not initialised to anything at all, so the compiler won't use it. So you need to need to initialise it thus:
double result = 0;
It's also generally good practice to initialise every variable you use in C++, that way you don't get nasty compiler messages, and don't run the risk of returning some random chunk of memory. You always want to start your program from a known state, so if you know that result is 0, then all is good.
C++ is picky about this sometimes, have you tried double result = 0?

Heap corruption detected when use delete [closed]

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 8 years ago.
Improve this question
i run this code and got this exception in visual studio:
int main ()
{
int * x=new int(23);
for(int i=0;i<9;i++)
{
x[i]=i;
}
delete []x;
return(0);
}
thanks for your help ;)
int * x = new int(23); does not do what you think. It allocates the memory for a int and gives it the value of 23.
What you want to do is this:
int * x = new int[23];
Although, I would recommend you use std::vector if the size change or std::array if the size is fix.
If you use new you need to use delete. If you use new[] then you need to use delete []. new() is not the same as new[]. new(someval) sets the new object to someval. It does not make someval number of objects.

Can't return nullpointer in Cpp function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am not able to return the nullptr at the end of this method? Is there some kind of library I need to import?
const char* strstr(const char* string1, const char* string2) {
// TODO:
for (int i = 0; i < strlen(string1); i++) {
for (int j = 0; j < strlen(string2); j++) {
if (string1[i] == string2[i]) {
return &string1[i];
}
}
}
return nullptr;
}
nullptr is a feature introduced in c++11, see: What exactly is nullptr?. You'll need a compiler that supports at least some features of c++11.

C++ std List initialization with a constant value, does it change the performance? [closed]

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 8 years ago.
Improve this question
I am working on a simulator (Computer architecture).
A piece of code read
List freeList;
const int numEntries
registers = new MSHR[numEntries];
for (int i = 0; i < numEntries; ++i) {
registers[i].queue = this;
freeList.push_back(&registers[i]);
}
I changed it to
List freeList;
int numEntries;
registers = new MSHR[numEntries + 100];
for (int i = 0; i < numEntries + 100; ++i) {
registers[i].queue = this;
freeList.push_back(&registers[i]);
}
Just changing the const numEntries has had a drastic effect on the memory usage. If I run the program for long (Code does a lot of push_back, pop_front on the list), I run into this error
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
What can be the difference?
LMGTFY: The documentation says "Exception thrown on failure allocating memory".
You are trying to create more objects than you have memory for.
Since numEntries is not initialized, that is the problem.