Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Pointer (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Dynamic memory allocation=== In some programs, the required amount of memory depends on what ''the user'' may enter. In such cases the programmer needs to allocate memory dynamically. This is done by allocating memory at the ''heap'' rather than on the ''stack'', where variables usually are stored (although variables can also be stored in the CPU registers). Dynamic memory allocation can only be made through pointers, and names – like with common variables – cannot be given. Pointers are used to store and manage the addresses of [[dynamic memory allocation|dynamically allocated]] blocks of memory. Such blocks are used to store data objects or arrays of objects. Most structured and object-oriented languages provide an area of memory, called the ''heap'' or ''free store'', from which objects are dynamically allocated. The example C code below illustrates how structure objects are dynamically allocated and referenced. The [[standard C library]] provides the function [[malloc|<code>malloc()</code>]] for allocating memory blocks from the heap. It takes the size of an object to allocate as a parameter and returns a pointer to a newly allocated block of memory suitable for storing the object, or it returns a null pointer if the allocation failed. <syntaxhighlight lang="C"> /* Parts inventory item */ struct Item { int id; /* Part number */ char * name; /* Part name */ float cost; /* Cost */ }; /* Allocate and initialize a new Item object */ struct Item * make_item(const char *name) { struct Item * item; /* Allocate a block of memory for a new Item object */ item = malloc(sizeof(struct Item)); if (item == NULL) return NULL; /* Initialize the members of the new Item */ memset(item, 0, sizeof(struct Item)); item->id = -1; item->name = NULL; item->cost = 0.0; /* Save a copy of the name in the new Item */ item->name = malloc(strlen(name) + 1); if (item->name == NULL) { free(item); return NULL; } strcpy(item->name, name); /* Return the newly created Item object */ return item; } </syntaxhighlight> The code below illustrates how memory objects are dynamically deallocated, i.e., returned to the heap or free store. The standard C library provides the function [[free()|<code>free()</code>]] for deallocating a previously allocated memory block and returning it back to the heap. <syntaxhighlight lang="C"> /* Deallocate an Item object */ void destroy_item(struct Item *item) { /* Check for a null object pointer */ if (item == NULL) return; /* Deallocate the name string saved within the Item */ if (item->name != NULL) { free(item->name); item->name = NULL; } /* Deallocate the Item object itself */ free(item); } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)