segmentation fault for string function argument - c++

I have a simple main code that gives me segmentation fault when calling a function. In the following code, I have two functions, the first one works correctly but the program doesn't enter the second one and gives me segmentation fault error. Is there any reason for that? I have made sure about the following:
The variables o and c are not out of bound.
cn is initialized correctly.
I have a read-only access to cm and argv. Plus it does not even enter the function evaluate
Here is the code:
void print_cm(vector<vector<int> > *cm, char* gtf);
void evaluate(vector<vector<int> > *cm, char* gtf);
int main(int argc, char** argv)
{
int o = 2; // It is initialized
int c = 4; // It is initialized
vector<vector<int> > cm; // It is initialized
if (argc>4)
print_cm(&cm, argv[o]);
if (argc>4)
{
cout << argv[c] << endl; // Works
// The following also works
for (int i=0; i<cm.size(); i++)
for (int j=0; j<cm[i].size(); j++)
cout << cm[i][j] << " ";
// The following causes segmentation fault;
evaluate(&cm, argv[c]);
}
return 0;
}
void evaluate(vector<vector<int> > *cm, char* gtf)
{
// Read-only access to cm and gtf
}
void print_cm(vector<vector<int> > *cm, char* gtf)
{
// Read-only access to cm and gtf
}
Here is the complete code:
#include "includes/Utility.h"
#include "includes/Graph.h"
void print_cm(vector<vector<int> > *cores, char* output);
void evaluate(vector<vector<int> > const *cm, char* gtf);
int main(int argc, char** argv)
{
int g = -1, c = -1, o = -1;
for (int i=1; i<argc-1; i++)
if (argv[i][0]=='-')
{
if (argv[i][1]=='g')
g = i + 1;
else if (argv[i][1]=='c')
c = i + 1;
else if (argv[i][1]=='k')
ki = i + 1;
else if (argv[i][1]=='s')
si = i + 1;
else if (argv[i][1]=='o')
o = i + 1;
}
Graph G;
if (c>0) G.read_input(argv[g], argv[c]);
else G.read_input(argv[g]);
if (ki > 0)
{
int k = atoi(argv[ki]);
cout << k << endl;
}
if (si > 0)
{
int s = atoi(argv[si]);
cout << s << endl;
}
// Find communities
vector<vector<int> > cores;
G.partitioning(&cores);
if (o>0)
print_cm(&cores, argv[o]);
if (c>0)
{
cout << "here" << endl;
for (size_t i=0; i<cores.size(); i++)
for (size_t j=0; j<cores[i].size(); j++)
if (cores.at(i).at(j)<0) cout << "here";
cout << "here" << endl;
evaluate(&cores, argv[c]);
}
}
return 0;
}
void print_cm(vector<vector<int> > *cores, char* output)
{
ofstream out;
out.open(output);
for(size_t i=0; i<(*cores).size(); i++)
{
for(size_t j=0; j<(*cores)[i].size(); j++)
out << (*cores)[i][j] << " ";
out << endl;
}
out.close();
return ;
}
void evaluate(vector<vector<int> > const *cm, char* gtf)
{
// we evaluate precision, recall, F1 and F2
vector<vector<int> > gt;
ifstream in;
char str[100000000];
in.open(gtf);
while(in.getline(str, 100000000))
{
stringstream s;
s << str;
int a;
gt.resize(gt.size()+1);
while (s >> a) gt[gt.size()-1].push_back(a);
}
in.close();
cout << "==================== Evaluation Results ====================" << endl;
int imax = 0;
for(size_t i=0; i<(*cm).size(); i++)
imax = max(imax, *max_element((*cm)[i].begin(), (*cm)[i].end()));
for(size_t i=0; i<gt.size(); i++)
imax = max(imax, *max_element(gt[i].begin(), gt[i].end()));
vector<bool> flag(imax, false);
vector<double> recall((*cm).size(), 0), precision((*cm).size(), 0), f1((*cm).size(), 0), f2((*cm).size(), 0);
int overlap;
double size = 0;
for(size_t i=0; i<(*cm).size(); i++)
{
// evaluate
size += (double) (*cm)[i].size();
for(size_t j=0; j<(*cm)[i].size(); j++)
flag[(*cm)[i][j]] = true;
double p, r, ff1, ff2;
for(size_t j=0; j<gt.size(); j++)
{
overlap = 0;
for(size_t k=0; k<gt[j].size(); k++)
if (flag[gt[j][k]]) overlap++;
p = (double) overlap / (double) (*cm)[i].size();
if (p > precision[i])
precision[i] = p;
r = (double) overlap / (double) gt[j].size();
if (r > recall[i])
recall[i] = r;
ff1 = (double) 2*(p*r)/(p+r);
if (ff1 > f1[i])
f1[i] = ff1;
ff2 = (double) 5*(p*r)/(4*p + r);
if (ff2 > f2[i])
f2[i] = ff2;
}
for(size_t j=0; j<(*cm)[i].size(); j++)
flag[(*cm)[i][j]] = false;
}
double Recall = 0, Precision = 0, F1 = 0, F2 = 0;
for(size_t i=0; i<(*cm).size(); i++)
{
Recall += recall[i];
Precision += precision[i];
F1 += f1[i];
F2 += f2[i];
}
cout << "+--------------+--------------+--------------+--------------+" << endl;
cout << "| " << setiosflags( ios::left ) << setw(10) << "Precision";
cout << " | " << setiosflags( ios::left ) << setw(10) << "Recall";
cout << " | " << setiosflags( ios::left ) << setw(10) << "F1-measure";
cout << " | " << setiosflags( ios::left ) << setw(10) << "F2-measure";
cout << " |" << endl;
cout << "| " << setiosflags( ios::left ) << setw(10) << Precision/(*cm).size() ;
cout << " | " << setiosflags( ios::left ) << setw(10) << Recall/(*cm).size();
cout << " | " << setiosflags( ios::left ) << setw(10) << F1/(*cm).size();
cout << " | " << setiosflags( ios::left ) << setw(10) << F2/(*cm).size();
cout << " |" << endl;
cout << "+--------------+--------------+--------------+--------------+" << endl;
cout << "Number of communities: " << (*cm).size() << endl;
cout << "Average community size: " << size/(*cm).size() << endl;
return ;
}

