Accessment of dynamic variables.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parul Bagadia
    New Member
    • Mar 2008
    • 188

    Accessment of dynamic variables.

    The space allocated for dynamic variables can be accessed only through pointers... this is a statemant i got in book of data structure..
    Why is it so?
    One more doubt is; whenever we are using scanf...
    scanf("%format specifier",&var iable); is it not a dynamic allocation?
    If yes how?, if not how?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Dynamic allocations are identified by address. That means you will be using pointers whenever you use a dynamic allocation.


    The scanf() functions require that you pass in the address of the variable where the data is to go. That means you either a) pass in the address ofg a local variable or b) dynamically allocate memory for the variable yourself and pass in the address of the allocation. scanf() istself does not do any allocating.

    This applies to all of the scanf()-type functions.

    Comment

    • Parul Bagadia
      New Member
      • Mar 2008
      • 188

      #3
      Originally posted by weaknessforcats
      Dynamic allocations are identified by address. That means you will be using pointers whenever you use a dynamic allocation.


      This applies to all of the scanf()-type functions.
      Thanx for explaining me that; but i want to know that is it only for dynamic memeory allocations?
      As in, in case of static memory allocations; we access it using its address ; and address is nothing but a pointer.

      The scanf() functions require that you pass in the address of the variable where the data is to go. That means you either a) pass in the address ofg a local variable or b) dynamically allocate memory for the variable yourself and pass in the address of the allocation. scanf() istself does not do any allocating.

      Here my point is; in your first case; i.e. when ur passing the address of local variable; thats also a pointer right?........ So ; if is it true that means scanf accesses dynamic locations?
      I dont know whether m i right or not.
      But address of anything is nothing but a pointer.
      and second case is itself of dynamic allocation.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        Just because you have a pointer doesn't mean you've dynamically allocated memory. The only dynamically allocated memory is memory allocated using new or malloc() (or any other *alloc() function). Anything else appears on the stack. The stack is part of memory, so you can still get the address of such data. To get more than a local copy of any memory, you use pointers. The difference is in scoping and when the memory is no longer considered 'safe' to use.

        Comment

        • Parul Bagadia
          New Member
          • Mar 2008
          • 188

          #5
          Originally posted by Laharl
          Just because you have a pointer doesn't mean you've dynamically allocated memory. The only dynamically allocated memory is memory allocated using new or malloc() (or any other *alloc() function). Anything else appears on the stack. The stack is part of memory, so you can still get the address of such data. To get more than a local copy of any memory, you use pointers. The difference is in scoping and when the memory is no longer considered 'safe' to use.
          Thanx for that.
          But i didnt actually get ur last point; when the memory is not considered safe to use? when is that ? And what can happen if we use such unsafe memory?
          And one more thing; like you said static memory is stored on stacks.
          So , where the dynamic memory is stored?

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            The dynamic memory is on 'the heap'. 'The stack' and 'the heap' are just chunks of memory set aside for programs, nothing more. They're just set up differently. Every function has a certain amount of space on the stack for its local variables, and when one function exits, its stack space is deallocated and the next function call will use that memory. Thus, that memory is not guaranteed to hold its value.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Stack memory is managed by the compiler while the heap allocations are managed by you. There may be limits an how much stack memoryis available.

              It does not matter where your data is. All that matters is whether you or the compiler is in control of it.

              When you access memory by address you need a variable to contain that address. That variable is a pointer. So a scanf() has a pointer as an argument for where the data goes.

              When you call it with &var, the stack pointer for scanf() is created with &var as the initial contents. At this point stack and heap are irrelevant.

              Comment

              Working...