Sth strange: public variable can be output in the constructor but not outside

Here's my main function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>
using namespace std;

#include "createnetwork.h"

int main()
{
	int N[1]={2};
	int arr1[3]={0,1,1};
	int arr2[3]={2,0,0};
	network network1(1,N,arr1,arr2);
	cout << network1.neighbor[0];
	cout << endl;
}


and here's the header file createnetwork.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
struct link
{
	int a;
	int b;
};

class network
{
public:
	int *cumul;
					
	int *ins;		
	int *outs;		
	struct link *link;
	int n;			
	int *conn;		
	int *N;			
	int *neighbor;	

	network(){n=0;}

	network(int,int[],int[],int[]);
};

network::network (int layer, int node[],int in[],int out[])
{
/* some complicated thing giving dynamic memories and assign values */
cout << "network created\n";
cout << neighbor[0] << " ";
}


As you can see, the variable neighbor[0] is being displayed in both the constructor and the main function.
There is no problem in compiling. However, when I try to run the program, the neighbor[0] is displayed and then get stuck (just like a runtime error) but what is left is just another cout in the main function. I guess nothing can get wrong?

When I tried debugging, the message box gives me this:
Unhandled exception at 0x5edad442 in class.exe: 0xC0000005: Access violation reading location 0xcccccccc

I searched on the Internet about the problem, some said it comes out when I am derefrencing a pointer that hasn't been initialized. But I can't understand why it can come out in the constructor with a good value(meaning it has been initialized) but not in the main function.
Last edited on
You haven't initialised neighbour. You need to actually set it to point to some memory that's allocated for storing the array.

EDIT: Unless it's in the code that you've edited out. You'll need to show us the whole of your constructor for us to know what's going on.
Last edited on
Perhaps I will have my problem restated:
All the public member (including 'neighbor') is working well in the constructor. I can access them freely.
But when thing go into the main function, 'neighbor' and only 'neighbor' cannot be accessed, others (like 'conn') have no problem.

I have made some change to let the problem more clear.
The main function is now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
using namespace std;

#include "createnetwork.h"

int main()
{
	int N[1]={2};
	int arr1[3]={0,1,1};
	int arr2[3]={2,0,0};
	network network1(1,N,arr1,arr2);
	cout << network1.n << endl;
	for (int i=0;i<network1.n;i++)
		cout << network1.conn[i] << " ";
	cout << endl;
	cout << network1.neighbor[0];
	cout << endl;
}


Here's some background infomation:
I am major in Physics and I am studying network problem. What I do here is to create a specific network. The nodes can be divided into layers and nodes within same layer form a ring-like structure and
node[i] gives the number of nodes in each layer.

Node 0 is the center of the network and all the 'rings'. Between adjacent layers, there are some links connecting them.
So in[i] represents the number of links going towards center of node i and out[i] is the no of links going out.
The meaning of each variable:
1
2
3
4
5
6
7
8
9
	int *cumul;		//cumul[n]: The number of nodes in the first n layers
					//n=cumul[layer]: Total no of nodes in the network
	int *ins;		//cumulative inwards links
	int *outs;		//cumulative outwards links
	struct link *link;
	int n;			//number of nodes
	int *conn;		//conn[i]: connectivity of node i
	int *N;			//N[i]: sum of connectivity of nodes with label<i
	int *neighbor;	//neighbor[N[i]+(j-1)]: the j-th neighbor of node i 

I hope the description may help understanding my code.