char str[100000000];
This is in your evaluate function. This are 100 million bytes, or about 95 MB that you're allocating on the stack.
Typical stack sizes are far less than that, around 1 MB.
So apart from possible other problems this is most likely causing a stack overflow.
When entering the function, the stack frame gets extended to be large enough to hold the local variables. As soon as the stack is used then (to write a default value) you're accessing invalid (non stack, thankfully protected) memory.

Related

How to format output like this

My code is like this so far :
void matrix::print(int colWidth) const
{
cout << getRows() << " x " << getCols() << endl;
cout << "-";
for (unsigned int d = 0; d < getCols(); d++) {
cout << "--------";
}
cout << endl;
for (unsigned x = 0; x < getRows(); x++) {
cout << "|";
for (unsigned y = 0; y < getCols(); y++) {
cout << setw(colWidth) << at(x, y) << " |";
}
cout << endl;
}
cout << "-";
for (unsigned int d = 0; d < getCols(); d++) {
cout << "--------";
}
cout << endl;
}
But the output depends on the colWidth which will be the space between each number printed. So how can I adjust my dashes to be printed like the following no matter the colWidth it should align.
One output should look like this:
Second output is like this:
If the column width is a parameter, you're almost done with your code. Just turn the cout<<"--------" into:
std::cout << std::string(getCols()*(colWidth + 2) + 1, '-');
That code prints a string of dashes, which width is: number of matrix columns, times column width plus 2, plus 1:
Plus 2 because you are appending a " |" to each column.
Plus 1 because you are adding a '|' at the beginning of each row.
You may want to check for empty matrices at the beginning of your print method.
[Demo]
#include <initializer_list>
#include <iomanip> // setw
#include <iostream> // cout
#include <vector>
class matrix
{
public:
matrix(std::initializer_list<std::vector<int>> l) : v{l} {}
size_t getRows() const { return v.size(); }
size_t getCols() const { if (v.size()) { return v[0].size(); } return 0; }
int at(size_t x, size_t y) const { return v.at(x).at(y); }
void print(int colWidth) const
{
std::cout << "Matrix: " << getRows() << " x " << getCols() << "\n";
// +2 due to " |", +1 due to initial '|'
std::cout << std::string(getCols()*(colWidth + 2) + 1, '-') << "\n";
for (unsigned x = 0; x < getRows(); x++) {
std::cout << "|";
for (unsigned y = 0; y < getCols(); y++) {
std::cout << std::setw(colWidth) << at(x, y) << " |";
}
std::cout << "\n";
}
std::cout << std::string(getCols()*(colWidth + 2) + 1, '-') << "\n";
}
private:
std::vector<std::vector<int>> v{};
};
int main()
{
matrix m{{1, 2}, {-8'000, 100'000}, {400, 500}};
m.print(10);
}
// Outputs
//
// Matrix: 3 x 2
// -------------------------
// | 1 | 2 |
// | -8000 | 100000 |
// | 400 | 500 |
// -------------------------

