Dynamic Memory Allocation consists of many areas, the most known is malloc which comes in a form of:
void *malloc(size_t size);
A simple malloc would be the following which will reserve 5 bytes to every ip.
int *ip;
ip = malloc(5 * sizeof(ip));
Pretty simplistic. sizeof(int) returns the sizeof an integer on the machine, multiply by 5 and malloc that many bytes. The second malloc works because it sends what ip is pointing to, which is an int.
You might say what if we run out of allocated memory during the run-time of our program and need to give more memory! Then we have a function called realloc which will give more memory for that pointer. Realloc comes in the form of:
void *realloc(void *ptr, size_t size);
Some person can quickly say lets just allocate some memory everytime we are empty. So you might have done this:
ip = realloc(ip, sizeof(ip) + sizeof(int)*5);
As every programmer knows... This is quite WRONG. If the realloc on ip fails? ip gets set to NULL, and the previously allocated memory to ip now has no pointer to it. Now we have allocated memory just floating in the heap without a pointer. This is called a memory leak.
Now a better way is to create a temporary pointer. If realloc fails, then it isn't a big problem as we keep our ip pointer on the original memory space.
int *tmp;
if ((tmp = realloc(ip, sizeof(int) * (INITIAL_ARRAY_SIZE + 5))) == NULL) {
// Possible free on ip?
fprintf(stderr, "ERROR: realloc failed");
}
ip = tmp;
As you see, the above function works as a charm! Becarefull when you use malloc and realloc since you can come with many exploits and code faults within your application from memory leakage and bufffer overflows.!
2006-07-02 09:59:24
·
answer #1
·
answered by ? 6
·
0⤊
0⤋
In computer science, dynamic memory allocation is the allocation of memory storage for use in a computer program during the runtime of that program. It is a way of distributing ownership of limited memory resources among many pieces of data and code. A dynamically allocated object remains allocated until it is deallocated explicitly, either by the programmer or by a garbage collector; this is notably different from automatic and static memory allocation. It is said that such an object has dynamic lifetime.
Check my source for more info.
2006-07-01 17:49:59
·
answer #2
·
answered by Anonymous
·
0⤊
0⤋
hhhmm very real .. they purely take position like noooo .. i imagine we shouldn't in any respect imagine about them, yet although they're memories in spite of everything which gonna proceed to be on your mind like for a lengthy lengthy time period so there is no longer something you may do, yet distract your self in the course of something else ... and this happens with me each of the time .. i imagine maximum of my existence i'm residing in my previous .. each from time to time i'm like oohh i might want to be extra present than i'm lol Bq: very few .. my formative years .. yet back there's a lot unhappy suitable to it so i continually attempt to distance myself from them .. Bq: Love memories been the worst in my existence .. then if it became my kin, acquaintances or a guy ... i dont love all people no extra .... each individual betrays and leaves!...
2016-11-30 03:18:01
·
answer #3
·
answered by ganiban 3
·
0⤊
0⤋