I am learning C++ and have lost quite some time trying to solve to understand the reason of the error i am getting.
When i run the code below i am getting an Exception thrown. It happens when the program ends, so i believe it's related to the Edge pointer:
#include <iostream>
#include <vector>
#include <map>
using namespace std;
struct Edge {
int src, dest;
};
class Graph {
public:
int V, E;
Edge *edge = new Edge[E * sizeof(Edge)];
Graph(int Ver, int Edg);
};
Graph::Graph(int Ver, int Edg) {
V = Ver;
E = Edg;
}
Graph* createGraph(int V, int E) {
Graph* graph = new Graph(V,E);
return graph;
}
int find(int* parents, int val) {
if (parents[val] == -1)
return val;
return find(parents, parents[val]);
}
void Union(int *parents, int x, int y) {
parents[x] = y;
}
int isCycle(Graph* graph) {
int* parents = new int[graph->V * sizeof(int)];
memset(parents, -1, graph->V * sizeof(int));
for (int i = 0; i < graph->E; i++) {
int x = find(parents, graph->edge[i].src);
int y = find(parents, graph->edge[i].dest);
if (x == y) {
return 1;
};
Union(parents, x, y);
}
return 0;
}
int main()
{
int V = 9, E = 8;
Graph* graph = createGraph(V, E);
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[6].src = 0;
graph->edge[6].dest = 6;
graph->edge[5].src = 0;
graph->edge[5].dest = 7;
graph->edge[1].src = 1;
graph->edge[1].dest = 2;
graph->edge[2].src = 3;
graph->edge[2].dest = 2;
graph->edge[3].src = 4;
graph->edge[3].dest = 3;
graph->edge[4].src = 4;
graph->edge[4].dest = 5;
graph->edge[7].src = 5;
graph->edge[7].dest = 7;
if (isCycle(graph))
cout << "graph contains cycle";
else
cout << "graph doesn't contain cycle";
return 0;
}
I started learning C++ only few months ago, can somebody help me to understand why I am getting that exception?
Edge *edge = new Edge[E * sizeof(Edge)];
Unless E is initialized, this multiplies an uninitalized variable by sizeof(Edge) (which is also wrong on its face value as well, but we'll get to it later). This is undefined behavior.
Graph::Graph(int Ver, int Edg) {
V = Ver;
E = Edg;
}
This isn't good enough. The default values of class members, if specified, are used to initialize them before the constructor's body starts running.
The proper way to do this is to use the constructor's initialization section:
Graph::Graph(int Ver, int Edg) : V{Ver}, E{Ver}
{
}
This initializes V and E first, so now:
Edge *edge = new Edge[E * sizeof(Edge)];
So here, E is now initialized, fixing this problem. But this is still slightly incorrect. It's clear, based on the rest of the code, that this should really be:
Edge *edge = new Edge[E];
In C++, when you wish to declare an array of, say, 4 integers, all you have to do is declare:
int n[4];
The compiler takes care of multiplying 4 by however many bytes it takes to hold an int. The same thing is true for the new statement. If your goal is to construct an array of #E Edges, that would be, unsurprisingly: new Edge[E]. This same mistake occurs several times in the shown code.
Related
I've declared the following function in C++
void setCellInfo (CELL_MESH* target, int Global_ID, int node0,vector<NODE_MESH *>* NodeStore, vector<CELL_MESH *>* CellStore) {
CellStore->push_back(target); //No Errors
target->Global_ID = Global_ID; //No Errors
if (node0 != 0) {
target->node[0] = NodeStore[vector<NODE_MESH *>::size_type(node0)]->ID; //ERROR 1
target->node_pointers[0] = NodeStore[vector<NODE_MESH *>::size_type(node0)]; //ERROR 2
}
}
ERROR1: Gives me a "No member named 'ID' in 'std::vector<NODE_MESH *>'" for the target->node[] attributions although its the entities from the pointers within the vector that have this ID member. Since I'm trying to get a specific entity in the vector using NodeStore[value], I would think it would work.
ERROR2: Gives me "Assigning to 'NODE_MESH *' from incompatible type 'vector<NODE_MESH *>'" for all the target->node_pointers attributions. This seems to be the same problem but with pointers directly (without the ID member).
the NodeStore and CellStore vectors a defined as follows outside the function
vector<NODE_MESH*> NodeStore;
vector<CELL_MESH*> CellStore;
I then try to use the function like this, with 'i' being the int Global_ID and 'nodes_x*y+x' being some integer.
CELL_MESH *newCell = new CELL_MESH;
setCellInfo (&newCell, i, nodes_x*y+x, &NodeStore, &CellStore);
I've tried many different alterations to pointers but can't get it work. Would you know how to ?
Here's a simplified complete version:
#include <vector>
using namespace std;
typedef struct NODE_MESH{
int ID;
}NODE_MESH;
typedef struct CELL_MESH{
int Global_ID;
NODE_MESH* node_pointers[4];
int node[4];
}CELL_MESH;
vector<NODE_MESH*> NodeStore;
vector<CELL_MESH*> CellStore;
double nodes_y = 5;
double nodes_x = 4;
int cells_y = 4;
int cells_x = 3;
void setCellInfo (CELL_MESH* target, int Global_ID, int node0,vector<NODE_MESH *>* NodeStore, vector<CELL_MESH *>* CellStore) {
CellStore->push_back(target); //No Errors
target->Global_ID = Global_ID; //No Errors
if (node0 != 0) {
target->node[0] = NodeStore[vector<NODE_MESH *>::size_type(node0)]->ID; //ERROR 1
target->node_pointers[0] = NodeStore[vector<NODE_MESH *>::size_type(node0)]; //ERROR 2
}
}
int main() {
int i = 0;
for (double y = 0; y < nodes_y; y++) {
for (double x = 0; x < nodes_x; x++) {
NODE_MESH *newNode = new NODE_MESH;
NodeStore.push_back(newNode);
newNode -> ID = i;
i++;
}
}
i = 0;
for (int y = 0; y < cells_y; y++) { //nodes_y since horizontal faces are aligned with nodes horizontaly (same y)
for (int x = 0; x < cells_x; x++) { //x coordinate for horizontal faces is in-between nodes so 0.5 with count for faces
CELL_MESH *newCell = new CELL_MESH;
setCellInfo (newCell, i, nodes_x*y+x, &NodeStore, &CellStore);
i++;
}
}
return 0;
}
Given a variable T* t, the syntax t[x] is equivalent to *(t+x), which is the cause of this confusion. Concretely, NodeStore[vector<NODE_MESH *>::size_type(node0)] is of type vector<NODE_MESH *>& instead of an element of the NodeStore as you expected.
Change your code to take variables by reference instead:
void setCellInfo (CELL_MESH* target, int Global_ID, int node0,vector<NODE_MESH *>& NodeStore, vector<CELL_MESH *>& CellStore) {
CellStore->push_back(target); //No Errors
target->Global_ID = Global_ID; //No Errors
if (node0 != 0) {
target->node[0] = NodeStore[node0]->ID;
target->node_pointers[0] = NodeStore[node0];
}
}
The call is then simply
setCellInfo (&newCell, i, nodes_x*y+x, NodeStore, CellStore);
Alternatively, you will need to dereference the pointer before indexing:
(*NodeStore)[node0]->ID
Final Solution:
void setCellInfo (CELL_MESH* target, int Global_ID, int node0,vector<NODE_MESH *>& NodeStore, vector<CELL_MESH *>& CellStore) {
CellStore.push_back(target);
target->Global_ID = Global_ID;
if (node0 != 0) {
target->node[0] = NodeStore[node0]->ID;
target->node_pointers[0] = NodeStore[node0];
}
}
With this when using it:
CELL_MESH *newCell = new CELL_MESH;
setCellInfo (newCell, i, nodes_x*y+x, NodeStore, CellStore);
So I've been trying to implement Kruskal's algorithm, first I want to make clear the question is not related to the implementation of the algorithm. I've created one graph.hpp file, one kruskalsAlgo.hpp and main.cpp as follows respectively:
#pragma once
struct Edge
{
int source;
int destination;
int weight;
};
struct Graph
{
int V;
int E;
Edge* edge;
};
Graph* create_graph(int V, int E)
{
Graph* graph = new Graph;
graph -> V = V;
graph -> E = E;
graph -> edge = new Edge[E];
return graph;
}
#include <stdlib.h>
#include <tuple>
#include "../Graph/Graph.hpp"
class Kruskals_Algo
{
private:
struct subset
{
int parent;
int rank;
};
void make_set(subset*, int);
int find_set(subset*, int);
void _union(subset*, int, int);
public:
Edge* kruskal(Graph*);
void print_kruskals_MST(Edge*, int);
};
void Kruskals_Algo::make_set(subset* subsets, int V)
{
subsets[V].parent = V;
subsets[V].rank = 0;
}
int Kruskals_Algo::find_set(subset* subsets, int V)
{
if(subsets[V].parent != V)
subsets[V].parent = find_set(subsets, subsets[V].parent);
return subsets[V].parent;
}
void Kruskals_Algo::_union(subset* subsets, int x, int y)
{
int xroot = find_set(subsets, x);
int yroot = find_set(subsets, y);
if(subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if(subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else
{
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
inline int myComp(const void* a, const void* b)
{
Edge* a1 = (Edge*)a;
Edge* b1 = (Edge*)b;
return a1 -> weight > b1 -> weight;
}
Edge* Kruskals_Algo::kruskal(Graph* graph)
{
int V = graph -> V;
Edge result[V];
Edge* result_ptr = result;
int e = 0;
int i = 0;
qsort(graph -> edge, graph -> E, sizeof(graph -> edge[0]), myComp);
subset* subsets = new subset[(V * sizeof(subset))];
for (int v = 0; v < V; ++v)
make_set(subsets, v);
while(e < V - 1 && i < graph -> E)
{
Edge next_edge = graph -> edge[i++];
int x = find_set(subsets, next_edge.source);
int y = find_set(subsets, next_edge.destination);
if (x != y)
{
result[e++] = next_edge;
_union(subsets, x, y);
}
}
//return std::make_tuple(res, e);
return result_ptr;
}
void Kruskals_Algo::print_kruskals_MST(Edge* r, int e)
{
int minimumCost = 0;
for(int i=0; i<e; ++i)
{
std::cout << r[i].source << " -- "
<< r[i].destination << " == "
<< r[i].weight << std::endl;
minimumCost = minimumCost + r[i].weight;
}
std::cout << "Minimum Cost Spanning Tree: " << minimumCost << std::endl;
}
#include <iostream>
#include "Graph/Graph.hpp"
#include "Kruskals_Algo/kruskalsAlgo.hpp"
//#include "Prims_Algo/primsAlgo.hpp"
using namespace std;
class GreedyAlgos
{
public:
void kruskals_mst();
//void prims_mst();
};
void GreedyAlgos::kruskals_mst()
{
Kruskals_Algo kr;
int V;
int E;
int source, destination, weight;
cout << "\nEnter the number of vertices: ";
cin >> V;
cout << "\nEnter the number of edges: ";
cin >> E;
Edge* res;
Graph* graph = create_graph(V, E);
for(int i=0; i<E; i++)
{
cout << "\nEnter source, destinstion and weight: ";
cin >> source >> destination >> weight;
graph -> edge[i].source = source;
graph -> edge[i].destination = destination;
graph -> edge[i].weight = weight;
}
//std::tie(result, E) = kr.kruskal(graph);
res = kr.kruskal(graph);
kr.print_kruskals_MST(res, E);
}
int main()
{
int choice;
GreedyAlgos greedy;
greedy.kruskals_mst();
return 0;
}
So my question here is when I debug the program the values in Edge result[V], which is a structure array, are calculated correctly, at position [0] [1] [2] as in the following picture:
but when the function print_kruskals_MST(res, E) is called from the main the values printed are different:
Is there any pointer thing that I'm doing wrong?
Thanks in advance!
P.S. Ignore the comments!
This answer might not answer your question directly but it should shed some light on the problem.
First of all, yes you have a lot of pointer problems...
Secondly, pair ANY use of the new operator with the delete operator. As it stands, you have a bunch of memory leaks.
Also, why create_graph? Create a constructor for Graph instead (and a destructor since the class has an Edge* edge it needs to take care of).
struct Graph
{
int V;
int E;
Edge* edge;
// constructor
Graph(int V, int E)
{
this->V = V;
this->E = E;
this->edge = new Edge[E];
}
// destructor
~Graph()
{
// nullify the member variable before deleting its memory is just a safety measure pertaining to multithreading.
Edge* _edge = this->edge;
this->edge = nullptr;
delete _edge;
}
};
Then change Graph* graph = create_graph(V, E); into Graph* graph = new Graph(V, E); and do delete graph when you're done using it.
Make sure you remove all memory leaks and we can go on to discussing referencing the correct data (f.ex. by me changing my answer).
I'm trying to create a vector of a class-name vertex. The value of "n" is not known at compile-time so I'll be using new to create to create the "path" array. But the problem occurs when I create the input array in a function and push it in the vector.
int n;
class vertex {
public:
int *path;
int visited = 0;
vertex(int *y) {
path = new int(n);
for (int i = 0; i < n; i++)
path[i] = y[i];
}
};
void inp(vector<vertex> graph) {
int t1[] = { 0,1,0,0 };
int t2[] = { 0,0,1,0 };
int t3[] = { 0,0,0,1 };
int t4[] = { 0,0,0,0 };
graph.push_back(vertex(t1));
graph.push_back(vertex(t2));
graph.push_back(vertex(t3));
graph.push_back(vertex(t4));
}
int main() {
n=4;
vector<vertex> graph;
inp(graph);
_getch();
}
For simplicity I've created t1 to t4 as static arrays. But still it shows some error at runtime
1:try use: path = new int [n], rather than path = new int(n);
2:if you want to push elements to graph, you should change your function inp to void inp(vector<vertex>& graph)
This is a follow-up question to
mem_fn to function of member object
This is the current code.
#include <vector>
#include <algorithm>
#include <functional>
struct Int
{
Int(int _x = 0) : x(_x) {}
int GetInt() const { return x; }
int x;
};
struct IntWrapper
{
IntWrapper(int _x = 0) : test(_x) {}
int GetWrappedInt() const { return test.GetInt(); }
Int test;
};
template<class ContainerT, class Mem> constexpr auto maxElem(const ContainerT& _container, Mem _Pm)
{
auto memFn = std::mem_fn(_Pm);
return memFn(std::max_element(_container.cbegin(), _container.cend(), [&](auto _rhs, auto _lhs) { return memFn(_rhs) < memFn(_lhs); }));
}
int main()
{
{
std::vector<Int> vec;
for (int i = 0; i < 10; ++i)
{
vec.push_back(i * 11 % 7); // some random values
}
int m = maxElem(vec, &Int::GetInt);
int n = maxElem(vec, &Int::x);
}
{
std::vector<IntWrapper> vec;
for (int i = 0; i < 10; ++i)
{
vec.push_back(i * 7 % 11); // some random values
}
int m = maxElem(vec, &IntWrapper::GetWrappedInt);
//int o = maxElem(vec, ???) // what if GetWrappedInt didn't exist?
}
return 0;
}
The original question was about retrieving the x value of the Int struct through anIntWrapper object. I used mem_fn for this because it doesn't seem to distinguish between a function returning an int and an int member variable (Seen in these lines:
int m = maxElem(vec, &Int::GetInt);
int n = maxElem(vec, &Int::x);
The solution for IntWrapper objects was to add .test
auto y = std::mem_fn(&Int::GetInt);
auto b = y(wrapper.test);
to the call. However, in the maxElem function, I cannot do this.
I'm wondering if there is a way to formulate the call in such a way that the mem_fn goes from the IntWrapper object directly to the int x variable (Without the helper function and assuming that all members are public).
//int o = maxElem(vec, ???) // what if GetWrappedInt didn't exist?
The original approach was auto y = std::mem_fn(&IntWrapper::test.GetInt); // ERROR, which of course does not compile, but shows the idea.
Thanks in advance!
You cannot use std::mem_fn with something different than a pointer to member (such as a pointer to member of member). So, you must use that. In your particular case, you can achieve that with
std::vector<IntWrapper> vec;
for (int i = 0; i < 10; ++i)
{
vec.push_back(i * 11 % 7); // some random values
}
auto m = maxElem(vec, &IntWrapper::GetWrappedInt);
However, I strongly advise you to use lambda expressions whenever possible. std::mem_fn should be considered as if deprecated, since, AFAIK, it serves no purpose that cannot be achieved at least as well by other means, i.e. a lambda.
So strange situation, I am creating a list of structs, and then I am trying to update one of the list members with new values, and then move it back into the list.
I seem to be able to copy the values of the struct at iterator just fine, but when I attempt to update the value of the struct's member (using int++;) it throws an exception in the vector class of all things.
Any kind of explanation as to what might be happening here would be helpful.
struct Blob
{
int x;
int y;
};
list<Blob> blob;
// Add a Blob to blob using .push_back(); here
for(list<Blob>::iterator iterator=blob.begin(); iterator!=blob.end(); ++iterator)
{
Blob temp;
temp.x = ((Blob)*iterator).x;
temp.y = ((Blob)*iterator).y;
if (temp.x < 10 - 1)
temp.x++; /* Exception: vector: line 932 - "Vector subscript out of range" */
((Rain)*iterator) = temp;
}
When you want to update the existing value of object then take a reference of it. I have written a sample code to explain the same
#include<list>
#include<iostream>
using namespace std;
struct Test
{
int x;
int y;
};
int main()
{
list<Test> lTest;
int i = 0;
for(i=0;i<5;i++)
{
Test t1;
t1.x = i;
t1.y = i*i;
lTest.push_back(t1);
}
list<Test>::iterator lIter = lTest.begin();
for(;lIter != lTest.end();++lIter)
{
Test &t1 = *lIter;
cout<<"1 Val is:"<<t1.x<<"|"<<t1.y<<endl;
t1.x += 2;
t1.y += 2;
cout<<"2 Val is:"<<t1.x<<"|"<<t1.y<<endl;
}
lIter = lTest.begin();
for(;lIter != lTest.end();++lIter)
{
Test t1 = *lIter;
cout<<"3 Val is:"<<t1.x<<"|"<<t1.y<<endl;
}
return 0;
}
If you're writing a loop it's likely there's another way to do it. You can use std::for_each:
#include <list>
#include <algorithm>
struct Blob
{
int x;
int y;
};
void incrementXIfLessThanNine(Blob& blob)
{
if(blob.x < 9)
{
blob.x++;
}
}
int main()
{
std::list<Blob> blobs;
std::for_each(blob.begin(), blob.end(), incrementXIfLessThanNine);
return 0;
}
If you're using C++11:
#include <list>
struct Blob
{
int x;
int y;
};
int main()
{
std::list<Blob> blobs;
for(Blob& blob: blobs)
{
if(blob.x < 9)
{
blob.x++;
}
}
return 0;
}