Write a function that returns a pointer to the maximum value using pointers c++

This is the problem that I'm trying to solve for class in C++.
Write a function that returns a pointer to the maximum value of an array of floating-point data: double* maximum(double* a, int size). If size is 0, return nullptr.
The issues I'm having are that:
The final output is not the correct location for the maximum value in the array.
An error that says: "cannot convert 'double**' to 'double*' in the initialization".
If I use nullptr at any point in this code, CodeBlocks gives me an error.
#include <iostream>
using namespace std;
// return pointer to location from function
double * maximum(double* a, int size)
{
double maxVal = a[0]; // this is the starting max value
double* max_pos = &a; // points to the value in a[0]
// initialis]ze both variables
for(int i = 0; i < size; i++){
if(a[i] > maxVal){
maxVal = a[i];
cout << max_pos << endl;
max_pos = &a[i];
}
}
// return address
return max_pos;
}
int main()
{
double myarr[5];
int i = 0;
int arrSize = 5;
cout << "Input 5 floating point values for your array" << endl;
for(i = 0; i < arrSize; i++){ // loop to input values
cin >> myarr[i];
}
for(int j = 0; j < arrSize; j++){
cout << "Location for " << myarr[j] << " = " << &myarr[j] << endl;
}
double* maxNum = maximum( myarr, arrSize);
cout << &maxNum << endl;
return 0;
}
This is the output I'm getting after finding max_pos:
The code you showed has a few mistakes in it:
using namespace std; is bad!
you are not following your instructions to return nullptr when size is 0.
you are trying to initialize max_pos (a double*) with &a (a double**), which is a compiler error.
you are passing &maxNum (a double**) to std::cout, printing the address of the maxNum variable itself, not the address that it is pointing to (the found array element). You need to pass maxNum (a double*) if you want to print the address of the found element, or pass *maxNum (a double) if you want to print the value of the found element.
Try something more like this instead:
#include <iostream>
// return pointer to location from function
double* maximum(double *a, int size)
{
if (size == 0) return 0;
// initialize both variables
double* max_pos = a; // points to the value in a[0]
double maxVal = *max_pos; // this is the starting max value
std::cout << "max_pos = " << max_pos << " (" << maxVal << ")" << std::endl;
for(int i = 1; i < size; ++i){
if (a[i] > maxVal){
max_pos = &a[i];
maxVal = *max_pos;
std::cout << "max_pos = " << max_pos << " (" << maxVal << ")" << std::endl;
}
}
// return address
return max_pos;
}
int main()
{
const int arrSize = 5;
double myarr[arrSize];
std::cout << "Input " << arrSize << " floating point values for your array" << std::endl;
for(int i = 0; i < arrSize; ++i) { // loop to input values
std::cin >> myarr[i];
}
for(int j = 0; j < arrSize; ++j) {
std::cout << "Location for " << myarr[j] << " = " << &myarr[j] << std::endl;
}
double* maxNum = maximum(myarr, arrSize);
std::cout << "maxNum = " << maxNum << " (" << *maxNum << ")" << std::endl;
return 0;
}
Live Demo
And then, you can throw it all away and use STL algorithms instead, like std::max_element():
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
const int arrSize = 5;
double myarr[arrSize];
std::cout << "Input " << arrSize << " floating point values for your array" << std::endl;
// loop to input values
std::copy_n(std::istream_iterator<double>(std::cin), arrSize, myarr);
for(int i = 0; i < arrSize; ++i) {
std::cout << "Location for " << myarr[i] << " = " << &myarr[i] << std::endl;
}
double *maxNum = std::max_element(myarr, myarr + arrSize);
std::cout << "maxNum = " << maxNum << " (" << *maxNum << ")" << std::endl;
return 0;
}
Live Demo

Igraph (C) returns bad vertex ids

