   Scope of pointers
  Suppose that I create some variable pointer inside a function.  Will it still exist outside the function, or will I no longer be able to access that piece of data? Isn't one of the main perks of dynamic memory allocation the ability to create values inside functions that exist outside them?My program has an initilization function that I would prefer to use to create all of my variables, as opposed to throwing them all in main.  How can I go about doing this?   
Quote:Original post by zoner7Suppose that I create some variable pointer inside a function.  Will it still exist outside the function, or will I no longer be able to access that piece of data? The pointer will not exist outside of the function. That data (the 4 bytes that represent the pointer) will be lost. The data that it points at (assuming you new'd it) will sit around until you delete it.Quote:My program has an initilization function that I would prefer to use to create all of my variables, as opposed to throwing them all in main.  How can I go about doing this?int *x;int *y;// it's been a while since I've used C++, so this might be a little off;void    init(){    x = new int();    y = new int();    *x = 42;    *y = 9;}void main(){    init();    std::cout << *x << std::endl;    std::cout << *y << std::endl; delete x;    delete y;};  If you allocate them dynamically with 'new' or 'malloc', or whichever is appropriate for your programming environment, then they will exist until you explicitly free them with 'delete' or 'free', even outside the function. If you have a local variable, that you don't allocate memory for yourself, then it will cease to exist when the function returns.   ;  Your pointer won't exist -it's gone out of scope at the end of the function.The data your pointer was pointing to will still exist, and you will be unable to retrieve it, causing a memory leak.If you return that pointer from the function, the code receiving it will be able to work with the data pointed to by the original pointer. The original pointer will be lost - it's gone out of scope, a copy of that pointer will be returned, but both will be pointing to the same data in memory.   ;  Here is a simplified version of what I need to do.I want to create a bunch of ball's from my ball class.Could I simply create these and then put them all in an array that was passed into the function?  Since an array is a pointer, it would recognize the location of the data that stores these balls...void Initialize(BallHolder[15]){    //so this would use 2x bytes (however many are needed by the ball class) of memory I imagine. myball myball(0,0);    ball ball1;    //now we are setting the value pointed to by BallHolder[0] to myball and the value pointed to by BallHolder[0 + 1] to ball1.    BallHolder[0] = myball;BallHolder[1] = ball1;}When the function ends, the variables myball and ball1 go out of scope because they were defined inside the function.BallHolder now points to data that contains the same contents as at the original locations of and myball ball1 (even though the data is now located in a different place).I think i get itTotally unrelated note:  I think something with the forums might be broken because I have the option to edit other people's posts.   ;  You have the 'option' to 'try' to edit other people's posts. If you attempt to go through with it you'll find yourself restricted.Your latest sample code is problematic. You have two types, myball and ball, that you attempt to assign to the various elements in BallHolder. Unless you a)used inheritance and myball and ball are derived from a common base class and b)want to 'slice' the types then that code won't do what you want it to. I'm going to assume you have one ball type for the purposes of my following example of what I would do in your situation.And that is to use a std::vector and pass that to your initialization function by using a reference, something like this:void Initialize(std::vector<Ball> &BallHolder){  BallHolder.push_back(BallHolder(0,0)); // BallHolder[0]  BallHolder.push_back(BallHolder(1,0,3.14159)); // BallHolder[1]  Ball newBall;  newBall.param1 = whatever;  newBall.performAction(data);  BallHolder.push_back(newBall);}This will allow you to easily update your initialization function in the future to add more or less balls without having to update or manually keep track of the number of balls used. std::vector is an important tool in any C++ programmer's toolbox. Hope this helps.   ;Quote:Original post by zoner7Here is a simplified version of what I need to do.I want to create a bunch of ball's from my ball class.Could I simply create these and then put them all in an array that was passed into the function?  Since an array is a pointer, it would recognize the location of the data that stores these balls...*** Source Snippet Removed ***When the function ends, the variables myball and ball1 go out of scope because they were defined inside the function.BallHolder now points to data that contains the same contents as at the original locations of and myball ball1 (even though the data is now located in a different place).I think i get itTotally unrelated note:  I think something with the forums might be broken because I have the option to edit other people's posts.Now I will be happy to dissect your code :-void Initialize(BallHolder[15]){    //so this would use 2x bytes (however many are needed by the ball class) of memory I imagine. myball myball(0,0); <--- stack variableball ball1;    //now we are setting the value pointed to by BallHolder[0] to myball and the value pointed to by BallHolder[0 + 1] to ball1.   BallHolder[0] = myball;BallHolder[1] = ball1;}1. myball myball(0,0) is wrong as you shouldn't have variable with the class name. Strangely it does compile though. Although it would get really confusing. I assume this a typo.2. You are creating instances on the stack. Once you exit the function, the data in there is lost and the ones you created dynamically in myball just magically are left around and now there will be a memory leak.3.  BallHolder[0] = myball;BallHolder[1] = ball1;If you don't have a proper copy constructor, you messed up here. You will get BallHolder pointing to invalid data once its out of the function. Plus you don't want to assign it this way. It will get way confusing and error prone.  Edited for stupidity (the code I wrote was completely bogus)   ;Quote:Original post by nobodynewsYou have the 'option' to 'try' to edit other people's posts. If you attempt to go through with it you'll find yourself restricted.Your latest sample code is problematic. You have two types, myball and ball, that you attempt to assign to the various elements in BallHolder. Unless you a)used inheritance and myball and ball are derived from a common base class and b)want to 'slice' the types then that code won't do what you want it to.ball is inherited from one class and myball is inherited from ball.So i'd imagine that if I slightly modified your vector code, they would be exactly the same.I'm not sure why I need to use vectors, however.  it seems like that would only complicate the syntax of the program because of iterators and slow it down.  I don't need to use any search functions, so benefits of the vector don't apply.I thought this is a vector of classes.  Why would I want to merely add coordinates? Shouldn't that code be:[source lang = cpp]void Initialize(std::vector<Ball> &BallHolder){  ball ball1;  ball ball2  BallHolder.push_back(Ball1(0,0)); // BallHolder[0]  BallHolder.push_back(Ball2(1,0,3.14159)); // BallHolder[1]  newBall.param1 = whatever;  newBall.performAction(data);  BallHolder.push_back(newBall);}ooo... I just realized something... how can I make a vector with two different types of classes.  will it still work since myball is inherited from ball?   ;Quote:I'm not sure why I need to use vectors, however.  it seems like that would only complicate the syntax of the program because of iterators...It's not a given that using vector makes the syntax more complex (for one thing, you can use integer indices with vector objects just as you would with raw arrays).Quote:...and slow it down.How would using std::vector slow your program down? Can you give an example?Quote:I don't need to use any search functions, so benefits of the vector don't apply.I'm not quite sure what you mean by this, but the benefits of using std::vector have almost nothing to do with search functions.   ;Quote:Original post by jykQuote:I'm not sure why I need to use vectors, however.  it seems like that would only complicate the syntax of the program because of iterators...It's not a given that using vector makes the syntax more complex (for one thing, you can use integer indices with vector objects just as you would with raw arrays).Quote:...and slow it down.How would using std::vector slow your program down? Can you give an example?Quote:I don't need to use any search functions, so benefits of the vector don't apply.I'm not quite sure what you mean by this, but the benefits of using std::vector have almost nothing to do with search functions.I suppose its kind of irrelevant in this particular situation, but as I understand, an array is a more efficient container (if that's what you call it) than a vector (perhaps it has to do with memory usage... even though that is irrelevant in this program), at least in some respects that is.  one of the main advantages of a vector is that they are very adjustable.  Something can be added or removed from the middle of the list without much overhead.  This is not the case with arrays.  In my program, however, since I don't need to manipulate the list of balls very much, I would not gain any benefits from the use of a vector.  i would only incur the efficiency costs inherent in the vector container.I guess what I meant by search was the above... yea I see what you mean.  it isn't really searching at all.Anyhow, can someone show me sample code that details how to do this?  I'm really confused.  Note that myball and ball are two different classes.  I'm not sure how to specify that an array (or vector) holds two different kinds of classes.   