LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2009, 11:19 AM   #1
primenu
LQ Newbie
 
Registered: Oct 2009
Posts: 13
Blog Entries: 1

Rep: Reputation: 0
struct inside a struct


Hello, Can anyone help me..
I am trying to create a message passing structure in a grid .
I have a object switch_p inside which has two struct variables. When I update the elements of the struct from one method and try to take them from another method, g++ compiler complains it cannot read the address:
The Code goes:

typedef struct {
int Status; // -1 = Ideal,1 = Active
int OutputVC;
list<flit_pkt*> flit_buffer;
} Channel;

typedef struct flits {
int destin[2];
int data;
int hops;
int type; // Head =1, Body = 0, Tail = -1
flits *next;
//For the next trail flits in the packets.Tail->next = NULL
} flit_pkt;
I have defined a list of struct flits inside another struct Input_Channel, the compiler doesn't give errors for this syntax.

The two variables of switch class are :
Channel *Input_Channel[];
Channel *Output_Channel[];
//initially these variables are assigned status -1

update_Switch(){
/*This method basically does is takes message stored in the Input_Channel and puts into the output Channel */
......
for(int i=1; i < 10; i++){
if(Input_Channel[i]->Status == 1){

if(Input_Channel[i]->flit_buffer.front()->type == 1){
if(Channel_Selection(Input_Channel[i]->flit_buffer.front(), &VChannel)){
// Method Channel Selection is takes the message and assigns Output_Channel to the Input_Channel[i]
Input_Channel[i]->OutputVC = VChannel;
Input_Channel[i]->flit_buffer.pop_front();
cout<<"\n Header Flit Assigned VChannel:"<<VChannel;
}
}else if(Input_Channel[i]->flit_buffer.front()->type == 0){
if(Output_Channel[Input_Channel[i]->OutputVC]->flit_buffer.size() < flit_size){
Output_Channel[Input_Channel[i]->OutputVC]->flit_buffer.push_back(Input_Channel[i]->flit_buffer.front());
Input_Channel[i]->flit_buffer.pop_front();
}
}else if(Input_Channel[i]->flit_buffer.front()->type == -1){
if(Output_Channel[Input_Channel[i]->OutputVC]->flit_buffer.size() < flit_size){
Output_Channel[Input_Channel[i]->OutputVC]->flit_buffer.push_back(Input_Channel[i]->flit_buffer.front());
Input_Channel[i]->flit_buffer.pop_front();
Input_Channel[i]->Status == -1;
Input_Channel[i]->OutputVC = -1;
}
}



......
}
communicate_switch(){

/*This method takes the message stored in output_Channel and passes it to next object of same time*/
for(int i = 1; i < 10; i++){
if(Output_Channel[i]->Status == 1){
switch(i){
case 1:
case 2:
if(Output_Channel[i]->flit_buffer.front()->type == 1 && port_E->eject_message(Output_Channel[i]->flit_buffer.front(), &Output_Channel[i]->OutputVC, 5,6)){
Output_Channel[i]->Status = 1;
// Output_Channel[i]->OutputVC = VChannel;
Output_Channel[i]->flit_buffer.pop_front();
}
else if(Output_Channel[i]->flit_buffer.front()->type == 0 && port_E->eject_message(Output_Channel[i]->flit_buffer.front(), &Output_Channel[i]->OutputVC, 5,6)){
Output_Channel[i]->Status = 1;
Output_Channel[i]->flit_buffer.pop_front();
}
else if(Output_Channel[i]->flit_buffer.front()->type == -1&& port_E->eject_message(Output_Channel[i]->flit_buffer.front(), &Output_Channel[i]->OutputVC, 5,6)){
Output_Channel[i]->Status = -1;
Output_Channel[i]->flit_buffer.pop_front();
Output_Channel[i]->OutputVC = 0;
}
break;

}
The problem here is when I call Update_Switch and the communicate_switch metods simultaneously, the communicate_switch method takes the message stored in Input_Channel[i] and empties the buffer <flit_buffer>, though I am manipulating output_channel[i] from this last method,
and the while the turn of first method comes, since the status is not -1, it tries to read an empty buffer and error comes.This is what I understood, is there somethig else or I am missing something...I would really appreciate anyones help..
Thanks in advance
 
Old 11-25-2009, 11:51 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
To get any useful help, I think you will need to do a much better job of asking the question.

You have a vague description of a problem near the top of your post

Quote:
Originally Posted by primenu View Post
g++ compiler complains it cannot read the address:
and you have a vague but apparently contradictory description of the problem near the end of the post

Quote:
while the turn of first method comes, since the status is not -1, it tries to read an empty buffer and error comes.
The compiler is done with its job before the program starts to run. So I expect you didn't really mean the compiler in that first description. But even with that guess, I have no idea what problem you are trying to describe.

In between the two descriptions, you have a lot of code. The lack of code tags makes the whole post unreadable. Learn to use code tags if you want to post code. Edit your first post to add the code tags, so the long code lines don't continue to mess up the whole thread.

It is too much code to expect us to look through with such a confusing description. But it is too little code in the sense that it leaves out key parts without which the parts you posted can't be understood.

Finally, is this a multi threaded program?

Quote:
I call Update_Switch and the communicate_switch metods simultaneously,
What do you mean "simultaneously". Really simultaneously implies multi threaded. To have multiple threads manipulating a complicated linked structure like this ought to require significant use of some kind of lock or semaphore, but I don't see any in your code.

Last edited by johnsfine; 11-25-2009 at 11:53 AM.
 
Old 11-25-2009, 01:53 PM   #3
primenu
LQ Newbie
 
Registered: Oct 2009
Posts: 13

Original Poster
Blog Entries: 1

Rep: Reputation: 0
Ok, sorry about the confusion.
First thing is I got runtime errors, that is I have been trying to read an empty buffer, which actually I don't intend to. Input_Channel[] and Output_Channel[] are two properties of an Object, lets say switch here, of struct type Channel. I am using object of another fren class to put in the values into the list <flit_buffer> of the properties Input_Channel.
So, now the 'update' method of the switch class here tries to take the values in the list of Input_Channel[i], and put that into list of Output_Channel[j].
'Communicate' method takes in values stored list of the Output_Channel[i], and sends to another object.
The problem is, When I call the method communicate, instead of emptying list of Output_Channel[i], list of Input_Channel[i] is emptied. And in my code, I have nowhere manipulated the variables Input_Channel[] in the method communicate.
I didn't mean multi-threaded, its just the two methods are called one after another, until all the buffers are empty. I don't know If this makes things clearer. Putting my entire code would make it even messier.
 
Old 11-25-2009, 02:16 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by primenu View Post
The two variables of switch class are :
Channel *Input_Channel[];
Channel *Output_Channel[];
That needs some explanation.

You are declaring two arrays of unspecified size and each element of each array is a pointer to an object of type Channel.

Where are the actual arrays defined (since the size isn't specified, what you showed doesn't actually define either array)?

Where are all those pointers initialized to point to distinct objects?
 
Old 11-25-2009, 03:23 PM   #5
primenu
LQ Newbie
 
Registered: Oct 2009
Posts: 13

Original Poster
Blog Entries: 1

Rep: Reputation: 0
hi, Thanks for pointing out the problem. I guess I made a mistake by declaring the array pointers of undefined size. The problem is solved for now. I will obviously get back in case of other problems. The thing is while trying to explain my problem to you I managed to solve other problems in my code )
You have been real helpful
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Typecast struct sockaddr in struct sockaddr_in sudhansu Linux - Kernel 1 02-17-2009 10:33 AM
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 04:40 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration