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.
Related
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 2 years ago.
Improve this question
I am tring to copy a const char* to a char* and below is what I have written:
int main()
{
const char* string = "Hello";
int size = strlen(string) + 1; // add '/0' in the end
char* data = new char(size);
memcpy(data, string, size);
delete data;
}
unfortunately I am getting a error with "Heap corruption detected: after normal block (#77)".
I have no idea what the problem is.
I am complying my code using MSVC under visual studio 2019.
new char(size) is not allocating an array of characters but allocating single character whose value is size.
You should use new char[size] to allocate an array and delete[] data; to delete the array.
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 3 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int* i = new int(75);
double* d = new double(3.14159);
printf("%d\n",*i);
printf("%d\n",*d);
}
In the above code i returns a value of 75 however, d returns 1.
I tried explicitly initializing it as
*d = 3.14159
But the value is still returned as 1.
Can anyone explain what I am doing wrong here?
Use this for printing.
cout<<*i;
cout<<*d
"%f" is the (or at least one) correct format for a double if you want to use printf for printing the value of the double in C++.
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
void Remove(int x)//x is the number that i want to remove
{
for(int i=0;i<CAPACITY;i++)//loop is to find the first case of x
{
if(x==data[i])//if x is in data
{
cout<<data[i]<<endl;//for debugging
data[i]==0; //change x to 0
cout<<data[i]<<endl;
}
}
}
when i cout to see if it works the number that i wanted to delete is still there.
Here is the output before i run it when x=15:
12,15,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
I used cout to see if there was a problem with the condition however it runs if x is in the array.
Here is the output after, even if x is in the array:
12,15,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
In your If loop you use comparison operator '==' so put only one = which means you assign variable x to data array.
The problem in in the line data[i]==0; //change x to 0
== is an comparison operator. In order to assign a value use = instead. So:
data[i] = 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 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;
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.