Getting “./a.out” terminated by signal SIGSEGV (Address boundary error) - c++11

I'm writing a program that splits any two numbers. The problem is whenever I run the program I get an error that says:
“./a.out” terminated by signal SIGSEGV (Address boundary error)
And that error occurs at the lines:
a = std::stoi(temp_vec.front());
b = std::stoi(temp_vec.back());
and
c = std::stoi(temp_vec.front());
d = std::stoi(temp_vec.back());
Here's my program:
#include <iostream>
#include <string>
#include <vector>
void split_number(std::vector<std::string> vect, int x);
int main()
{
int x = 0, y = 0, a = 0, b = 0, c = 0, d = 0;
std::vector<std::string> temp_vec;
std::cout << "Enter x: ";
std::cin >> x;
std::cout << "Enter y: ";
std::cin >> y;
split_number(temp_vec, x);
a = std::stoi(temp_vec.front());
b = std::stoi(temp_vec.back());
split_number(temp_vec, y);
c = std::stoi(temp_vec.front());
d = std::stoi(temp_vec.back());
return 0;
}
void split_number(std::vector<std::string> vect, int x)
{
vect.clear();
//1. convert x to string
std::string temp_str = std::to_string(x);
//2. calculate length
std::size_t len = temp_str.length();
std::size_t delm = 0;
if(len % 2 == 0) {
delm = len / 2;
} else {
delm = (len + 1) / 2;
}
//3. populate vector
vect.push_back(temp_str.substr(0, delm));
vect.push_back(temp_str.substr(delm + 1));
}
Any help would be appreciated.

You get the segmentation fault because your vector is empty. Your vector is empty because you pass a copy of your initial vector to split_number(). The copy is passed because the signature of split_number() says it requires a copy. Change it to:
void split_number(std::vector<std::string> & vect, int x)
The ampersand makes the vect parameter a reference parameter, and modifications will show in the calling code.

Related

Possibly Wrong Equations of planes of Convex Hull using CGAL

