Thread: Avoiding Global variables

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    cstdio is C++, not C.
    C uses stdio.h.

    Are you writing C or C++? You seem to have a mix.
    C...

    No were in my code i've included cstdio.h... Don;t know where's that coming from

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, but you #include <string>, which is a C++ standard header. Perhaps you wanted to #include <string.h> instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by laserlight View Post
    Ah, but you #include <string>, which is a C++ standard header. Perhaps you wanted to #include <string.h> instead.
    No Difference... Is there anyone using Visual Studio 2003/2005/2008 who can just copy my code and try compiling it as .c?

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    C...

    No were in my code i've included cstdio.h... Don;t know where's that coming from
    Oh right, I saw it in the error output.
    Anyway, you are using C, yet you are using C++ stuff like std::string.
    Seriously, that code needs to rego a big change and either you need to learn C or stick to learning C++, because this code cannot compile as C (namespaces aren't supported in C either).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Oh right, I saw it in the error output.
    Anyway, you are using C, yet you are using C++ stuff like std::string.
    Seriously, that code needs to rego a big change and either you need to learn C or stick to learning C++, because this code cannot compile as C (namespaces aren't supported in C either).
    Thought about that too, will fix such and come back to you... Thnx

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Oh right, I saw it in the error output.
    Anyway, you are using C, yet you are using C++ stuff like std::string.
    Seriously, that code needs to rego a big change and either you need to learn C or stick to learning C++, because this code cannot compile as C (namespaces aren't supported in C either).
    Actually, when i though about that, i commented every CPP code style but still got the same errors... But will go through my code to make sure is 100&#37; C

  7. #22
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by matsp View Post
    [my personal opinion is that precompiled headers are only worth-wile in big projects - in small projects the saving is pretty small, really].
    First thing I turn off in new projects, usually in the wizard, or first thing if the wizard wont let me (win32 projects). The savings even on large projects are negligiable. I have one project that has 100,000 lines of code in ~100 files, each including ~5 headers. It takes about 2 minutes to compile with PCH and 2 minutes 30 seconds without. I dont honestly see the point. I suppose if you had millions of lines of code and tens of thousands of #include's it migth make a bigger difference, but again, whats the point, you will still be compiling for an hour.

    IMO they woudl get better performance by multithreading the compile. FileA can be compiled in parallel with FILEB, the linker stage would probably still need to be serial, but not compilation.
    Last edited by abachler; 05-15-2008 at 09:17 AM.

  8. #23
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Visual Studio is terribly slow with large projects but is reported to be faster than most other compilers. Pre-compiled headers are far more headache than they are worth which is why at home I don't use them and we also do not use them at work.

    I've noticed that even on a quad-core machine VS2003 and VS2005 take one core up to 100&#37; and leave the other three at 0%. The compile is multithreaded insofar as you can still interact with the GUI but I seriously doubt if the compile process itself is multi-threaded. It 'appears' to be one thread.

  9. #24
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Honestly, i think the whole multithreading thing kind of caught most companies by surprise, it was never a serious technique until multi-core made it give huge performance increases. Now I see hundreds of black box 'turn yoru serial code into parallel code without having to think' solutions. OpenMP, RapidMind, CUDA, although the last 2 are really for use with GPU's its only a matter of time before CPU's support native vector processing. I wouldn't mind, but I end up having to evaluate every new flavor of the month and then explain to the suit why it wont improve our applications or reduce development time (it mostly has to do with some internally closed source libraries that he wont let me get the code to so I can't optimize them).

    The only real reason I can see to use PCH is to obfuscate a distributed LIB so thay cant even see your class structure.
    Last edited by abachler; 05-15-2008 at 04:47 PM.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think somewhere both of you have had bad experiences, because I really don't get what you do.
    PCH speeds up compiles greatly. You just have to include windows.h and you'll notice how fast it gets. 30 seconds is a big deal if you ask me.

    PCH are not difficult to use and they are not error prone (at least not to me). I haven't received any problems with them pretty much in any project I have. Just don't include headers that keep on changing. Only static non-changing headers go there and you should have no problems whatsoever.

    So far as I know, VS can only do multi-threaded compile with different projects, because I really have been unable to get it compiling on anything else than one core in one project.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Yeah I know it only uses a single thread, I was just saying they would get better performance increases by going MT than by saddling us with PCH. 30 seconds on a 2 minute compile is nothing, since Im going to get a water and bull........ with a collegue anyway.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, MT with a single project would be great. Additionally, I think there is something along the lines of that, or should be, but I've never gotten something like that to work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    ...since Im going to get a water and bull........ with a collegue anyway.
    In this case your not slacking, your code is compiling. Just like the shirt says.

  14. #29
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Bubba View Post
    In this case your not slacking, your code is compiling. Just like the shirt says.
    I need one of those shirts. My boss is always acting like he is catching me slacking when Im just thinking He got ........ed today because he thought he 'caught' me again when I was just posting the post up above. He thinks im always doing something nefarious, poor guy has a lot of mental issues because of previous employees. I tolerate it because I can empathize with him about getting screwed over, but its getting to the point where he is the one screwing me over by not giving me my reviews or raises or profit sharing. I really would like to get a good review, but if I'm just going to get a crap review then I think its better to cut my losses now and stop wasting time at this company. Its not like he is developing my skill set like I expected. He acts like he has this amazing breakthrough AI that is so secret noone can see the source code, when in reality its just a neural network class that doesnt do anything any other neural network class doesnt do. Even if he did have some amazing new neuron model, it woudlnt matter because you cant get down to the individual neuron, you are saddled with ancient auto associative and hetero architectures. I mean its really frustrating on a personal, intellectual and professional level. He isnt teaching me anything about AI that I didn't already know 10 years ago, he doesnt let me talk to the customers directly, so I'm not making business contacts, and he basically ignores any suggestions I make, in fact he will do the opposite just to spite me. e.g. yesterday another engineer was having a problem with a robot where the set screws on teh wheels keep backign out. SO I suggested using a dab of nail polish to keep the screw from backing out rather than using lok-tite which would keep it from EVER backing out even if you wanted to change the tires (these are really small screws, so you cant get any torque on them to break the lok-tite). He overrides my suggestion and tells the guy to use superglue. I just walked back to my office, how the hell do you argue with a level of intelligence like that?
    Last edited by abachler; 05-15-2008 at 09:25 PM.

  15. #30
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Oh right, I saw it in the error output.
    Anyway, you are using C, yet you are using C++ stuff like std::string.
    Seriously, that code needs to rego a big change and either you need to learn C or stick to learning C++, because this code cannot compile as C (namespaces aren't supported in C either).
    Ok, took me a while but managed to convert all the code to pure C... I now can compile but down to two warnings... This has to do with the ID of the child window

    Code:
    HWND CreateButton(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    				  const RECT& rc,const int id,const ustring& caption)
    {
    	dwStyle|=WS_CHILD|WS_VISIBLE;
    	return CreateWindowEx(0,         
    		_T("button"),               
    		caption.c_str(),            
    		dwStyle,                    
    		rc.left,                     
    		rc.top,                      
    		rc.right,                     
    		rc.bottom,                    
    		hParent,                      
    		reinterpret_cast<HMENU>(static_cast<INT_PTR>(id)),
    		hInst,                        
    		0);                        
    }
    What you see above is a C++ style, converting into C ...

    Code:
    (HMENU)id
    ... gives a warning

    Code:
    warning C4312: 'type cast' : conversion from 'int' to 'HMENU' of greater size
    Ignoring this warning disables the minimize/restore/close buttons on the window... Any ideas on how to convert this properly?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. global variables
    By shadovv in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2005, 02:21 PM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM