Scope Question

I have a main program with a lot of modules. For instance, my LLNCCONVERT program that I discussed in the Icon thread is a module in that main program.

I know that if I declare a global string in the header file of the main program that it has global scope throughout all the modules, as long as I include that header file, of course.

But here's my question...

My LLNCONVERT module has a .cpp, a .h, and a .rc file. Now then, if I declare this string in the .cpp file for this module, does the string remain in memory after I exit this module?

In other word, say I do this...

1
2
3
4
5
6
7
// llnconvert.cpp

#include "llnconvert.h"

char szGlobalString[MAX_PATH]

int WINAPI MainProc(...)


So does szGlobalString go out of memory when I exit this module, or does it remain?

Trick Question! You don't have a semicolon at the end of Line 5 so this won't compile at all :D !

J\K, This should be accesible the same way that any functions that you declare in that file would be. That is to say you would need a way to access it if you plan on using it in your main .cpp file.
Yeah, I know about using the variable and how to access it, but what I want to know is if it still uses memory after the module is exited.

In my code above, I should have said...

1
2
3
4
5
6
7
// llnconvert.cpp

#include "llnconvert.h"

char szGlobalString[MAX_PATH];

INT_PTR CALLBACK LLNConvertProc(...)


And thus LLNConvertProc(0 would be called as a module from the main program...

1
2
3
4
5
// main.cpp

#include "main_prog.h"

int WINAPI WinMain(...)


so that after WinMain and all that, in the main proc I call the module, like so...

1
2
3
4
5
6
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDB_LLNCONVERT:
CreateDialog(..., ..., ..., LLNConvertProc();
break;


So you can see that LLNConvertProc() is only active when it is called by the main proc. So does the global string I declare in llnconvert.cpp stay active in memory after I exit the LLNConvertProc(), or is it discarded?
Last edited on
Not to nitpick but your switch case that handles the system messages should be in the callback function, you seem to be implying that it is part of your WinMain function. It technically executes faster that way.

As for you question, yes. As a global variable this is allocated as long as your application is running. Do you see a reason to suspect otherwise like an error or improper output? Or was this an academic question?
You're still not understanding me.

The switch statement IS in my callback function. I should have made that more explicit.

But here's the main point -- as you can see, the global string is ONLY declared in the module llnconvert.cpp.

I click on a button in the main program to call the llnconvert procedure. When I EXIT that llnconvert procedure, does the string remain in memory, even though I have exited the module.

Even though I'm still running the main program which calls the llnconvert module, yet the llnconvert module has now been closed. So does the string I declare in llnconvert.cpp remain in memory, or is it discarded?
Yes, your answer is YES Yankee-Echo-Sigma. The string held by the variable you declared in llnconvert.cpp that you labelled "szGlobalString" is still in memory after you exit that module. How is it you think I'M the one not understanding YOU?

EDIT: If you need visual proof then the test is VERY simple, try to create another global variable with the same name in your main .cpp file. Read the error.
Last edited on
Okay, I guess that answers it, but I don't know why it would remain in memory since its scope is ONLY in the llnconvert module, and thus when that module exits it should go out of scope. So that puzzles me.

I never can keep all the memory issues straigt, and when I go back to my C++ books I get impatient because there's so much to read on it.
When you declare a variable in the global namespace that means it's global across the board. I've tried a few times to come up with some kind of analogy but I can't think of anything, it's just something you get used to.
Topic archived. No new replies allowed.