I am trying to load a simple graph in the gml format (I can upload it if needed) and the compute its adjacency list and print it for each vertex.
Here is what I am doing:
int
main(int argc, char *argv[])
{
igraph_t g;
igraph_adjlist_t adjlist;
int num_edges, num_vertices;
FILE *ifile;
ifile = fopen("example.gml", "r");
igraph_read_graph_gml(&g, ifile);
fclose(ifile);
n_vertices = igraph_vcount(&g);
cout << "vertices: " << n_vertices << endl;
igraph_adjlist_init(&g, &adjlist, IGRAPH_OUT);
igraph_vector_t *vect_adj;
for (int n = 0; n < n_vertices; n++)
{
igraph_vs_t adc;
igraph_vs_adj(&adc, n, IGRAPH_OUT);
cout << "TYPE " << igraph_vs_type(&adc) << "\n";
vect_adj = (igraph_vector_t *)igraph_adjlist_get(&adjlist, n);
printf("\nvertex %d n adjs %ld\n", n, igraph_vector_size(vect_adj));
for (int f = 0; f < igraph_vector_size(vect_adj); f++)
{
cout << "node id " << (long int)igraph_vector_e(vect_adj, f) << endl;
long int neighbor = VECTOR(*vect_adj)[f];
cout << "nid " << neighbor << endl;
}
}
igraph_destroy(&g);
}
but what happens is that I am always getting 0 as an id as if there had been some type conversion issue. What am I doing wrong here?
I guess the point here is to use a typed vector like igraph_vector_int_t and typed functions as well, igraph_vector_int_size.

Something to change

as the title, i don't know how to convert this struct into a class?
Also i've another problem, how can i convert the array Sets[][] and top[] into vector?
I tried, but i have problem with editing in a vector of [i] position.
//*******************************
// Class Kruskal *
//*******************************
class kruskal
{
private:
struct Edge {
//Arco: vertice V -> vertice U : peso W
int v, u, w;
Edge(int v, int u, int w) : v(v), u(u), w(w) {}
bool operator < (const Edge& c) const{
if (w != c.w)
return w < c.w;
if (v != c.v)
return v < c.v;
return u < c.u;
}
};
int n; //n. nodes
int nre; //n. edges
vector<Edge> edges; //vector contenente tutti gli archiì
vector<Edge> tree; //Albero che conterrà tutti gli archi dell'MST
int sets[100][10]; //matrice contente i sets (tagli)
int top[100]; //supporto alla matrice dei sets
public:
kruskal(){};
~kruskal(){ cout << "Grafo distrutto"; };
void read_graph();
void sort_edges();
void algorithm();
int find_node(int);
void print_min_span_t();
};
//*******************************************
// read_graph() *
// Legge in input n, nre, e i vari archi *
//*******************************************
void kruskal::read_graph()
{
cout << "Algoritmo di Kruskal" << endl;
cout << "Minimum Spanning Tree su Grafo non orientato e pesato" << endl << endl;
cout << "-Inserire numero di nodi e numero di archi: ";
cin >> n >> nre;
int v, u, w;
cout << "-Inserire vertice 1, vertice 2 e peso:" << endl;
for (int i = 0; i < nre; i++)
{
cin >> v >> u >> w;
if (w != 0)
{
edges.push_back(Edge(v, u, w));
}
}
//Print graph edges
cout << endl << endl << "Archi del grafo:" << endl;
for (unsigned int i = 0; i < edges.size(); i++)
{
cout << " < " << edges[i].v
<< " , " << edges[i].u
<< " > " << edges[i].w << endl;
}
}
//*******************************************
// sort_edges() *
// Ordina gli archi per peso con sort() *
//*******************************************
void kruskal::sort_edges()
{
sort(edges.begin(), edges.end());
//Print graph edges
cout << endl << endl << "Archi del grafo dopo l'ordinamento:" << endl;
for (unsigned int i = 0; i < edges.size(); i++)
{
cout << " < " << edges[i].v
<< " , " << edges[i].u
<< " > " << edges[i].w << endl;
}
}
//***********************************************
// algorithm() *
// Inizializza i sets (make-set) *
// Trova i sets dei due nodi (Find_node) *
// Controlla se i sets sono diversi (Findset) *
// Se si lo inserisce nel vector "tree" (MST) *
// E unisce i due sets (Union) *
// Altrimenti "scarta" l'arco *
//***********************************************
void kruskal::algorithm()
{
//Make-set
for (int i = 1; i <= n; i++)
{
sets[i][1] = i;
top[i] = 1;
}
cout << endl << "Avvio algoritmo di Kruskal:" << endl << endl;
for (unsigned int i = 0; i < edges.size(); i++)
{
int p1 = find_node(edges[i].v);
int p2 = find_node(edges[i].u);
//Findset(p1) != Findset(p2)
if (p1 != p2)
{
cout << "Arco preso nell'albero:"
<< " < " << edges[i].v << " , "
<< edges[i].u << " > " << endl << endl;
//Union
tree.push_back(Edge(edges[i].v, edges[i].u, edges[i].w));
//Union two sets
for (int j = 1; j <= top[p2]; j++)
{
top[p1]++;
sets[p1][top[p1]] = sets[p2][j];
}
top[p2] = 0;
}
else
{
cout << "Questo arco"
<< " < " << edges[i].v << " , "
<< edges[i].u << " > " << "forma un ciclo ed e' stato rimosso" << endl << endl;
}
}
}
//*******************************************
// find_node() *
// Trova il sets di appartenenza del nodo *
//*******************************************
int kruskal::find_node(int n)
{
for (int i = 1; i <= nre; i++)
{
for (int j = 1; j <= top[i]; j++)
{
if (n == sets[i][j])
return i;
}
}
return -1;
}
//*******************************
// print_min_span_t() *
//*******************************
void kruskal::print_min_span_t()
{
cout << endl << "Minimum Spanning Tree del grafo:" << endl;
for (unsigned int i = 0; i < tree.size(); i++)
{
cout << " < " << tree[i].v
<< " , " << tree[i].u
<< " > " << tree[i].w << endl;
}
}
As top is just the size of corresponding sets, you may remove top, and change std::vector<std::vector<int> > sets.
Then
MakeSet:
//Make-set
sets.resize(nre); // or n, not sure of the definition of each one...
for (std::size_t i = 0; i != nre; ++i)
{
sets[i].push_back(i);
}
UnionSet:
//Union two sets
sets[p1].insert(sets[p1].end(), sets[p2].begin(), sets[p2].end());
sets[p2].clear();
Find node:
int kruskal::find_node(int n) const
{
for (size_t i = 0; i != sets.size(); ++i)
{
if (std::find(sets[i].begin(), sets[i].end(), n) != sets[i].end())
{
return i;
}
}
return -1;
}

