Header file about 'directed graphs' gives a Segmentation FaultBest algorithm for detecting cycles in a directed graphWhy can templates only be implemented in the header file?What is a segmentation fault?Segmentation fault while creating an adjacency listC++ Vector of classes push back errorC++ map<int, vector <int> > segmentation faultSegmentation fault (core dumped) while declaring vectorsSegmentation fault when storing lambda as std::functionSegmentation fault error?c++ segmentation fault trying to access vector

Why does string strummed with finger sound different from the one strummed with pick?

Is a reptile with diamond scales possible?

Gambler's Fallacy Dice

How to fix "webpack Dev Server Invalid Options" in Vuejs

How can sister protect herself from impulse purchases with a credit card?

pwaS eht tirsf dna tasl setterl fo hace dorw

Why does Taylor’s series “work”?

Should I twist DC power and ground wires from a power supply?

Hotel booking: Why is Agoda much cheaper than booking.com?

Working hours and productivity expectations for game artists and programmers

Better than Rembrandt

How can I stop my kitten from growing?

Cycling to work - 30 mile return

Reference for electronegativities of different metal oxidation states

How can I prevent Bash expansion from passing files starting with "-" as argument?

How does the "reverse syntax" in Middle English work?

Can I have a delimited macro with a literal # in the parameter text?

Addressing an email

DISTINCT NULL return single NULL in SQL Server

How to choose the correct exposure for flower photography?

Character had a different name in the past. Which name should I use in a flashback?

Does the Aboleth have expertise in history and perception?

In How Many Ways Can We Partition a Set Into Smaller Subsets So The Sum of the Numbers In Each Subset Is Equal?

How do you play the middle D and F in this passage?



Header file about 'directed graphs' gives a Segmentation Fault


Best algorithm for detecting cycles in a directed graphWhy can templates only be implemented in the header file?What is a segmentation fault?Segmentation fault while creating an adjacency listC++ Vector of classes push back errorC++ map<int, vector <int> > segmentation faultSegmentation fault (core dumped) while declaring vectorsSegmentation fault when storing lambda as std::functionSegmentation fault error?c++ segmentation fault trying to access vector






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I use Code::Blocks and am trying to implement a header file that has 'graph' related methods like creating an adjacency list and printing them. But unfortunately, it gives a segmentation fault.



I use 'Vector of vectors' for my adjacency lists. Now weirdly Code::Blocks doesn't give any warnings or errors when I compile my code(nor does it work) but another compiler gives a segmentation fault.



(P.S. I know I should only prototype methods in my header file and use a separate source file to write them out but it's not working for some reason. Please bear with me)



Meat of my header file: (omitted the includes and namespace to be concise)



struct Edge
int src,dest;
;

class Graph
public:
vector<vector<int>> adjList;
int V,E;
Graph(int,int);
void dirGraph(vector<Edge> edges);
void printGraph();
;

Graph::Graph(int V,int E)

this->V = V;
this->E = E;


void Graph::dirGraph(vector<Edge> edges)

adjList.resize(V);
for(auto edge:edges)
adjList[edge.src].push_back(edge.dest);



void Graph::printGraph()
for(int i=0 ; i<V ; ++i)
cout << i;
for(auto x:adjList[i])
cout << " -> " << x;

cout << endl;





Main File:



#include <iostream>
#include <vector>
#include "Graph.h"

using namespace std;

int main()
vector<Edge> edges =

0,1,1,2,2,0,2,1,3,2,4,5,5,4
;
Graph g1(5,7);
g1.dirGraph(edges);
g1.printGraph();
return 0;



It should print the directed graph but it does nothing. (Code::Blocks gives 0 warnings and 0 errors. A separate online compiler gave the seg fault. But the code doesn't work)



Edit: Someone asked for the includes, here they are:



#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED
#include <vector>
#include <iostream>
using namespace std;

//my header file code

#endif









share|improve this question
























  • What did you observe when running your code in debug mode and stepping through line by line? Where exactly doe the exception occur?

    – πάντα ῥεῖ
    Mar 23 at 18:32






  • 1





    adjList is too small.

    – molbdnilo
    Mar 23 at 18:33











  • Concise is not helpful when it means that anyone that wants to run your code has to imagine the parts you left out. Please make a Minimal, Complete, and Verifiable example.

    – Retired Ninja
    Mar 23 at 18:34











  • but another compiler gives a segmentation fault -- Do this instead: adjList.at(edge.src).push_back(edge.dest); -- Now do you get a std::out_of_range exception thrown, regardless of the compiler you're using?

    – PaulMcKenzie
    Mar 23 at 18:39












  • Yes! code::blocks terminates the program after the out of range exception is thrown.

    – Krutik Desai
    Mar 23 at 18:45

















0















I use Code::Blocks and am trying to implement a header file that has 'graph' related methods like creating an adjacency list and printing them. But unfortunately, it gives a segmentation fault.



I use 'Vector of vectors' for my adjacency lists. Now weirdly Code::Blocks doesn't give any warnings or errors when I compile my code(nor does it work) but another compiler gives a segmentation fault.



(P.S. I know I should only prototype methods in my header file and use a separate source file to write them out but it's not working for some reason. Please bear with me)



Meat of my header file: (omitted the includes and namespace to be concise)



struct Edge
int src,dest;
;

class Graph
public:
vector<vector<int>> adjList;
int V,E;
Graph(int,int);
void dirGraph(vector<Edge> edges);
void printGraph();
;

Graph::Graph(int V,int E)

this->V = V;
this->E = E;


void Graph::dirGraph(vector<Edge> edges)

adjList.resize(V);
for(auto edge:edges)
adjList[edge.src].push_back(edge.dest);



void Graph::printGraph()
for(int i=0 ; i<V ; ++i)
cout << i;
for(auto x:adjList[i])
cout << " -> " << x;

cout << endl;





Main File:



#include <iostream>
#include <vector>
#include "Graph.h"

using namespace std;

int main()
vector<Edge> edges =

0,1,1,2,2,0,2,1,3,2,4,5,5,4
;
Graph g1(5,7);
g1.dirGraph(edges);
g1.printGraph();
return 0;



It should print the directed graph but it does nothing. (Code::Blocks gives 0 warnings and 0 errors. A separate online compiler gave the seg fault. But the code doesn't work)



Edit: Someone asked for the includes, here they are:



#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED
#include <vector>
#include <iostream>
using namespace std;

//my header file code

#endif









share|improve this question
























  • What did you observe when running your code in debug mode and stepping through line by line? Where exactly doe the exception occur?

    – πάντα ῥεῖ
    Mar 23 at 18:32






  • 1





    adjList is too small.

    – molbdnilo
    Mar 23 at 18:33











  • Concise is not helpful when it means that anyone that wants to run your code has to imagine the parts you left out. Please make a Minimal, Complete, and Verifiable example.

    – Retired Ninja
    Mar 23 at 18:34











  • but another compiler gives a segmentation fault -- Do this instead: adjList.at(edge.src).push_back(edge.dest); -- Now do you get a std::out_of_range exception thrown, regardless of the compiler you're using?

    – PaulMcKenzie
    Mar 23 at 18:39












  • Yes! code::blocks terminates the program after the out of range exception is thrown.

    – Krutik Desai
    Mar 23 at 18:45













0












0








0








I use Code::Blocks and am trying to implement a header file that has 'graph' related methods like creating an adjacency list and printing them. But unfortunately, it gives a segmentation fault.



I use 'Vector of vectors' for my adjacency lists. Now weirdly Code::Blocks doesn't give any warnings or errors when I compile my code(nor does it work) but another compiler gives a segmentation fault.



(P.S. I know I should only prototype methods in my header file and use a separate source file to write them out but it's not working for some reason. Please bear with me)



Meat of my header file: (omitted the includes and namespace to be concise)



struct Edge
int src,dest;
;

class Graph
public:
vector<vector<int>> adjList;
int V,E;
Graph(int,int);
void dirGraph(vector<Edge> edges);
void printGraph();
;

Graph::Graph(int V,int E)

this->V = V;
this->E = E;


void Graph::dirGraph(vector<Edge> edges)

adjList.resize(V);
for(auto edge:edges)
adjList[edge.src].push_back(edge.dest);



void Graph::printGraph()
for(int i=0 ; i<V ; ++i)
cout << i;
for(auto x:adjList[i])
cout << " -> " << x;

cout << endl;





Main File:



#include <iostream>
#include <vector>
#include "Graph.h"

using namespace std;

int main()
vector<Edge> edges =

0,1,1,2,2,0,2,1,3,2,4,5,5,4
;
Graph g1(5,7);
g1.dirGraph(edges);
g1.printGraph();
return 0;



It should print the directed graph but it does nothing. (Code::Blocks gives 0 warnings and 0 errors. A separate online compiler gave the seg fault. But the code doesn't work)



Edit: Someone asked for the includes, here they are:



#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED
#include <vector>
#include <iostream>
using namespace std;

//my header file code

#endif









share|improve this question
















I use Code::Blocks and am trying to implement a header file that has 'graph' related methods like creating an adjacency list and printing them. But unfortunately, it gives a segmentation fault.



I use 'Vector of vectors' for my adjacency lists. Now weirdly Code::Blocks doesn't give any warnings or errors when I compile my code(nor does it work) but another compiler gives a segmentation fault.



(P.S. I know I should only prototype methods in my header file and use a separate source file to write them out but it's not working for some reason. Please bear with me)



Meat of my header file: (omitted the includes and namespace to be concise)



struct Edge
int src,dest;
;

class Graph
public:
vector<vector<int>> adjList;
int V,E;
Graph(int,int);
void dirGraph(vector<Edge> edges);
void printGraph();
;

Graph::Graph(int V,int E)

this->V = V;
this->E = E;


void Graph::dirGraph(vector<Edge> edges)

adjList.resize(V);
for(auto edge:edges)
adjList[edge.src].push_back(edge.dest);



void Graph::printGraph()
for(int i=0 ; i<V ; ++i)
cout << i;
for(auto x:adjList[i])
cout << " -> " << x;

cout << endl;





Main File:



#include <iostream>
#include <vector>
#include "Graph.h"

using namespace std;

int main()
vector<Edge> edges =

0,1,1,2,2,0,2,1,3,2,4,5,5,4
;
Graph g1(5,7);
g1.dirGraph(edges);
g1.printGraph();
return 0;



It should print the directed graph but it does nothing. (Code::Blocks gives 0 warnings and 0 errors. A separate online compiler gave the seg fault. But the code doesn't work)



Edit: Someone asked for the includes, here they are:



#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED
#include <vector>
#include <iostream>
using namespace std;

//my header file code

#endif






c++ stl codeblocks directed-graph






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 18:43







Krutik Desai

















asked Mar 23 at 18:29









Krutik DesaiKrutik Desai

84




84












  • What did you observe when running your code in debug mode and stepping through line by line? Where exactly doe the exception occur?

    – πάντα ῥεῖ
    Mar 23 at 18:32






  • 1





    adjList is too small.

    – molbdnilo
    Mar 23 at 18:33











  • Concise is not helpful when it means that anyone that wants to run your code has to imagine the parts you left out. Please make a Minimal, Complete, and Verifiable example.

    – Retired Ninja
    Mar 23 at 18:34











  • but another compiler gives a segmentation fault -- Do this instead: adjList.at(edge.src).push_back(edge.dest); -- Now do you get a std::out_of_range exception thrown, regardless of the compiler you're using?

    – PaulMcKenzie
    Mar 23 at 18:39












  • Yes! code::blocks terminates the program after the out of range exception is thrown.

    – Krutik Desai
    Mar 23 at 18:45

















  • What did you observe when running your code in debug mode and stepping through line by line? Where exactly doe the exception occur?

    – πάντα ῥεῖ
    Mar 23 at 18:32






  • 1





    adjList is too small.

    – molbdnilo
    Mar 23 at 18:33











  • Concise is not helpful when it means that anyone that wants to run your code has to imagine the parts you left out. Please make a Minimal, Complete, and Verifiable example.

    – Retired Ninja
    Mar 23 at 18:34











  • but another compiler gives a segmentation fault -- Do this instead: adjList.at(edge.src).push_back(edge.dest); -- Now do you get a std::out_of_range exception thrown, regardless of the compiler you're using?

    – PaulMcKenzie
    Mar 23 at 18:39












  • Yes! code::blocks terminates the program after the out of range exception is thrown.

    – Krutik Desai
    Mar 23 at 18:45
















What did you observe when running your code in debug mode and stepping through line by line? Where exactly doe the exception occur?

– πάντα ῥεῖ
Mar 23 at 18:32





What did you observe when running your code in debug mode and stepping through line by line? Where exactly doe the exception occur?

– πάντα ῥεῖ
Mar 23 at 18:32




1




1





adjList is too small.

– molbdnilo
Mar 23 at 18:33





adjList is too small.

– molbdnilo
Mar 23 at 18:33













Concise is not helpful when it means that anyone that wants to run your code has to imagine the parts you left out. Please make a Minimal, Complete, and Verifiable example.

– Retired Ninja
Mar 23 at 18:34





Concise is not helpful when it means that anyone that wants to run your code has to imagine the parts you left out. Please make a Minimal, Complete, and Verifiable example.

– Retired Ninja
Mar 23 at 18:34













but another compiler gives a segmentation fault -- Do this instead: adjList.at(edge.src).push_back(edge.dest); -- Now do you get a std::out_of_range exception thrown, regardless of the compiler you're using?

– PaulMcKenzie
Mar 23 at 18:39






but another compiler gives a segmentation fault -- Do this instead: adjList.at(edge.src).push_back(edge.dest); -- Now do you get a std::out_of_range exception thrown, regardless of the compiler you're using?

– PaulMcKenzie
Mar 23 at 18:39














Yes! code::blocks terminates the program after the out of range exception is thrown.

– Krutik Desai
Mar 23 at 18:45





Yes! code::blocks terminates the program after the out of range exception is thrown.

– Krutik Desai
Mar 23 at 18:45












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317071%2fheader-file-about-directed-graphs-gives-a-segmentation-fault%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317071%2fheader-file-about-directed-graphs-gives-a-segmentation-fault%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript