I have this struct:
struct event_ {
bool is_crossover = false;
bool is_birth = false;
bool is_repetitive = false;
int eID = 0;
bool inicio_fin = false;
fecha inicio_fecha;
fecha fin_fecha;
locacion inicio_l;
string eLatitud_i = 0;
string eLongitud_i = 0;
locacion fin_l;
string eLatitud_f = 0;
string eLongitud_f = 0;
personaje_info personajes_evento; //This is a class
int cantidad_personajes = 0;
string nombre;
string descripcion;
string tipo_evento;
event_ *sig, *ant;
};
And then, when I call the function:
event_ *n = new event_;
it sends me an Access Violation Error:
Exception thrown at 0x0F69F6E0 (ucrtbased.dll) in Auxiliar Libros.exe: 0xC0000005: Access violation reading location 0x00000000.
Anyone knows why is this happening?
As additional information, I ran a Code Metrics Analysis, and before this, the program worked perfectly fine. And also it tells me about exceptions, what should I do?
This code
string eLongitud_f = 0;
calls the string constructor with a NULL pointer (0 is another way of writing the NULL pointer), resulting in your access validation error.
What do you think that code is doing? Obviously 0 is an integer not a string. Did you mean this?
string eLongitud_f = "0";
Or did you mean this?
string eLongitud_f = "";
Maybe you even meant this
double eLongitud_f = 0.0;
You can also just have this
string eLongitud_f;
which is the same as the second alternative above. All these are possible, it's hard to know which you really want, but the fundamental problem is that you are have a string variable and you are trying to give it a value which is not a string.
To solve your problem, I think that the best thing to do is to reduce your code and try some combinaison.
First, you must try a little struct with only one bool variable to see if your new function is correct
struct event_
{
bool is_crossover = false;
};
event_ *n = new event_;
If your program continue to crash, your error is there, in new().
Else you can try then to reduce your structure removing what you think is correct.
Personnaly, I think that all your bool, int and event_ declaration are correct, so I remove them.
I think that similar object declaraction can also be removed and I remove them.
I have following structure:
struct event_
{
fecha fin_fecha;
locacion inicio_l;
string eLatitud_i = 0;
personaje_info personajes_evento;
};
What happens when you build and run this code ?
If you program has stopped to crash, the error is in removed code ?
Else, one (or more) declaration's line of this new structure is incorrect.
If changing your struct has too much impact in your code, your create a similar structure (other name not yet used) and you test it.
Please, can you try ? I think that you will find very quickly solve the problem yourself !
There are too much variables in your first code that can produce your crash ?
Related
I'd like to use C++ version of SymSpell, which is called SymSpellPlusPlus. In C# version using WordSegmentation looks like this (from the first link):
//word segmentation and correction for multi-word input strings with/without spaces
inputTerm="thequickbrownfoxjumpsoverthelazydog";
maxEditDistance = 0;
suggestion = symSpell.WordSegmentation(input);
//display term and edit distance
Console.WriteLine(suggestion.correctedString + " " + suggestion.distanceSum.ToString("N0"));
In C++ version method WordSegmentation returns shared pointer (from the second link):
...
shared_ptr<WordSegmentationItem> WordSegmentation(const char* input)
{
return WordSegmentation(input, this->maxDictionaryEditDistance, this->maxDictionaryWordLength);
}
shared_ptr<WordSegmentationItem> WordSegmentation(const char* input, size_t maxEditDistance)
{
return WordSegmentation(input, maxEditDistance, this->maxDictionaryWordLength);
}
shared_ptr<WordSegmentationItem> WordSegmentation(const char* input, size_t maxEditDistance, size_t maxSegmentationWordLength)
{
// lines 1039 - 1179 under second link
std::vector<shared_ptr<WordSegmentationItem>> compositions;
...
return compositions[circularIndex];
}
In my code I tried among others the following code:
const char* inputTerm = "whereis th elove hehad dated forImuch of thepast who couqdn'tread in sixtgrade and ins pired him";
auto suggestions = symSpell.WordSegmentation(inputTerm);
But it gives an error:
free() invalid next size (fast)
It is related to memory error, but I don't know how to overcome this problem.
Class WordSegmentationItem looks as follows (lines 292-325 in second link):
class WordSegmentationItem
{
public:
const char* segmentedString{ nullptr };
const char* correctedString{ nullptr };
u_int8_t distanceSum = 0;
double probabilityLogSum = 0;
WordSegmentationItem() { }
WordSegmentationItem(const symspell::WordSegmentationItem & p)
{
this->segmentedString = p.segmentedString;
this->correctedString = p.correctedString;
this->distanceSum = p.distanceSum;
this->probabilityLogSum = p.probabilityLogSum;
}
WordSegmentationItem& operator=(const WordSegmentationItem&) { return *this; }
WordSegmentationItem& operator=(WordSegmentationItem&&) { return *this; }
void set(const char* pSegmentedString, const char* pCorrectedString, u_int8_t pDistanceSum, double pProbabilityLogSum)
{
this->segmentedString = pSegmentedString;
this->correctedString = pCorrectedString;
this->distanceSum = pDistanceSum;
this->probabilityLogSum = pProbabilityLogSum;
}
~WordSegmentationItem()
{
delete[] segmentedString;
delete[] correctedString;
}
};
How should I get the correctedString from the WordSegmentationItem?
The library is buggy and the author needs to make some fixes.
First, compiling gives us a warning about SuggestItem::ShallowCopy, which returns a local variable by reference. Very bad! We can change that to return by value, probably.
This doesn't fix the crash, though.
If we clone the library's repo then run the following testcase in a debugger:
#include "symspell6.h"
int main()
{
const char* inputTerm = "whereis th elove hehad dated forlmuch of thepast who couqdn'tread in sixtgrade and ins pired him";
symspell::SymSpell symSpell;
auto suggestions = symSpell.WordSegmentation(inputTerm);
}
…we see that returning compositions[circularIndex] from the WordSegmentation function is causing an invalid access in the shared_ptr constructor. This suggests that circularIndex is out-of-bounds and giving us a non-existent shared_ptr. Indeed, circularIndex is 95 but compositions.size() is 0!
The function is lacking some serious error checking.
Now, only the author (or at least someone who knows what the library is supposed to do; that's not me!) can fix this properly. But as a quick patch I added the following after line 1055:
if (compositions.empty())
return nullptr;
…and it now at least runs.
It seems that the function assumes the dictionary is non-empty. I don't know whether that's expected behaviour or not (other than the missing error checking as detailed above).
The project is in serious need of some documentation, because no preconditions or postconditions are mentioned for these functions and there is no indication as to how the library is supposed to be used. Again, the author should fix these things.
#include <iostream>
using namespace std;
const int ALPHABET = 26;
const int LANG = 4;
const double TOLK[LANG][ALPHABET]= {{0}};
class Text
{
private:
string sample;
int* histogram;
double* rel_histogram;
int sample_size;
public:
Text();
~Text();
string parse();
};
string parsing(const double TOLK[][ALPHABET], double rel_occurence_arr[]);
int main()
{
Text myText;
myText.parse();
return 0;
}
Text::Text(){
sample = "";
histogram = new int[ALPHABET];
rel_histogram = new double[ALPHABET];
sample_size = 0;
}
Text::~Text(){
delete[] histogram;
delete[] rel_histogram;
}
string Text::parse(){
parsing(TOLK, rel_histogram);
//Invalid pointer here
}
string parsing(const double TOLK_HJALP[][ALPHABET], double rel_occurence_arr[]){
return "test";
}
This is part of a larger code, but I've peeled of everything I could till only the parts causing the error remains. Running it like this results in a invalid pointer error, running it with all the extra bits causes a double free/corruption error. But I think that if I can figure it out at this level I can probably figure it out at the larger scale.
From what I've gathered, I think that the Text class is trying to delete something which has already been deleted when the parsing function returned. I don't know if that is correct, but if it was, I have no idea on how to stop it from happening. It doesn't matter if I send a copy(in the way I tried, maybe there are more ways than one?).
And also, removing iostream from the include seems to remove the error, for whatever reason. Why is that? It isn't even used here?
Thanks in advance.
There are two issues with your code that I can see.
(1) This should be what is causing your error. You are not including string, and iostream doesn't need to include it. This means you are returning a pointer to a char from parsing, but the pointer is deleted when parsing returns. This results in undefined behavior.
(2) parse doesn't return a value, but it promises to in its declaration. That could cause some issues.
Note: You should try using -Wall when you run into a problem (or just all the time). That would have caught both of those errors for you.
I have a char pointer as a private member of a class. I need to read record from a file and insert it into class array. First, I need to get number of record first then create a myStudent array during runtime. Then insert all the record in. But when I tried to initialize the name field using set method, it gave me Practise1(3278,0x7fff7287e300) malloc:error for object 0x100105578: incorrect checksum for freed object - object was probably modified after being freed. set a breakpoint in malloc_error_break to debug error
if i use debugger to run the program step-by-step, it works perfectly fine with no error, however if i run it normally, it gave me the above error. (Sometimes it works, sometime it doesn't)
Here is a small portion of my code:
myClass.h:
class myClass{
private:
char *name;
public:
myClass();
void setName(string);
}
myClass.cpp
myClass:: myClass(){}
void myClass::setName(string x){
name = new char[x.length()+1]; //my xcode give me signal SIGBART here
strcpy(name, x.c_str());
}
main.cpp
int main(){
myClass *classArr;
int amountRecord = getRecord(); //check the number of record and return it(assuming it return 5)
classArr = new myClass[amountRecord];
loadClassData("test.dat",classArr);
}
void loadClassData(string filename,myClass *classArr){
ifstream ins(filename,ios::in);
int counter = 0;
string className;
string temp;
if(ins.good()){
while(!ins.eof()){
className = "";
getline(ins, className,'\n');
classArr[counter].setName(className);
counter++;
}
ins.close();
}
The problem is in how you loop when reading (see Why is “while ( !feof (file) )” always wrong? for why).
This causes the loop to iterate one extra time leading you to use an out-of-bounds index into the classArr array, which leads to undefined behavior and the crash.
Instead do e.g. while (std::getline(ins, className))
In function void myClass::setName(string x) you are using some variable called sName.
I have no idea where it's declared, but you should be using the variable x that is passed in the function.
Where sName is come from? I think it should be like this.
myStudent::myStudent(){}
void myStudent::setName(string x){
name = new char[x.length()+1]; //my xcode give me signal SIGBART here
strcpy(name, x.c_str());
}
I have this structure in my code.
'Compilable' part of code:
#define MONITOR_TOPKEY HKEY_LOCAL_MACHINE
#define MONITOR_SUBKEY TEXT("SOFTWARE\\WMyRegistry")
struct params {
HKEY hMainKey;
LPTSTR hSubKey;
string path;
bool* runflg;
};
void _tmain(void) {
bool work = true;
string defaultPath = "HKEY_LOCAL_MACHINE";
defaultPath += "\\";
defaultPath += MONITOR_SUBKEY;
params* defaultParams = (params*) malloc(sizeof (params));
defaultParams->hMainKey = MONITOR_TOPKEY;
defaultParams->hSubKey = MONITOR_SUBKEY;
defaultParams->path = defaultPath; // HERE THERE IS A PROBLEM
defaultParams->runflg = &work;
}
When I set all parametrs (except "string") - all are good and working.
But when I try to inizialize 'string' parametr (or another type instead of this, for ex myClass type or another else type variable) I have the error
"Unhandled exception at 0x0FDEEAD0 (msvcr110d.dll) in ConsoleApplication1.exe:
0xC0000005: Access violation when writing to the address 0xCDCDCDCD."
I don't understand, why doens't work " defaultParams->path = defaultPath". Can someone explain?
I think there may be something wrong with malloc. Because malloc just allocate some memory for the object.
The string in your code may excess the boundary of the the memory you allocated. So there is access violation.
Try to use new instead of malloc.
You are using malloc on a struct with a C++ class std:string in it
malloc knows nothing about constructors so your string will not be initialized.
instead use new/delete and avoid using malloc/free in your C++ program
params* defaultParams = new params;
or preferably
std::unique_ptr<params> defaultParams(new params);
here you use a Registry class obj which initialize value second obj,
you can't initialize obj without using assignment operator overload.
first you should overload the assignment.
I have the following structure:
struct CountCarrier
{
int *CurrCount;
};
And this is what I want to do:
int main()
{
CountCarrier carrier = CountCarrier();
*(carrier.CurrCount) = 2; // initialize the *(carrier.CurrCount) to 2
IncreaseCount(&carrier); // should increase the *(carrier.CurrCount) to 3
}
void IncreaseCount(CountCarrier *countCarrier)
{
int *currCounts = countCarrier->CurrCount;
(*currCounts)++;
}
So, my intention is specified in the comments.
However, I couldn't get this to work. For starters, the program throws an exception at this line:
*(carrier.CurrCount) = 2;
And I suspect the following line won't work as well. Anything I did wrong?
struct CountCarrier
{
int *CurrCount; //No memory assigned
};
You need to allocate some valid memory to the pointer inside the structure to be able to put data in this.
Unless you do so, What you ar trying to do is attempting to write at some invalid address, which results in an Undefined Behavior, which luckiy in this case shows up as an exception.
Resolution:
struct CountCarrier
{
int *CurrCount; //No memory assigned
CountCarrier():CurrCount(new(int))
{
}
};
Suggestion:
Stay away from dynamic allocations as long as you can.
When you think of using pointers always think whether you really need one. In this case it doesn't really seem that you need one, A simple int member would be just fine.
You need to create the pointer. ie. carrier->CurrCount = new int;
*(carrier.CurrCount)
This is dereferencing the pointer carrier.CurrCount, but you never initialized it. I suspect this is what you want:
carrier.CurrCount = new int(2);
I seriously doubt that your program throws an exception at the line:
*(carrier.CurrCount) = 2;
While throwing an exception is certainly allowed behaviour, it seems much more likely that you encountered an access violation that caused the process to be killed by the operating system.
The problem is that you are using a pointer, but your pointer is not initialised to point at anything. This means that the result of the pointer dereference is undefined.
In this situation there does not seem to be any advantage to using a pointer at all. Your CurrCount member would work just as well if it was just a plain int.
If you are using C++, then you should encash its facilities. Instead of correcting your code, I am showing here that how the code should look like:
struct CountCarrier
{
int CurrCount; // simple data member
CountCarrier(int count) : CurrCount(count) {} // constructor
CountCarrier& operator ++ () // overloaded operator
{
++ CurrCount;
return *this;
}
};
We are overloading operator ++, because you have only one data member. You can replace with some named method also, like void IncrementCount().
CountCarrier carrier(2);
++ carrier;
As Als said, you need to provide some memory for the code to work.
But why make it so complicated? You don't need any pointers for the code you have to work. The "modern C++" way looks more like this:
struct CountCarrier
{
public:
CountCarrier(int currCount) : currCount(currCount) {}
void IncreaseCount() { ++currCount; }
int GetCount() const { return currCount; }
private:
int currCount;
};
int main()
{
CountCarrier carrier(2); // Initialize carrier.currCount to 2
carrier.IncreaseCount(); // Increment carrier.currCount to 3
}
Note how much cleaner and less error prone that is. Like I said, pick up a good introductory C++ book and read through it.