I am new to Computational Geometry. This may be a naive question. I have used CGAL to find convex hull of a set of points. Then I extract the planes for convex hull using the example code given on the CGAL website here. The program is given below.
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/GMP/Gmpq_type.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/convex_hull_3.h>
#include <CGAL/Side_of_triangle_mesh.h>
#include <CGAL/number_utils.h>
#include <iomanip>
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron_3;
typedef Kernel::Point_3 Point_3;
typedef Kernel::Plane_3 Plane_3;
typedef Kernel::Vector_3 Vector_3;
typedef CGAL::Side_of_triangle_mesh<Polyhedron_3, Kernel> Point_inside;
struct Plane_equation {
template <class Facet>
typename Facet::Plane_3 operator()( Facet& f) {
typename Facet::Halfedge_handle h = f.halfedge();
typedef typename Facet::Plane_3 Plane;
return Plane( h->vertex()->point(),
h->next()->vertex()->point(),
h->next()->next()->vertex()->point());
}
};
Point_3 create_point(std::string x, std::string y, std::string z) {
Point_3 p;
std::istringstream input(x + " " + y + " " + z);
input >> p;
return p;
}
int main() {
std::vector<Point_3> points;
points.push_back(create_point("-780692485006853753617237/10240000000000000000000000000",
"-1563546083667768699030143/2500000000000000000000000",
"0"));
points.push_back(create_point("379213264894343110801237/10240000000000000000000000000",
"-1563546083667768699030143/2500000000000000000000000",
"0"));
points.push_back(create_point("-780692485006853753617237/10240000000000000000000000000",
"0",
"0"));
points.push_back(create_point("379213264894343110801237/10240000000000000000000000000",
"-379213264894343110801237/10240000000000000000000000000",
"0"));
points.push_back(create_point("-780692485006853753617237/10240000000000000000000000000",
"3223865085330535926726143/2500000000000000000000000",
"-780692485006853753617237/10240000000000000000000000000"));
points.push_back(create_point("379213264894343110801237/10240000000000000000000000000",
"3223865085330535926726143/2500000000000000000000000",
"379213264894343110801237/10240000000000000000000000000"));
points.push_back(create_point("379213264894343110801237/10240000000000000000000000000",
"0",
"379213264894343110801237/10240000000000000000000000000"));
points.push_back(create_point("-780692485006853753617237/10240000000000000000000000000",
"780692485006853753617237/10240000000000000000000000000",
"-780692485006853753617237/10240000000000000000000000000"));
points.push_back(create_point("0", "0", "0"));
Polyhedron_3 poly;
CGAL::convex_hull_3(points.begin(), points.end(), poly);
// CGAL::draw(poly);
std::transform(poly.facets_begin(), poly.facets_end(), poly.planes_begin(), Plane_equation());
CGAL::set_pretty_mode(std::cout);
std::copy(poly.planes_begin(), poly.planes_end(),
std::ostream_iterator<Plane_3>(std::cout, "\n"));
Point_inside inside_tester(poly);
CGAL::Bounded_side res = inside_tester(Point_3(0, 0, 0));
if (res == CGAL::ON_BOUNDED_SIDE) {
std::cout << "Origin is inside" << "\n";
} else if (res == CGAL::ON_BOUNDARY) {
std::cout << "Origin is on the boundary" << "\n";
} else {
std::cout << "Origin is outside the polyhedron" << "\n";
}
for (auto it = poly.planes_begin(); it != poly.planes_end(); ++it) {
std::cout << "A = " << it->a().exact() << "\n";
std::cout << "B = " << it->b().exact() << "\n";
std::cout << "C = " << it->c().exact() << "\n";
std::cout << "D = " << it->d().exact() << "\n";
std::cout << "\n";
}
return EXIT_SUCCESS;
}
After generating convex hull using convex_hull_3, I try to extract the plane equations for all the planes. Below is the output for the program
Plane_3(0, -4.19475e-09, 7.08424e-05, -2.62348e-09)
Plane_3(-4.76816e-05, 0, 0, -3.63522e-09)
Plane_3(4.76788e-05, 0, -7.08382e-05, -1.76567e-09)
Plane_3(-9.83085e-05, 0, 0, -7.49499e-09)
Plane_3(-0.00014607, 8.6358e-09, 0.00014607, -1.11363e-08)
Plane_3(9.83085e-05, 0, -0.000146061, -3.64062e-09)
Plane_3(0.000146074, 4.19475e-09, -0.000146074, -5.40933e-09)
Plane_3(0, -8.6358e-09, -7.08511e-05, -5.40099e-09)
Plane_3(-4.77552e-05, 0, 0.00014607, -3.64083e-09)
Plane_3(-2.31608e-05, 0, 7.08424e-05, -1.76577e-09)
Plane_3(4.77552e-05, 0, 0, -1.7685e-09)
Plane_3(2.31595e-05, 0, 0, -8.57654e-10)
Origin is inside
A = 0
B = -219925823194877128532786493609493825335102426169/52428800000000000000000000000000000000000000000000000000
C = 906783046340871373991245871626108775814746030891/12800000000000000000000000000000000000000000000000000
D = -343864159553760260821702206832186399183132849909762204558692676563012167/131072000000000000000000000000000000000000000000000000000000000000000000000000000
A = -1220648677481324419677714143344481488159947374891/25600000000000000000000000000000000000000000000000000
B = 0
C = 0
D = -952951249343224727578040741115129097199790308301319818242882753758596167/262144000000000000000000000000000000000000000000000000000000000000000000000000000
A = 4999480934017386895980017109227629957533970363431367/104857600000000000000000000000000000000000000000000000000
B = 0
C = -3713963431989014270739610303686932051911864640103367/52428800000000000000000000000000000000000000000000000000
D = -1895869487765753248495994462519358098874827535092392449815572846416028200979/1073741824000000000000000000000000000000000000000000000000000000000000000000000000000
A = -10308396833918147444194565967124294714798768363431367/104857600000000000000000000000000000000000000000000000000
B = 0
C = 0
D = -8047687940708342026893035469747920357861814098710951530122415210209637672979/1073741824000000000000000000000000000000000000000000000000000000000000000000000000000
A = -1869689824690300646434385556309825774908173982891/12800000000000000000000000000000000000000000000000000
B = 452764851132051796393052935016376879217993818169/52428800000000000000000000000000000000000000000000000000
C = 1869689824690300646434385556309825774908173982891/12800000000000000000000000000000000000000000000000000
D = -1459652795429499560461049209852394023543048968469784428121757724600692167/131072000000000000000000000000000000000000000000000000000000000000000000000000000
A = 10308396833918147444194565967124294714798768363431367/104857600000000000000000000000000000000000000000000000000
B = 0
C = -7657796757080339395998850185710029997144662640103367/52428800000000000000000000000000000000000000000000000000
D = -3909080819216610292966257636154114768379010997867208966773927801542028200979/1073741824000000000000000000000000000000000000000000000000000000000000000000000000000
A = 1531693889550933264984755205027731173569843147269541/10485760000000000000000000000000000000000000000000000000
B = 219925823194877128532786493609493825335102426169/52428800000000000000000000000000000000000000000000000000
C = -1531693889550933264984755205027731173569843147269541/10485760000000000000000000000000000000000000000000000000
D = -709011182760540911322636241675226035790615443402528424913924328539636167/131072000000000000000000000000000000000000000000000000000000000000000000000000000
A = 0
B = -452764851132051796393052935016376879217993818169/52428800000000000000000000000000000000000000000000000000
C = -742927224532668239932907228623111584523283547269541/10485760000000000000000000000000000000000000000000000000
D = -707918709809939897610498041142184898705751664465632699646023679792068167/131072000000000000000000000000000000000000000000000000000000000000000000000000000
A = -1222532404587072576861092389210725463247004638891/25600000000000000000000000000000000000000000000000000
B = 0
C = 1869689824690300646434385556309825774908173982891/12800000000000000000000000000000000000000000000000000
D = -954421860938486024795167813392498809224920480657508534678004510218164167/262144000000000000000000000000000000000000000000000000000000000000000000000000000
A = -592917415200418328304777599907736063469544686891/25600000000000000000000000000000000000000000000000000
B = 0
C = 906783046340871373991245871626108775814746030891/12800000000000000000000000000000000000000000000000000
D = -462886170276655067642955341169240700211713020629945581049164605825540167/262144000000000000000000000000000000000000000000000000000000000000000000000000000
A = 1222532404587072576861092389210725463247004638891/25600000000000000000000000000000000000000000000000000
B = 0
C = 0
D = -463600504582595797850104669957953262356310406147548315149844146861108167/262144000000000000000000000000000000000000000000000000000000000000000000000000000
A = 2428445929960641645499203498146234146289758916775367/104857600000000000000000000000000000000000000000000000000
B = 0
C = 0
D = -920898909719754197337687026347401189092620563028663434317768459371714728979/1073741824000000000000000000000000000000000000000000000000000000000000000000000000000
You can notice the line which says that origin is inside the convex hull. Now if the origin is inside the convex hull, then if I check the side of origin relative to a plane by checking 0*a + 0*b + 0*c + d, then for some planes, the output should be positive and for some it should be negative. But in this case, the outputs (essentially the d value) is negative for all planes. What is it that I'm doing wrong here?

