Register | Login | |||||
Main
| Memberlist
| Active users
| Calendar
| Chat
| Online users Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album |
| |
0 users currently in Programming. |
Acmlm's Board - I3 Archive - Programming - Compiler warning when using specific variable | New poll | | |
Add to favorites | Next newer thread | Next older thread |
User | Post | ||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6432 days Last view: 6432 days |
| ||
I know it's possible to generate a compiler warning if a specific flag is #defined, like this:
#ifdef SOME_FLAG #warning SOME_FLAG is defined #endif Is it possible, though, to make it give a warning when a certain global variable is used? #ifdef and #if defined don't seem to catch variables. The reason is I have a piece of code that I include into most of my programs, and previously it required you to define a FILE* as a global variable for it to use, and fclose() this when done. I recently changed it to not require this anymore, but if that fclose() call is left in, the program will crash trying to close a file that was never opened. The only thing I can think of to avoid this is to generate a warning if that variable is defined. Can this be done? |
|||
Leno_jr 90 Since: 05-14-06 Last post: 6619 days Last view: 6619 days |
| ||
Couldn't you just check to see if the variable is valid before you close?
Like for dynamic arrays, you generally declare them as 0 when you start up, make them if a file is loaded, then destroy it if it was made. However, since it won't always be made, basically throw in a if(variable) delete variable; line? On a side note, why the hell aren't there [code] tags? |
|||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6432 days Last view: 6432 days |
| ||
There's <code> tags, just not [code] tags.
Anyway the programs in question didn't do this check because it wasn't necessary. The variable would always be valid. What I want to do now is have it warn me when compiling a program that still uses this method, so I can remove all references to that variable entirely. Else I fear I'll find myself spending several hours trying to find out why it crashes on exit, because it's still trying to close that file that's no longer being opened. |
|||
neotransotaku Sledge Brother Liberated from school...until MLK day Since: 11-17-05 From: In Hearst Field Annex... Last post: 6434 days Last view: 6432 days |
| ||
I wonder why you don't have a check if the file pointer is valid before you call fclose? You can do a generic macro of the form
#define VERIFY_FILE_POINTER(pt) if(pt == NULL) { printf("invalid pointer"); exit(1); } if you then feel you don't need to do this check anymore, then just transform it to #define VERIFY_FILE_POINTER(pt) |
|||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6432 days Last view: 6432 days |
| ||
No, you guys don't seem to get the idea. This is old code I already wrote a long time ago that doesn't validate the pointer. I want the compiler to warn me when that old code is being used so I can go get rid of it. It's too late to implement error handling. | |||
MisterJones Tooky Since: 12-08-05 From: Mexico Last post: 6521 days Last view: 6458 days |
| ||
Originally posted by HyperMackerel Why not use an IDE with a class/var/function browser, and look for the instances it appears? |
|||
neotransotaku Sledge Brother Liberated from school...until MLK day Since: 11-17-05 From: In Hearst Field Annex... Last post: 6434 days Last view: 6432 days |
| ||
I doubt you can do what you want to do without tracing variables yourself by walking through the code. One thing that I highly doubt will work is to do a #define where the global variable is and then to do a #ifdef check at all fclose() calls.
You could try making a wrapper around fclose() and do a runtime check that way by providing context information each time you call fclose(). |
|||
Cellar Dweller + Red Koopa Since: 11-18-05 From: Arkansas Last post: 6441 days Last view: 6432 days |
| ||
You could delete or comment out the declaration of the variable and any extern declerations. Then try to compile the program and read the error messages. It may be useful to instruct your build tool to try to compile as much as possible, because otherwise the first source file to generate an error may stop the build attempt. If you use GNU make from the command line the option is -k. If you use an IDE, there may be dialog box that allows changes to the flags for make.
Also, why not just grep for the variable in all of the source files? |
|||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6432 days Last view: 6432 days |
| ||
Originally posted by Cellar Dweller + I think that's what I'll end up doing. Your post gave me an idea - put a second declaration of the variable in the header file, so it would generate a warning about duplicate definitions (the declaration is in each program, not in the file they include) - but I like the grep idea more. |
Add to favorites | Next newer thread | Next older thread |
Acmlm's Board - I3 Archive - Programming - Compiler warning when using specific variable | | |