Thread: local variables and pointers

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    50

    local variables and pointers

    Hi guys, Im studying Functions subject and there's something confusing me regarding to local variables and memory managment , what I've understand that after finishing from function execution then all the local variables are vanished -empytted out- , I understand it conceptually -vanished- but what does -vanished- means in termsof memory .. the addresses gets deleted?

    Another question, sometimes when I return a local pointer -local pointer means that I defined a pointer in the function itself- and if I want to return this pointer, then an Error Execution pop out because Im returning a pointer .. alothugh the function should return a pointer because its type is to return pointer, what I figured out that because the pointer is locally and Im doing return to that pointer , then it shows an error .. I didn't understand why this error happens ...any help?
    from my understanding although that pointer is locally I can return it because return is a function that return whatever things in its argument ..


    thanks alot

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Brian_Teir
    I understand it conceptually -vanished- but what does -vanished- means in termsof memory .. the addresses gets deleted?
    The exact meaning is implementation defined, but it likely means that the memory becomes available for allocation elsewhere.

    Quote Originally Posted by Brian_Teir
    Another question, sometimes when I return a local pointer -local pointer means that I defined a pointer in the function itself- and if I want to return this pointer, then an Error Execution pop out because Im returning a pointer
    It's perfectly fine to return a local pointer. The problem arises when you return a pointer to a non-static local object. If the pointer thus returned is then used in the caller, the behaviour is undefined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    50
    Quote Originally Posted by laserlight View Post

    It's perfectly fine to return a local pointer. The problem arises when you return a pointer to a non-static local object. If the pointer thus returned is then used in the caller, the behaviour is undefined.
    TO BE HONEST, I didn't understand what do you mean with non-static local object, may you attach example -simple example- to ilustrate it more? thanks alot

  4. #4
    Registered User
    Join Date
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    26
    Quote Originally Posted by Brian_Teir View Post
    but what does -vanished- means in termsof memory .. the addresses gets deleted?
    Usually, space is allocated for local variables in the stack. The address calling the function is at the top of the stack (conceptually, although it might well be the lowest memory address, as the stack builds downwards) with the space for local variables lower in the stack. When the function terminates, the stack pointer is adjusted to whatever value it had before the function was called, releasing that space.

  5. #5
    Registered User
    Join Date
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    26
    "Static" is a key word with which you can define variables in a function. It means that the variable is private to the function but retained between calls of the function. So, unlike ordinary local variables, it has a permanent address rather than being in the stack.

  6. #6
    Registered User
    Join Date
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    26
    Quote Originally Posted by laserlight View Post
    The exact meaning is implementation defined, but it likely means that the memory becomes available for allocation elsewhere.
    Just out of curiosity, are there any modern systems that implement it differently than by means of a stack?

    My first exposure to assembler programming was for the IBM 360, and it was nightmarish, partly because it was not stack-based. The developers must have had their reasons for such convoluted alternatives; but I'm at a loss to think of any, and read somewhere that the manager who gave final approval to that decision was later fired for it. A few years later, I took a course using the DEC-10, and suddenly everything made sense.

  7. #7
    Registered User
    Join Date
    Nov 2019
    Posts
    50
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do the global and local variables use the RAM?
    By YuminZhao in forum C++ Programming
    Replies: 2
    Last Post: 09-04-2011, 10:45 AM
  2. Replies: 6
    Last Post: 12-02-2009, 08:47 AM
  3. local and global variables???
    By geo_c in forum C Programming
    Replies: 5
    Last Post: 08-23-2004, 03:02 PM
  4. local variables
    By Gil22 in forum C++ Programming
    Replies: 14
    Last Post: 04-09-2003, 09:13 PM
  5. local variables
    By Unregistered in forum C Programming
    Replies: 14
    Last Post: 03-20-2002, 02:55 PM

Tags for this Thread