Structures? *brain melts*

This is a discussion on Structures? *brain melts* within the C++ Programming forums, part of the General Programming Boards category; Originally posted by Bubba Well anyways now that the interested party is confused thanks to our ranting and raving....next question ...

  1. #46
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by Bubba
    Well anyways now that the interested party is confused thanks to our ranting and raving....next question MrDoomMaster and we will see if we can't confuse you more.



    Just kidding man...really..we don't mind helping. We've all been there scratching our heads before....and still do.


    edit: Man this post is growing exponentially. Can't keep up.


    Looks like you got it man....but the 3D analogy lost me...so let's just forget you made it and move on.
    LOL thanks man, I'm getting there! But yeah, I have an obsession with creating local variables to save memory... I like to free space whenever I'm not using a variable, I believe this is good programming technique. So, having a local structure in a module when you're only going to be USING it in that module would be healthy to ME, but I'm just a noob, so if someone has an argument to this please argue, because I'm in a "learning" mode

    Bubba most certainly you do know your code, as all of you do. I was a bit lost when you were getting into the complex terminology, I'm not THAT knowledgeable yet!
    --MrDoomMaster
    The kind of DooM that makes the MooD

  2. #47
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Originally posted by Bubba
    What? When did we start talking about headers? Now I think I'm confused.
    He asked the question very discreetly. It was a comment within his code.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #48
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    So, having a local structure in a module when you're only going to be USING it in that module would be healthy to ME, but I'm just a noob, so if someone has an argument to this please argue, because I'm in a "learning" mode
    I'll take you up on that offer.

    First, by module, do you mean the entire source file, or just the function int main()? If you mean the latter, don't say module, say function.

    You can't have local structures in a function, that's not possible. It wouldn't make a difference to memory usage anyway, it's just the way c++ works.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #49
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Anyone think the Bloodshed Dev-C++ compiler is any good? Is there a better? Opinions welcome!
    --MrDoomMaster
    The kind of DooM that makes the MooD

  5. #50
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Ok, yeah I see it now. Man like I said thread is moving at light speed. Almost like a chat room.




    Might hold the record for fastest growing thread.


    Would be good for FAQ board perhaps.

  6. #51
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I have an obsession with creating local variables to save memory
    It wont make a difference. The variable takes up the exact same amount of space wherever it is.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #52
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by bennyandthejets
    I'll take you up on that offer.

    First, by module, do you mean the entire source file, or just the function int main()? If you mean the latter, don't say module, say function.

    You can't have local structures in a function, that's not possible. It wouldn't make a difference to memory usage anyway, it's just the way c++ works.
    well yes, I do mean functions... different blocks of code (for organizing data, to make the code more readable)

    and by "local" I mean, once I'm done with using initialized variables from a structure, I want to free up all memory that was used by that structure and all of its variables. Seems only right if I wouldn't use it again. I have learned that Local variables only last till the end of the function or block of code. Prove me wrong again if you will, I still feel like I'm missing some things lol
    --MrDoomMaster
    The kind of DooM that makes the MooD

  8. #53
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Here goes...

    Local variables, for functions that is, have a very limited lifetime and are not in memory at all. They are created on the stack and then removed from the stack when the function returns.

    Code:
    void FooBar(void)
    {
      int x=50;
      int y=100;
      int z=150;
    }
    Here is the simplified pseudocode for this function:

    Create stack space for x
    Create stack space for y
    Create stack space for z
    Set x to 50
    Set y to 100
    Set z to 150
    Remove z from stack
    Remove y from stack
    Remove x from stack
    Restore stack pointer to where it was prior to entering function
    Return to caller


    I omitted some things for clarity's sake



    x,y, and z only exist within this function. After that they have gone out of scope.
    Last edited by VirtualAce; 11-01-2003 at 05:45 PM.

  9. #54
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by Bubba
    Here goes...

    Local variables, for functions that is, have a very limited lifetime and are not in memory at all. They are created on the stack and then removed from the stack when the function returns.

    Code:
    void FooBar(void)
    {
      int x=50;
      int y=100;
      int z=150;
    }

    x,y, and z only exist within this function. After that they have gone out of scope.
    what is "stack"?
    --MrDoomMaster
    The kind of DooM that makes the MooD

  10. #55
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by Bubba
    New is basically malloc(sizeof(object)).
    It is similar in the manner that it allocates an object, yes. But the standard is not anything like this:
    Code:
    void *operator new(size_t size) {
     void *mem;
     mem = malloc(size);
     if(!mem) {
      bad_alloc ba;
      throw ba;
     }
     return mem;
    }
    The C++ standard of new allocates memory differently than malloc, so yes, they are similar at heart in that they allocate memory, but not similar in the way they do it
    Do not make direct eye contact with me.

  11. #56
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    If you declare a variable within a function, and it is not static, then yes, it will be freed when the function exits. Note that by static I mean the following:

    Code:
    void func()
    {
           static int a;
           int b;
           
           a++;
           b++;
    }
    The variable a is created once, and is only destroyed at the end of the program. That is what 'static' does. b however, is created and destroyed every time the function is called.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  12. #57
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    55 replies already? And you are getting any information out of all this nonsense Now you probably got bubba started with assembly stuff
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #58
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by MrDoomMaster
    what is "stack"?
    The stack is (hmm how to explain ) basically a list that can have items dynamically added / removed from it. The local variables are placed on the stack until the function returns, where the locals are removed from the stack.

    EDIT: Like benny said, as long as they are not static variables.
    Do not make direct eye contact with me.

  14. #59
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by bennyandthejets
    If you declare a variable within a function, and it is not static, then yes, it will be freed when the function exits. Note that by static I mean the following:

    Code:
    void func()
    {
           static int a;
           int b;
           
           a++;
           b++;
    }
    The variable a is created once, and is only destroyed at the end of the program. That is what 'static' does. b however, is created and destroyed every time the function is called.
    OH so by default, all variables are set to be "destroyed" after a function returns? Hmm... so you have to specify in the code which variables you want to remain after the function has ended? But of course, declaring a variable in the mainline would make it global also, right?
    --MrDoomMaster
    The kind of DooM that makes the MooD

  15. #60
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    OMG...slow down everyone. Now we are throwing and catching and talking about stacks and....we can't write the whole C++ book in one post.

    Now where were we?

    Thought we were going to talk about pointers.

Page 4 of 8 FirstFirst 12345678 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-06-2006, 12:06 AM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 09:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21