1

can someone explain me the compilation error in this code:

#include "common.h"

typedef struct nodeData {
    int procid;
    unsigned short localport;
    DWORD LIFETIME;
    DWORD HELLOTIMEOUT;
    DWORD MAXTIME;
} nodeData;

int listenerThread() {
    if(!bindSocket(listenSocket,nodeData.localport)){
        closesocket(listenSocket);
        WSACleanup();
        exit(-1);
}
    // more code goes here  
}

int main(int argc,char* argv[]) {
    nodeData.localport = 5001;
    // more code goes here  

}

I want the nodeData struct to be available to every listenerThread I will create. threads are going to manipulate this nodeData struct all the time (will protect it with a mutex).

so i want this struct to be availabe globally. where do i initialize it ? my guess is in main.

the compilation error in the line

nodeData.localport = 5001;

is

error: a nonstatic member reference must be relative to a specific object

what am i'm missing here ?

thanks !

Michael
  • 22,196
  • 33
  • 132
  • 187

2 Answers2

6

nodeData is a type not a variable - since you typedef it. Try e.g:

typedef struct nodeData_t {
    int procid;
    unsigned short localport;
    DWORD LIFETIME;
    DWORD HELLOTIMEOUT;
    DWORD MAXTIME;
} nodeData;

nodeData MyNodeData;

And then use the variable MyNodeData

Erik
  • 88,732
  • 13
  • 198
  • 189
  • I *swear* I didn't lift your answer :) ... we just apparently use the same camel case. – Brian Roach Apr 17 '11 at 17:51
  • thanks a lot for the quick reply. forgive me for the ignorance, but it always confuses me, isn't writing the nodeData just after defining the struct already creating an instance of it ? – Michael Apr 17 '11 at 17:55
  • @michael: No, not when you have `typedef` in front. `struct nodeData {} MyNodeData;` would create an instance – Erik Apr 17 '11 at 17:59
  • @Erik, i see thanks. and what is nodeData_t representing ? i'm not familiar with this syntax. is it just to differeniate nodeData the struc from nodeData that comes after the struct declaration ? – Michael Apr 17 '11 at 18:03
  • @michael: Yep. Personally I never ever use the same name for a struct, it's typedef and an instance. – Erik Apr 17 '11 at 18:08
  • @michael: you can (maybe you should) use separate lines for all of defining a type, creating an alias, defining an object. `struct whatever_tag {/*members*/};`; `typedef struct whatever_tag whatever;`; `whatever object = {0};` – pmg Apr 17 '11 at 18:11
1

Without going into why you shouldn't use a global for this, you haven't created a global variable anywhere, only defined a structure and typedef'd it.

Prior to main you would need to do:

nodeData myNodeData;

And access it as myNodeData

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • thanks, what do you suggest i do instead ? should i define the struct inside main ? would the other threads called from main be able to use it ? – Michael Apr 17 '11 at 18:04
  • Typically you want to avoid globals. Not only from a memory standpoint but for readability and data encapsulation. Instantiating the struct in main and passing a pointer to it to your threads would be the common approach. – Brian Roach Apr 17 '11 at 18:05
  • how would the thread know a struct that was defined in main ? the thread is not defined in the same scope of main... – Michael Apr 17 '11 at 18:32
  • Then you would instantiate it wherever you're creating your threads. If you returning to main after that, you would either need to create it on the heap with `malloc` to avoid scoping issues, or still instantiate it in main and pass a pointer up through to where you're creating your threads. (Without seeing your code, that's a hard question to answer specifically). On the subject of globals, this is a fairly good read: [Global Variables are Bad](http://c2.com/cgi/wiki?GlobalVariablesAreBad) – Brian Roach Apr 17 '11 at 18:39