folks i had a question regarding to memory-things in C
please see following code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *tesaja(char *data)
{
char *tmp = (char*)malloc(sizeof(char) * strlen(data));
tmp = data;
return tmp;
}
int main()
{
//area 1
char *wew = tesaja("bud开发者_运维技巧ipergikepasar");
printf("nilai wew : %s\n",wew);
//area 2
wew = tesaja("kepasarbudisedangpergi");
printf("nilai wew : %s\n",wew);
return 0;
}
and showing output like following :
nilai wew : budipergikepasar
nilai wew : kepasarbudisedangpergi
and the only question i had is, is the memory in area 1 will automatically deallocated and replacing with the new one ?
thx in advance
The line "tmp = data" is not doing what you think it is. In C, strings are copied using strcpy(). C strings are arrays of char elements. That line is setting the variable tmp to the value of the variable data, not copying the char's like you think.
The result is you lose your only reference to the memory allocated by malloc() and cause a memory leak.
Hold on. Consider replacing:
char *tmp = (char*)malloc(sizeof(char) * strlen(data));
tmp = data;
return tmp;
with:
char *tmp;
tmp = (char*)malloc(sizeof(char) * strlen(data));
if (tmp == NULL) {Do some error handling because you are out of heap memory};
strcpy(tmp,data);
return tmp;
To answer your question, each time you successfully call malloc(), you will be given a pointer that points to a newly identified block of free heap memory.
The way your code is written
char *wew = tesaja("budipergikepasar");
.
.
.
wew = tesaja("kepasarbudisedangpergi");
Yuo will "lose track" of where tesaja("budipergikepasar") is in heap RAM when wew is overwritten by the second call to tesaja. You will then be stuck with a "memory leak" because you no longer have a pointer to the block of RAM containing "budipergikepasar" and you will not be able to call free() with "budipergikepasar"'s location. Consider using your code like this:
if (wew != NULL) {
free(wew);
}
wew = tesaja("budipergikepasar");
.
.
.
if (wew != NULL) {
free(wew);
}
wew = tesaja("kepasarbudisedangpergi");
Using malloc() and free() this way will help you to make use of available RAM in a more efficient fashion. There are some other ways to handle memory management, but I laid things out this way to highlight the importance of keeping track of what you have allocated with malloc() and then freeing up that space with free() when you are ready.
hope that helps -
Perry
is the memory in area 1 will automatically deallocated
No. You shall call free()
This program is completely wrong.
tmp=data just assigns static character sequence declared in main to tmp, and leaks allocated memory.
What do you want to accomplish?
Right now, you're allocating space for tmp
, but then you are switching the reference to tmp
to be that of data
. Your code should instead use strcpy
, like so, to avoid memory leaks:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *tesaja(char *data)
{
char *tmp = (char*)malloc(sizeof(char) * strlen(data));
tmp = strcpy(tmp, data);
return tmp;
}
int main()
{
//area 1
char *wew = tesaja("budipergikepasar");
printf("nilai wew : %s\n",wew);
//area 2
wew = tesaja("kepasarbudisedangpergi");
printf("nilai wew : %s\n",wew);
return 0;
}
精彩评论