Recursive algorithm to find all possible solutions in a nonogram row

I am trying to write a simple nonogram solver, in a kind of bruteforce way, but I am stuck on a relatively easy task. Let's say I have a row with clues [2,3] that has a length of 10
so the solutions are:
$$-$$$----
$$--$$$---
$$---$$$--
$$----$$$-
$$-----$$$
-$$----$$$
--$$---$$$
---$$--$$$
----$$-$$$
-$$---$$$-
--$$-$$$--
I want to find all the possible solutions for a row
I know that I have to consider each block separately, and each block will have an availible space of n-(sum of remaining blocks length + number of remaining blocks) but I do not know how to progress from here
Well, this question already have a good answer, so think of this one more as an advertisement of python's prowess.
def place(blocks,total):
if not blocks: return ["-"*total]
if blocks[0]>total: return []
starts = total-blocks[0] #starts = 2 means possible starting indexes are [0,1,2]
if len(blocks)==1: #this is special case
return [("-"*i+"$"*blocks[0]+"-"*(starts-i)) for i in range(starts+1)]
ans = []
for i in range(total-blocks[0]): #append current solutions
for sol in place(blocks[1:],starts-i-1): #with all possible other solutiona
ans.append("-"*i+"$"*blocks[0]+"-"+sol)
return ans
To test it:
for i in place([2,3,2],12):
print(i)
Which produces output like:
$$-$$$-$$---
$$-$$$--$$--
$$-$$$---$$-
$$-$$$----$$
$$--$$$-$$--
$$--$$$--$$-
$$--$$$---$$
$$---$$$-$$-
$$---$$$--$$
$$----$$$-$$
-$$-$$$-$$--
-$$-$$$--$$-
-$$-$$$---$$
-$$--$$$-$$-
-$$--$$$--$$
-$$---$$$-$$
--$$-$$$-$$-
--$$-$$$--$$
--$$--$$$-$$
---$$-$$$-$$
This is what i got:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
typedef std::vector<bool> tRow;
void printRow(tRow row){
for (bool i : row){
std::cout << ((i) ? '$' : '-');
}
std::cout << std::endl;
}
int requiredCells(const std::vector<int> nums){
int sum = 0;
for (int i : nums){
sum += (i + 1); // The number + the at-least-one-cell gap at is right
}
return (sum == 0) ? 0 : sum - 1; // The right-most number don't need any gap
}
bool appendRow(tRow init, const std::vector<int> pendingNums, unsigned int rowSize, std::vector<tRow> &comb){
if (pendingNums.size() <= 0){
comb.push_back(init);
return false;
}
int cellsRequired = requiredCells(pendingNums);
if (cellsRequired > rowSize){
return false; // There are no combinations
}
tRow prefix;
int gapSize = 0;
std::vector<int> pNumsAux = pendingNums;
pNumsAux.erase(pNumsAux.begin());
unsigned int space = rowSize;
while ((gapSize + cellsRequired) <= rowSize){
space = rowSize;
space -= gapSize;
prefix.clear();
prefix = init;
for (int i = 0; i < gapSize; ++i){
prefix.push_back(false);
}
for (int i = 0; i < pendingNums[0]; ++i){
prefix.push_back(true);
space--;
}
if (space > 0){
prefix.push_back(false);
space--;
}
appendRow(prefix, pNumsAux, space, comb);
++gapSize;
}
return true;
}
std::vector<tRow> getCombinations(const std::vector<int> row, unsigned int rowSize) {
std::vector<tRow> comb;
tRow init;
appendRow(init, row, rowSize, comb);
return comb;
}
int main(){
std::vector<int> row = { 2, 3 };
auto ret = getCombinations(row, 10);
for (tRow r : ret){
while (r.size() < 10)
r.push_back(false);
printRow(r);
}
return 0;
}
And my output is:
$$-$$$----
$$--$$$---
$$---$$$--
$$----$$$--
$$-----$$$
-$$-$$$----
-$$--$$$--
-$$---$$$-
-$$----$$$-
--$$-$$$--
--$$--$$$-
--$$---$$$
---$$-$$$-
---$$--$$$
----$$-$$$
For sure, this must be absolutely improvable.
Note: i did't test it more than already written case
Hope it works for you

