Archived

This topic is now archived and is closed to further replies.

Quick Question. Global Vars...

This topic is 5649 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you declare a Global Var inside a function in C++? Is it possible? Normally global vars are declared outside of functions, but can you declare a var inside a function and access it outside functions? Thanks! ~~~~~~~~~~~ Chris Vogel ~~~~~~~~~~~

Share this post


Link to post
Share on other sites
How about:



int* myVarRef = NULL;


int func1(void)
{
static int myVar = 1;

myVarRef = &myVar;

}


This should work. Not very recommended though. You can''t reference it by name though.

Share this post


Link to post
Share on other sites
I got it now... I created the global var i needed and just returned the value from the function into the global var.. I''m still pretty new to C, but I''m slowly getting it.

What still throws me WAY off is pointers. I just don''t really follow them. And then there are double pointers.. I wish I could find a good explanation of what pointers are for. I know WHAT they are and some of what they do. I just don''t understand WHEN to use them. And when I see them in code that I didn''t write, I have trouble following it.



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
quote:
Original post by Radagar
I got it now... I created the global var i needed and just returned the value from the function into the global var.. I''m still pretty new to C, but I''m slowly getting it.

What still throws me WAY off is pointers. I just don''t really follow them. And then there are double pointers.. I wish I could find a good explanation of what pointers are for. I know WHAT they are and some of what they do. I just don''t understand WHEN to use them. And when I see them in code that I didn''t write, I have trouble following it.



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~



Just keep on going. It takes some getting-used-to. Here is a very good introduction to pointers:

http://www.iota-six.co.uk/c/24_point.htm

ENJOY



**************************
A year spent in artificial intelligence is enough to make one believe in God.

Share this post


Link to post
Share on other sites
I''m just starting to understand pointers (I think) and one of the best uses that someone pointed out is say you want a pass a class to a function... why pass the entire class which might be HUGE, when you can simply pass an address to that class? Don''t remember who said that, but it has helped me a lot recently

Share this post


Link to post
Share on other sites
quote:
Original post by Radagar
What still throws me WAY off is pointers.


Hang in there. After a while that little light will switch on (you know, that very instance it all of sudden makes sense. My first computer science teacher called this, "getting the aha!" Very accurate description IMO ) and after that, they''ll become second nature.

Just remember this mantra:

"Pointers are good. Pointers are good..."


/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

Share this post


Link to post
Share on other sites
quote:
Original post by JuNC
How about:


int* myVarRef = NULL;

int func1(void)
{
static int myVar = 1;
myVarRef = &myVar;
}




Yikes! Don''t do that! Although it should compile fine, it will not perform correctly. Local variables are created at the start of a function, and destroyed when the function is done. If you assign a pointer to a local variable (which will soon be destroyed) you are left with a dangling pointer (memory leak). The data the pointer points at will become irrelevant and useless.

Something that might help you with pointers:


  
int iNumber = 10;
int* pPointerToNumber = &iNumber;


iNumber is an integer, a plain old number. You already know this.
pPointerToNumber is a pointer (denoted by the * after the int). pPointerToNumber stores the address of iNumber. When you use pPointerToNumber, you are requesting whatever data exists at the address that has been assigned to pPointerToNumber. In the example, pPointerToNumber is assigned to the address of iNumber (&iNumber), but it could be reassigned at any time during runtime.

Try to think of it this way:

A data variable is a house.
A pointer is the house''s street address.

Hope that helps some.
-Mike

Share this post


Link to post
Share on other sites
quote:
Original post by doctorsixstring
Although it should compile fine, it will not perform correctly. Local variables are created at the start of a function, and destroyed when the function is done. If you assign a pointer to a local variable (which will soon be destroyed) you are left with a dangling pointer (memory leak).


Incorrect. He is using static local vaiables. Static local variables lives on when your function is inactive.

Share this post


Link to post
Share on other sites
quote:
Original post by En Dum En
Incorrect. He is using static local vaiables. Static local variables lives on when your function is inactive.



True. This would compile and run fine, but it''s still not a good idea to return a reference or pointer to something that is local, IMO. That defeats the whole point of it being local. If it''s supposed to be global, then it should be global from the start and not just suddenly become global. It''s just bad design.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

Share this post


Link to post
Share on other sites
quote:
Original post by Chem0sh

True. This would compile and run fine, but it''s still not a good idea to return a reference or pointer to something that is local, IMO. That defeats the whole point of it being local. If it''s supposed to be global, then it should be global from the start and not just suddenly become global. It''s just bad design.



Incidentally, this how one version of the Singleton pattern is implemented.

Share this post


Link to post
Share on other sites