So here's the whole constructor:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
network::network (int layer, int node[],int in[],int out[])
{
	cumul=new int [layer+1];
	cumul[0]=1;
	for (int i=1;i<=layer;i++)
		cumul[i]=cumul[i-1]+node[i-1];
	n=cumul[layer];

	ins=new int [n];
	outs=new int [n];

	ins[0]=in[0];
	outs[0]=out[0];

	for (int i=1;i<n;i++)
		ins[i]=ins[i-1]+in[i];
	
	for (int i=1;i<n;i++)
		outs[i]=outs[i-1]+out[i];

	link = new struct link [ins[n-1]];
	int a=0;
	int b=0;
	int c=0;
	//see the excel file for the assignment of links
	for (int i=0;i<ins[n-1];i++)
	{
		while (outs[a]<=i)
			a++;
		link[i].a=a;
		if (i==ins[cumul[c]-1])
		{
			c++;
			link[i].b=cumul[c]-1;
		}
		else
		{
			while (ins[b]<i)
				b++;
			link[i].b=b;
		}
	}

	cout << "links found\n";

	conn = new int [n];
	conn[0]=0;
	for (int i=1;i<n;i++)
		conn[i]=2;
	for (int i=0;i<ins[n-1];i++)
	{
		conn[link[i].a]++;
		conn[link[i].b]++;
	}

	cout << "connectivity found\n";

	N=new int [n+1];
	N[0]=0;
	for (int i=1;i<=n;i++)
		N[i]=N[i-1]+conn[i-1];
	
	int *neighbor;
	neighbor = new int [N[n]];
	
	for (int i=0;i<N[n];i++)
		neighbor[i] = -1;

	c=0;
	for (int i=1;i<n;i++)
	{
		if (i==cumul[c])
			neighbor[N[i+1]-1]=cumul[++c]-1;
		else
			neighbor[N[i+1]-1]=i-1;
	}
	c=layer;
	for (int i=n-1;i>0;i--)
	{
		if (i==cumul[c]-1)
			neighbor[N[i+1]-2]=cumul[--c];
		else
			neighbor[N[i+1]-2]=i+1;
	}

	for (int i=0;i<ins[n-1];i++)
	{
		
		a=N[link[i].a];
		while (neighbor[a]!=-1)
			a++;
		neighbor[a]=link[i].b;

		b=N[link[i].b];
		while (neighbor[b]!=-1)
			b++;
		neighbor[b]=link[i].a;
	}
	cout << "network created\n";

	cout << n << endl;
	for (int i=0;i<n;i++)
		cout << conn[i] << " ";
	cout << endl;
	cout << neighbor[0] << endl << endl;
}


And the output window goes like this:

1
2
3
4
5
6
7
8
9
links found
connectivity found
network created
3                                                  //n in the constructor
2 3 3                                            //conn in the constructor
2                                                 //neighbor[0] in the constructor

3                                                 //network1.n in the main function
2 3 3                                           //network1.conn in the main function 


And then it gets stuck.

Everything is initialized in the constructor (able to return me the appropriate value)
But when things go to the main function, others still works well but not the neighbor array.

In fact, the whole thing will work well if I copy the whole thing to the main function, just as it works in the constructor. Indeed, I first write the code in the main function and then copy it to the constructor. I tested it with a number of parameters and it works. So I do not think that there is problem in the constructor. However, something got wrong now. Is that some underlying rules for defining class that I violate?
Last edited on
Or is there simpler way to do similar things?
In line 63 and 64, your are creating another instance of "neighbour", which is shadowing your "neighbour" variable in the struct. This means that it outputs the value of neighbour[0] in the constructor, and then that "neighbour" gets destroyed (out of scope). The "neighbour" you are getting in your main function is a completely different pointer, which is uninitialized. Try getting rid of line 63, that should fix your problem.
Thanks a lot,NT3! The problem is now fixed.
In line 63 of your constructor, you're declaring a local variable int *neighbor. This hides the data member of the same name. From then on, in your constructor, any change you are making to neighbor is being made to that local variable, not to the class data member.

At line 105 of the constructor, you're outputting the contents of the local variable, which you've stored values in, not the class member.

At line 17 of main.cpp, you're attempting to access the class member, but you haven't ever given the pointer a value.

EDIT: Damn - ninja'd by NT3!

Last edited on
Topic archived. No new replies allowed.