Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. IT & Infrastructure
  4. Allocate Memory for Global Pointers

Allocate Memory for Global Pointers

Scheduled Pinned Locked Moved IT & Infrastructure
c++performancequestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    traapons
    wrote on last edited by
    #1

    Hi, Is it correct to allocate and free memory for Global Pointers and also for global variable. eg code: ................. test.h typedef struct { int a; }test; ............... test.cpp include test.h int main() { test *T1; .. .. T1 = (struct test*)malloc(sizeof(struct test)+1);//??? .. free(T1);//??? } As far as my knowledge global variables is not in HEAP But Malloc will allocate space in HEAP. To ME it seems confusing... Can u guyz give some clear Ideas regarding this... Urs, -Pons ----------------------- The greatest of faults, I should say, is to be conscious of none.

    J 1 Reply Last reply
    0
    • T traapons

      Hi, Is it correct to allocate and free memory for Global Pointers and also for global variable. eg code: ................. test.h typedef struct { int a; }test; ............... test.cpp include test.h int main() { test *T1; .. .. T1 = (struct test*)malloc(sizeof(struct test)+1);//??? .. free(T1);//??? } As far as my knowledge global variables is not in HEAP But Malloc will allocate space in HEAP. To ME it seems confusing... Can u guyz give some clear Ideas regarding this... Urs, -Pons ----------------------- The greatest of faults, I should say, is to be conscious of none.

      J Offline
      J Offline
      Jun Du
      wrote on last edited by
      #2

      traapons wrote:

      As far as my knowledge global variables is not in HEAP But Malloc will allocate space in HEAP.

      You are actually right on both, but you are confused between a pointer variable and the memory it points to.

      traapons wrote:

      test *T1;

      You declare a pointer variable on the stack. This declaration also says that this variable will be able to point to an instance of test structure.

      traapons wrote:

      T1 = (struct test*)malloc(sizeof(struct test)+1);//???

      Now you allocate a memory chunk on the heap and assign its address to T1.

      traapons wrote:

      free(T1);//???

      Finally, you use the pointer variable to free/deallocate that memory chunk on the heap. After the deallocation, T1 is still the original variable on the stack - it just doesn't point to that memory address any more. Hope this helps. Best, Jun

      T 1 Reply Last reply
      0
      • J Jun Du

        traapons wrote:

        As far as my knowledge global variables is not in HEAP But Malloc will allocate space in HEAP.

        You are actually right on both, but you are confused between a pointer variable and the memory it points to.

        traapons wrote:

        test *T1;

        You declare a pointer variable on the stack. This declaration also says that this variable will be able to point to an instance of test structure.

        traapons wrote:

        T1 = (struct test*)malloc(sizeof(struct test)+1);//???

        Now you allocate a memory chunk on the heap and assign its address to T1.

        traapons wrote:

        free(T1);//???

        Finally, you use the pointer variable to free/deallocate that memory chunk on the heap. After the deallocation, T1 is still the original variable on the stack - it just doesn't point to that memory address any more. Hope this helps. Best, Jun

        T Offline
        T Offline
        traapons
        wrote on last edited by
        #3

        hi, Thx for your reply. Urs, -Pons ----------------------- The greatest of faults, I should say, is to be conscious of none.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups