Everything is working until the compiler tries to perform the push_back operation.
in the if condition proper values are being returned.
I have declared items as:
vector<int> items; // inside the header file.
//inside the .cpp file
void MsPs::findnSort()
{
for(int i = 1; i<50 ; i++)
{
string temp = static_cast<ostringstream*>( &(ostringstream() << i) )->str(); // TO convert int i to a string temp
if(findSupport(temp) >= MIS[i])
{
items.push_back(i);
}
}
}
the following error pops up:
Unhandled exception at 0x5052ad4a (msvcp100d.dll) in PrefixScan.exe: 0xC0000005: Access violation reading location 0x3d4cccd1.
PS: I have one more function using the push_back operation and there it's working fine.
Can anyone help me with this?
Even this gives the same error:
void MsPs::findnSort()
{
for(int i = 1; i<50 ; i++)
{
items.push_back(i);
}
}
I think the issue is that the ostringstream is destructed when the static cast returns. Thus your pointer is dangling when str() is called. Try this instead:
void MsPs::findnSort()
{
for(int i = 1; i<50 ; i++)
{
ostringstream blah;
string temp = (blah << i).str();
if(findSupport(temp) >= MIS[i])
{
items.push_back(i);
}
}
}
Related
I know this question's is a bit weird but I'm super new to c++ so i have no idea what should I even ask for but,
I am trying to implement a binary tree and i have this function:
std::string* Tree::getChildren(int node) {
std::string children[2];
for (int i = 0; i < 2; i++) {
children[i] = tree[2 * node + i];
}
return children;
}
which I am trying to output like this:
std::string* k = t.getChildren(1);
cout << k[0]<<","<<k[1] << endl;
but this trows the error:
Exception thrown at 0x6A46F3BE (ucrtbased.dll) in Project1.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC. occurred
What does this mean and what should I do to not have it?
This creates a local array of std::string:
std::string children[2];
When the function returns, that array is destroyed so the pointer you return immediately becomes invalid.
A better version would be to use the wrapper class for plain arrays, std::array:
#include <array>
std::array<std::string,2> Tree::getChildren(int node) {
std::array<std::string,2> children;
for (int i = 0; i < 2; i++) {
children[i] = tree[2 * node + i];
}
return children;
}
And use it:
auto k = t.getChildren(1);
cout << k[0]<<","<<k[1] << endl;
Below is my code just a function called kk that will be recursively called for 10 times so impossible to cause stack overflow, but it crashes with
Error in `./3': free(): invalid pointer: 0x0000000000602100
Who knows the reason??
string kk(string &s)
{
static int i=0;
s+="q";
i++;
cout<<"i="<<i<<endl;
if(i>=10) return s;
kk(s);
}
int main()
{
string s="wer";
cout<<"s="<<kk(s)<<endl;
}
I guess you forgot to put the return keyword in the last line. By putting it, the code is working without errors
string kk(string &s)
{
static int i=0;
s+="q";
i++;
cout<<"i="<<i<<endl;
if(i>=10) return s;
return kk(s);
}
int main()
{
string s="wer";
cout<<"s="<<kk(s)<<endl;
}
C26444 NO_UNNAMED_RAII_OBJECTS this was causing the problem.
On every return, the temporary object was getting created and deleted.
Modified the code like below:
#include <iostream>
#include <cstdlib>
static int i = 0;
void kk(std::string& s)
{
s += "q";
i++;
std::cout << "i=" << i << std::endl;
if (i >= 10) return ;
kk(s);
}
int main()
{
std::string s = "wer";
kk(s);
std::cout << "s=" << s << std::endl;
}
Output:
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
s=werqqqqqqqqqq
I am a beginner in programming, I am trying to solve the problem I encountered in my assignment.
Here I want to read a file and save each word as an element in a pointer pointing array.
I declared pointer in Array class and reading the file in ReadAcc class. I am trying to process it outside these two classes.
class ReadAcc {
private:
double*_customerBalanceArray;
double read1;
ifstream f;
int counter = 0;
public:
ReadAcc() {
_customerBalanceArray = new double[100];
}
int GetCounter() {
return counter;
}
void Reading(string path)
{
f.open(path);
while (!f.eof())
{
f >> read1;
_customerBalanceArray[counter] = read1;
counter++;
}
f.close();
}
double* GetPointer() {
return _customerBalanceArray;
}
void DeletePointer() {
delete[] _customerBalanceArray;
}
};
int main()
{
ReadAcc read;
read.Reading("acc.txt");
for (int i = 0; i < read.GetCounter(); i=i+2)
{
//cout << "Acc# " << *(a.getCustomerBlanceArray) << " Balance: " << *(a.getCustomerBlanceArray) << endl;
}
read.DeletePointer();
}
When it compiles, the error says Exception thrown: read access violation.
Access violation writing location 0x013CE000. The pointer _customerBalanceArray value is now 0x013CE000. I do not know where it went wrong. Please help me. Thanks a million!!!
I have a segmentation fault error at runtime, here is my code with an indication of the line where the error occurs :
EDIT : the variable Scenario is an attribute of the class Probleme, the code I posted is the code of its constructor
EDIT : The error happens at runtime
class scenario
{
public:
int id;
std::vector<std::vector<int> > demandes;
scenario() {}
void setid(int i) { id=i; }
void setdemande( std::vector<int> v) { demandes.push_back(v); }
int getid() const { return id; }
};
Scenario.resize(nb_scenario);
for (int i = 0; i < nb_scenario; ++i)
{
Scenario[i].setid(i);
}
int s;
for(int i=0; i<nb_periode ; i++) {
fichier >> s ;
for(int j=0;j<nb_produit;j++){
fichier >> s ;
for(int k=0;k<nb_scenario;k++){
fichier >> s;
Scenario[k].demandes[j][i]= s; //Error here
}
}
}
You never add anything to the demandes vector. Calling operator[] on an empty vector (the index is always out of bounds) is undefined behavior.
To fix this add elements to demandes using push_back or resize. To resize the inner vectors at the same time you can do demandes.resize(N, std::vector<int>(M)). Also consider using at instead of operator[], since it will throw an exception on an out of bounds access instead of causing undefined behavior.
Hi I have a problem with deleting an object array.
Whenever I start my code, it works just fine, but when I close,
I am getting the error: 0xC0000005: Access violation reading location 0xcccccccc.
The code goes like this:
I initialize an instance of an object and immediately make an empty array out of it.
Class* classObject[15];
Afterwards, I define the empty array in a for loop.
for(int i = 0; i < 15; i++){
classObject[i] = new Class();
}
When the application closes, the following code should delete the array out of memory.
for(int i = 0; i < 15; i++){
delete classObject[i];
}
Instead of successfully closing, I am getting the Access violation error.
How can I fix this problem and where?
Also, are there maybe other ways I could create objects in a for loop?
class A
{
public:
A():a(0){};
private:
int a;
};
int main()
{
A* arr[15];
for(int i=0;i<15;i++)
{
arr[i] = new A();
}
for(int i =0;i<15;i++)
{
delete arr[i];
}
return 0;
}
There is no any error in my code .Have you delete the point before?