Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
C/C++ Thingies.

 
Post new topic   Reply to topic Printable version
 View previous topic  Unknown but banned? Post :: Post Ownzor game.  View next topic  
Author Message
ExplodyThingy
Server Help Squatter


Age:38
Gender:Gender:Male
Joined: Dec 15 2002
Posts: 528
Location: Washington DC
Offline

PostPosted: Sat May 01, 2004 8:55 pm   Post maybe stupid    Post subject: C/C++ Thingies. Reply to topic Reply with quote

Fantastic. Hit something I should have learned, but didnt since I havent had a class in C/C++. Im trying to create a single instance of a class across the entire program. It would be the CFG class used to maintain the variables so I dont have to do GetPrivateProfileString every time, or create several instances of it which would seriously screw up a refresh of the class. I know its via "extern" by I haven't been able to get the syntax and setup correct. Im not sure if its a problem with something like this, but the program is multithreaded.
_________________
There are no stupid question, but there are many inquisitive idiots.
Loot

Dr Brain> I hate clean air and clean water. I'm a member of Evil Conservitive Industries
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sat May 01, 2004 8:57 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

You are trying to make it so all files see the same piece of data (in the case a class)?

File 1:

MyClass xyz;

File 2:

extern MyClass xyz;

File 3:

extern MyClass xyz;


It must be declared outside of any scoping mechanism (eg not in a function), making it public.
_________________
4,691 irradiated haggis!
Back to top
View users profile Send private message Add User to Ignore List
ExplodyThingy
Server Help Squatter


Age:38
Gender:Gender:Male
Joined: Dec 15 2002
Posts: 528
Location: Washington DC
Offline

PostPosted: Sat May 01, 2004 9:07 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

Gracies, and that was really friggin fast.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
ExplodyThingy
Server Help Squatter


Age:38
Gender:Gender:Male
Joined: Dec 15 2002
Posts: 528
Location: Washington DC
Offline

PostPosted: Sat May 01, 2004 9:13 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

Ok, didnt do what I wanted it to do.
I have "extern CFG cfg;" at the bottom of CFG.h to keep it across all the files, for now. In main.cpp, the file with main(), there is "CFG cfg;". In the CFG constructor there is "printf("CFG Made.\n");" Program runs and I see that once. If I goto another file, for example threads.cpp which also includes CFG.h, and do cfg.meh(), I see it "CFG Made.\nCFG Made.\n" printed out.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sat May 01, 2004 11:02 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

What does cfg.meh() do?
Back to top
View users profile Send private message Add User to Ignore List
ExplodyThingy
Server Help Squatter


Age:38
Gender:Gender:Male
Joined: Dec 15 2002
Posts: 528
Location: Washington DC
Offline

PostPosted: Sat May 01, 2004 11:46 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

its just "printf("meh.\h");" The class contains a constructor and a meh() function, no instance variables at the moment. What has me worried is that I see the constructor execute twice when it first revs up.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sun May 02, 2004 12:42 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

Debug it. Put a breakpoint in the contructor. When it breaks, note the value of "this" (pointer to the current object). If you see 2 different values of "this" each time it is called, then somehow you have instantiated 2 different instances of the class. If not, then something is wrong in the way the class is configured. If you are still stuck, I can look at your source.
Back to top
View users profile Send private message Add User to Ignore List
numpf
Newbie


Joined: Feb 17 2004
Posts: 24
Offline

PostPosted: Sun May 02, 2004 8:59 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

You aren't calling new CFG(); right? You know that the CFG cfg; declaration in main.cpp implicitly calls the constructor before main() gets called right?

The single-instance-per-process pattern is so common is has a name: Singleton. If you google it you can find how to rigorously code it (if you want), or at least read about the pitfalls of doing something like that. Make sure you find something about C++ specifically; singletons can and should be done differently in java, so reading about implementing a java singleton doesn't help you.

-numpf
Back to top
View users profile Send private message Add User to Ignore List
Explody
Guest


Offline

PostPosted: Mon May 03, 2004 7:51 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

Of course Im not calling new() outside the implicit one. The flaw may be that I had "extern CFG cfg;" at the bottom of cfg.h, which naturally is inluded into main.cpp. In main.cpp there is the "CFG cfg;", so it essense has "extern CFG cfg; CFG cfg;" Ill try moving it first then, doing a little more research on Singleton.
Again, multithreading wouldnt be a problem would it? At the moment, there is only 1 function ever called from the same thread as main(), and that is main() itself.
If theres still a problem Ill post the whole thing on my site.
Back to top
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Mon May 03, 2004 2:36 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

extern CFG cfg; Cfg cfg; is fine. That won't create 2 instances.
Back to top
View users profile Send private message Add User to Ignore List
ExplodyThingy
Server Help Squatter


Age:38
Gender:Gender:Male
Joined: Dec 15 2002
Posts: 528
Location: Washington DC
Offline

PostPosted: Mon May 03, 2004 6:19 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

Ha, fixed. Some macro somewhere was getting set wrong so it made it twice. Stupid me. Now, is there anyone familiar with POSIX Threads that knows how to make thread-safe stuff? Such as, thread locking?
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Mon May 03, 2004 7:58 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

What does this app do? I've had many people ask me about how to do multi-threading only to find out they were using it when they didn't need to. It's cool to play with it to learn, but it's a waste in general if it's not needed -- a little extra design, slightly more complex code and overhead.
Back to top
View users profile Send private message Add User to Ignore List
Explody Thingy
Guest


Offline

PostPosted: Tue May 04, 2004 7:49 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

Its a trial of a TCP/UDP billing server. You know, those knock-off things I have on my website that dont really work except that this one actually does work. For something as slow running as a billing server Im sure its more overhead than I need, but since the socket wrappers I am currently using are blocking, its the best fit. Naturally, again I found what I was looking for on the 'net with pthread_mutex'es.
And just so you know, almost every program Ive ever written has been simply to learn and play with. Thats why once I get them quasi-running, I often get bored (a la explbot).
Back to top
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Tue May 04, 2004 3:55 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

Is this app supposed to be cross-platform? If not, I suggest you make it using raw non-blocking socket and a single thread.
Back to top
View users profile Send private message Add User to Ignore List
ExplodyThingy
Server Help Squatter


Age:38
Gender:Gender:Male
Joined: Dec 15 2002
Posts: 528
Location: Washington DC
Offline

PostPosted: Tue May 04, 2004 4:03 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

Geeze. Just reply immediately after I reply to your PM why dont you.

It doesnt need to be cross platform, but it doesnt hurt either. Te only real reason Im using blocing sockets is because I dont have a good multi-session nonblocking TCP server side wrapper and dont yet have the know how to do my own.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Trash Talk All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 267 page(s) served in previous 5 minutes.

phpBB Created this page in 0.857167 seconds : 39 queries executed (86.6%): GZIP compression disabled