Register | Login | |||||
Main
| Memberlist
| Active users
| Calendar
| Chat
| Online users Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album |
| |
0 users currently in Programming. |
User | Post |
HyperHacker Posts: 2359/5072 |
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. |
Cellar Dweller + Posts: 69/138 |
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? |
neotransotaku Posts: 1348/1860 |
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(). |
MisterJones Posts: 41/125 |
Originally posted by HyperMackerel Why not use an IDE with a class/var/function browser, and look for the instances it appears? |
HyperHacker Posts: 2346/5072 |
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. |
neotransotaku Posts: 1339/1860 |
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 Posts: 2334/5072 |
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. |
Leno_jr Posts: 39/96 |
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 Posts: 2331/5072 |
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? |