C++11 app that uses dispatch_apply not working under Mac OS Sierra

I had a completely functioning codebase written in C++11 that used Grand Central Dispatch parallel processing, specifically dispatch_apply to do the basic parallel for loop for some trivial game calculations.
Since upgrading to Sierra, this code still runs, but each block is run in serial -- the cout statement shows that they are being executed in serial order, and CPU usage graph shows no parallel working on.
Queue is defined as:
workQueue = dispatch_queue_create("workQueue", DISPATCH_QUEUE_CONCURRENT);
And the relevant program code is:
case Concurrency::Parallel: {
dispatch_apply(stateMap.size(), workQueue, ^(size_t stateIndex) {
string thisCode = stateCodes[stateIndex];
long thisCount = stateCounts[stateIndex];
GameResult sliceResult = playStateOfCode(thisCode, thisCount);
results[stateIndex] = sliceResult;
if ((stateIndex + 1) % updatePeriod == 0) {
cout << stateIndex << endl;
}
});
break;
}
I strongly suspect that this either a bug, but if this is GCD forcing me to use new C++ methods for this, I'm all ears.
I'm not sure if it is a bug in Sierra or not. But it seems to work if you explicitly associate a global concurrent queue as target:
dispatch_queue_t target =
dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
dispatch_queue_t workQueue =
dispatch_queue_create_with_target("workQueue", DISPATCH_QUEUE_CONCURRENT, target);
// ^~~~~~~~~~~ ^~~~~~
Here is a working example
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <sstream>
#include <dispatch/dispatch.h>
void load_problem(const std::string, std::vector<std::pair<double,double>>&);
int main() {
// n-factor polynomial - test against a given problem provided as a set of space delimited x y values in 2d.txt
std::vector<std::pair<double,double>> problem;
std::vector<double> test = {14.1333177226503,-0.0368874860476915,
0.0909424058436257,2.19080982673558,1.24632025036125,0.0444549880462031,
1.06824631867947,0.551482840616757, 1.04948148731933};
load_problem("weird.txt",problem); //a list of space delimited doubles representing x, y.
size_t a_count = test.size();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
__block double diffs = 0.0; //sum of all values..
dispatch_apply(problem.size(), queue, ^(size_t i) {
double g = 0;
for (size_t j=0; j < a_count - 1; j++) {
g += test[j]*pow(problem[i].first,a_count - j - 1);
}
g += test[a_count - 1];
diffs += pow(g - problem[i].second,2);
});
double delta = 1/(1+sqrt(diffs));
std::cout << "test: fit delta: " << delta << std::endl;
}
void load_problem(const std::string file, std::vector<std::pair<double,double>>& repo) {
repo.clear();
std::ifstream ifs(file);
if (ifs.is_open()) {
std::string line;
while(getline(ifs, line)) {
double x= std::nan("");
double y= std::nan("");
std::istringstream istr(line);
istr >> std::skipws >> x >> y;
if (!isnan(x) && !isnan(y)) {
repo.push_back({x, y});
};
}
ifs.close();
}
}

