Vector problem

Hey everyone.
I am for the first time trying to use Vector (I assume thats the prefered dynamic array in c++).

I have done the following in my header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14


using namespace std;
#include <string>
#include <vector>

class LocalVersionNode {
public:
...
private:
..
	vector<EdgeOverlayNode*> versionEdge;
};


I dont get any errors when building but in the console there is written:

 
malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed. 


Any know why?
How are you using the vector in your program?
I have currently not used the vector, but have only initialised it.
If i remove:

vector<EdgeOverlayNode*> versionEdge;

it disappear.

#ne555
I don't exactly understand why you refer to that link. Should I have specified the message in a other way?
ne555 posted that link because you have not posted enough code to be useful.
i.e. malloc is encountering a run time error, yet the code you posted shows no call to malloc.


I have currently not used the vector, but have only initialised it.
 
vector<EdgeOverlayNode*> versionEdge;

That's not an initialization of the vector, only a declaration of a vector called versionEdge.

In order to cause malloc to fail a run time assertion, you've probably trashed the heap.

Okay sorry for not giving more code. This is my entire header.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

/*
 * EdgeVersionCommon.h
 *
 *  Created on: Oct 20, 2012
 *      Author: theis
 */

#ifndef EDGEVERSIONCOMMON_H_
#define EDGEVERSIONCOMMON_H_
using namespace std;
#include <string>
#include <vector>

class LocalVersionNode;



class EdgeOverlayNode {
public:
	EdgeOverlayNode();
private:
	LocalVersionNode *edgeVersionPointer;
	LocalVersionNode *edgeVersionBackPointer;
};




class LocalVersionNode {
public:
	LocalVersionNode(){};
	LocalVersionNode(int versionId, LocalVersionNode *a);
	LocalVersionNode* getAncestor();
	void setChilds(LocalVersionNode *c);
	LocalVersionNode* getChilds();
	void addEdge(EdgeOverlayNode *e);
	int getId();
	string getData();

private:
	int id;
	string data;
	LocalVersionNode* ancestor; //Containing 1 or 2 ancestors.
	LocalVersionNode* childs[2]; //Containing 1 or 2 children.
	vector<EdgeOverlayNode*> versionEdge;
	vector<EdgeOverlayNode*> inverseEdgeVersion;
};


#endif /* EDGEVERSIONCOMMON_H_ */




#AbstractionAnon
How have I trashed the heap, and how do I fix that?
You get the assertion error when you run the program, right? It's very hard to say what is causing this when we don't know what code is executing.
You're still not showing any executable code including your malloc call. However, the damage done to the heap is done at some point BEFORE the malloc call.

The most common cause of trashing the heap is to allocate an array or object, then write past the end of the array or object, typically because a subscript is out of range.
Hey Everyone.
Well I am nt executing the vector yet.


Here is my main-function, the header from above, and the class which i gonna use it in:

Main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

/**
 * This is only for testing
 * Uncomment after testing
 */

int main (int argc, char **argv) {

	dag* globalDag = new dag();




	//*****************************************
	//* TEST OF DAGNODE
	//*****************************************

	fprintf(stderr, "\n\n******************************* \n");
	fprintf(stderr, "Test in dagnode\n");
	dagnode rootAncestors[] = {}; //Both null -> root
	dagnode* root = new dagnode(1, rootAncestors);
	fprintf(stderr, "root got id: %d \n", root->getId());


	dagnode n1Ancestors[] = {*root};
	dagnode* n1 = new dagnode(2, n1Ancestors);
	fprintf(stderr, "n1 got id: %d\n", n1->getId());



	//**************************************
	//* TEST OF DAG CLASS
	//**************************************

	globalDag->makeVersion({root}, 1);
	dagnode *c1 = &root->getChilds()[0];
	fprintf(stderr, "Roots child is %d\n", c1->getId());

	globalDag->makeVersion({root}, 1);
	dagnode *ca1 = &root->getChilds()[0];
	dagnode *ca2 = &root->getChilds()[1];
	fprintf(stderr, "Roots child is %d and %d\n", ca1->getId(), ca2->getId());

	dagnode *search = globalDag->getVersionNode(1);
	fprintf(stderr, "Search result: %d", search->getId());



	//***************************************
	//* TESTING LOCAL VERSION TREE
	//***************************************
	fprintf(stderr, "\n\n*************************************\n");
	fprintf(stderr, "TEST IN LocalVersionNode\n");
	LocalVersionNode *lvn = new LocalVersionNode(1,{});
	fprintf(stderr, "Done\n\n");

	fprintf(stderr, "TEST IN LocalVersionTree\n");
	LocalVersionTree *lvt = new LocalVersionTree(1);
	fprintf(stderr, "Done\n");
}



EdgeVersinCommon.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

/*
 * EdgeVersionCommon.h
 *
 *  Created on: Oct 20, 2012
 *      Author: theis
 */

#ifndef EDGEVERSIONCOMMON_H_
#define EDGEVERSIONCOMMON_H_
using namespace std;
#include <string>
#include <vector>

class LocalVersionNode;



class EdgeOverlayNode {
public:
	EdgeOverlayNode();
private:
	LocalVersionNode *edgeVersionPointer;
	LocalVersionNode *edgeVersionBackPointer;
};




class LocalVersionNode {
public:
	LocalVersionNode(){};
	LocalVersionNode(int versionId, LocalVersionNode *a);
	LocalVersionNode* getAncestor();
	void setChilds(LocalVersionNode *c);
	LocalVersionNode* getChilds();

	int getId();
	string getData();

private:
	int id;
	string data;
	LocalVersionNode* ancestor; //Containing 1 or 2 ancestors.
	LocalVersionNode* childs[2]; //Containing 1 or 2 children.
	vector<EdgeOverlayNode*> versionEdge;
	//vector<EdgeOverlayNode> inverseEdgeVersion;
};


#endif /* EDGEVERSIONCOMMON_H_ */






LocalVersionTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

/*
 * LocalVersionTree.cpp
 *
 *  Created on: Oct 17, 2012
 *      Author: theis
 */

#include "LocalVersionTree.h"
#include "LocalVersionNode.h";

LocalVersionTree::LocalVersionTree(int versionId) {
	makeNode(versionId, {});
}

void LocalVersionTree::makeNode(int versionId, LocalVersionNode *a){
	LocalVersionNode *lvn = new LocalVersionNode(versionId, a); //Noget galt
	if(a != 0){
		//Have a ancestor
		fprintf(stderr, "The node have a ancestor\n");
		if(a->getChilds() == 0){
			//No child exist earlier
			a->setChilds({lvn});
			fprintf(stderr, "Added first child\n");
		} else {
			LocalVersionNode child1 = a->getChilds()[0];
			LocalVersionNode newChildList[] = {child1,*lvn};
			a->setChilds(newChildList);
			fprintf(stderr, "Added second child\n");
		}
	} else {
		fprintf(stderr, "The node is a root, and have no ancestors\n");
	}
}
1
2
	dagnode rootAncestors[] = {}; //Both null -> root
	dagnode* root = new dagnode(1, rootAncestors);


Again, not enough relevant code to know what's going on. Here, rootAncestors is an array of size 0. What is the dagnode constructor doing with a 0-sized array? This is a rhetorical question. Check it out yourself. Don't post the code.

When you have a problem of this sort, reduce it to a minimal, compilable example that still exhibits the problem, and post that.
> Here, rootAncestors is an array of size 0.
¿Is that legal?

dag* globalDag = new dag(); is ugly, error-prone and inefficient.
It's not legal C++, but some compilers allow it as an extension.
Topic archived. No new replies allowed.