C++ char vector addition

This is a part of a program that I am writing to compute the addition of two integers as strings. (Writing my own bigInt class).
There appears to be a problem when I am adding the two integers together. Because they are both in vectors of char type, I had to add a '0' to each element of the vector before concatenating it into a string.
However, the results are still not what I am expecting:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string const Number = "1000";
string const Number2 = "1000";
vector<char> reverse;
vector<char> reverse2;
//cout << (rostrNumber[1] - '0') << endl;
cout << "Original Number: " << Number << endl;
reverse.clear();
for (int i = Number.size() - 1; i >= 0; i--)
{
reverse.push_back(Number[i]);
}
cout << "Reversed: " << endl;
cout << reverse[0] << reverse[1] << reverse[2] << reverse[3] << endl;
cout << endl << endl;
reverse2.clear();
{
for (int i = Number2.size() - 1; i >= 0; i--)
{
reverse2.push_back(Number[i]);
}
}
cout << "Adding these two integers" << endl;
vector<char> const rcvN1 = reverse;
vector<char> const rcvN2 = reverse2;
vector<char> Results;
Results.clear();
//Local copies
vector<char> vN1 = rcvN1;
vector<char> vN2 = rcvN2;
int iSize1 = vN1.size();
int iSize2 = vN2.size();
int i, iSize = iSize2;
int iC = 0, iR;
for (i = 0; i<iSize; i++)
{
iR = vN1[i] + vN2[i] + iC;
if (iR > 9)
{
iR -= 10;
iC = 1;
}
else
iC = 0;
Results.push_back(iR);
cout << Results[0] << endl;
}
if (iC > 0)
Results.push_back(iC);
string ostr;
vector<char>::const_reverse_iterator rIter = Results.rbegin();
for (; rIter != Results.rend(); rIter++)
ostr += *rIter +'0';
cout << "Results: " << ostr << endl;
system("PAUSE");
return 0;
}