Finding incorrect implementation of JudyArray

I'm trying to give a better error report (possible bug) for this case (about judySArray give incorrect result, but I don't know which key that give incorrect result).
The code here from this folder, note on this blog. Dependencies: judySArray.h and cedar.h
// judy.cpp
#include "deps/judySArray.h"
#include <string>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef judySArray<double> MSD;
const int MAX_DATA = 12000000;
const char i2ch[] = {'0','1','2','3','4','5','6','7','8','9','a','B','c','D','e','F'};
int get_first_digit(double d) {
while(d > 10) d /= 10;
return d;
}
string to_rhex(int v) {
char hex[32];
int start = 0;
while(v>0) {
hex[start] = i2ch[v%16];
v /= 16;
++start;
}
hex[start] = 0;
return hex;
}
void add_or_inc(MSD &m, const string& key,double set, double inc, int& ctr) {
const char* cstr = key.c_str();
double it = m.find(cstr);
if(!it) {
m.insert(cstr,set);
return;
}
m.insert(cstr,it+inc);
++ctr;
}
int main() {
MSD m(64);
int dup1 = 0, dup2 = 0, dup3 = 0;
for(int z=MAX_DATA;z>0;--z) {
int val2 = MAX_DATA-z;
int val3 = MAX_DATA*2-z;
string key1 = to_string(z);
string key2 = to_string(val2);
string key3 = to_rhex(val3);
add_or_inc(m,key1,z,val2,dup1);
add_or_inc(m,key2,val2,val3,dup2);
add_or_inc(m,key3,val3,z,dup3);
}
cout << dup1 << ' ' << dup2 << ' ' << dup3 << endl;
int total = 0, verify = 0, count = 0;
for(auto &it = m.begin();m.success(); m.next()) {
total += get_first_digit(it.value);
verify += strlen((const char *) it.key);
count += 1;
}
cout << total << ' ' << verify << ' ' << count << endl;
}
other implementation (map, unordered_map, hat-trie and cedar) give correct result:
6009354 6009348 611297
36186112 159701682 23370001
but judy didn't:
6009354 6009348 611297
36186112 159701681 23370000
The problem is, which key that have incorrect result?
I've tried to build a code that insert those keys on another data structure (that is cedar), but that incorrect keys still not detected:
// judy.cpp
#include "deps/judySArray.h"
#include <string>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
typedef judySArray<double> MSD;
const int MAX_DATA = 12000000;
const char i2ch[] = {'0','1','2','3','4','5','6','7','8','9','a','B','c','D','e','F'};
int get_first_digit(double d) {
while(d > 10) d /= 10;
return d;
}
string to_rhex(int v) {
char hex[32];
int start = 0;
while(v>0) {
hex[start] = i2ch[v%16];
v /= 16;
++start;
}
hex[start] = 0;
return hex;
}
void add_or_inc(MSD &m, const string& key,double set, double inc, int& ctr) {
const char* cstr = key.c_str();
double it = m.find(cstr);
if(!it) {
m.insert(cstr,set);
return;
}
m.insert(cstr,it+inc);
++ctr;
}
#include "deps/cedar.h"
class MSD2 {
public:
vector<double> data;
typedef cedar::da<int> CI;
CI da;
bool exists(const string& key,double &old) {
int idx = -1;
bool found = da.exactMatchExists(key.c_str(),key.size(),&idx);
if(found) old = data[idx];
return found;
}
void insert(const string& key,double val) {
da.update(key.c_str(),key.size(),data.size());
data.push_back(val);
}
void update(const string& key,double val) {
int idx = -1;
bool found = da.exactMatchExists(key.c_str(),key.size(),&idx);
if(found) {
data[idx] = val;
return;
}
insert(key,val);
}
};
void add_or_inc(MSD2 &m, const string& key,double set, double inc, int& ctr) {
double old;
if(!m.exists(key,old)) {
m.insert(key,set);
return;
}
m.update(key,old+inc);
++ctr;
}
int main() {
MSD m(64);
MSD2 m2;
int dup1 = 0, dup2 = 0, dup3 = 0;
int vup1 = 0, vup2 = 0, vup3 = 0;
for(int z=MAX_DATA;z>0;--z) {
int val2 = MAX_DATA-z;
int val3 = MAX_DATA*2-z;
string key1 = to_string(z);
string key2 = to_string(val2);
string key3 = to_rhex(val3);
add_or_inc(m,key1,z,val2,dup1);
add_or_inc(m,key2,val2,val3,dup2);
add_or_inc(m,key3,val3,z,dup3);
add_or_inc(m2,key1,z,val2,vup1);
add_or_inc(m2,key2,val2,val3,vup2);
add_or_inc(m2,key3,val3,z,vup3);
}
cout << dup1 << ' ' << dup2 << ' ' << dup3 << endl;
cout << vup1 << ' ' << vup2 << ' ' << vup3 << endl;
int total = 0, verify = 0, count = 0;
int xotal = 0, xerify = 0, xount = 0;
union { int i; int x; } b;
size_t from = 0, p = 0;
char key[256] = {0};
for (b.i = m2.da.begin(from, p); b.i != MSD2::CI::CEDAR_NO_PATH; b.i = m2.da.next(from, p)) {
double it2 = m2.data[b.x]; // <-- find cedar's
xotal += get_first_digit(it2);
m2.da.suffix(key,p,from);
xerify += strlen(key);
xount += 1;
double it = m.find(key); // <-- find judy's
if(it != it2) { // if value doesn't match, print:
cout << "mismatch value for " << key << " : " << it2 << " vs " << it << endl;
}
}
for(auto &it = m.begin();m.success(); m.next()) {
total += get_first_digit(it.value);
verify += strlen((const char *) it.key);
count += 1;
}
cout << total << ' ' << verify << ' ' << count << endl;
cout << xotal << ' ' << xerify << ' ' << xount << endl;
}
compile with: clang++ -std=c++11 judy-findbug.cpp (or g++ -std=c++11)
the output would be:
6009354 6009348 611297
6009354 6009348 611297
36186112 159701681 23370000 <-- judy's
36186112 159701682 23370001 <-- cedar's
cedar has one more value than judy's (that is correct), but it didn't detected by the code above..
How to find that incorrect key(s)?
The bug on the code is someone (me) uncomment the assert(value != 0).
The bug was Karl's Judy implementation should not store null values (0 value).
Solution: use Doug Baskins' Judy implementation.

Debug Assertion Failed - MSVCP110D.dll

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string token = "000000:::AAAA:::000011:::Hello 8:::::::D Jay!";
string * stringArray = new string[token.size()];
string interim;
int r = 0;
int arrayCounter = 0;
for(int x = 0; x < token.length(); x++)
{
if(token[x] != ':')
{
interim[r] = token[x];
r++;
}
}
for (int x = 0; x < r; x++)
{
cout << interim[x] << endl;
}
system("pause");
return 0;
}
I am new and learning, and have narrowed it down to the line:
interim[r] = token[x];
..But i don't know why it crashes. Advice? I am coding in Visual C++ VSE2012
The string interim has a size of zero. Setting interim[r] = token[x] modifies the string at location r without changing its size. With a size of zero this is undefined behavior.
interim += token[x] is probably what you want.
Example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string token = "000000:::AAAA:::000011:::Hello 8:::::::D Jay!";
string interim;
for(int x = 0; x < token.length(); x++)
{
if(token[x] != ':')
{
interim += token[x];
}
}
cout << interim << endl;
system("pause");
return 0;
}
Output:
000000AAAA000011Hello 8D Jay!
Press any key to continue . . .

Resources