Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I found that in the parts of the code is leak memory. Please help me fix this:
C#
inline string Change(char *pointer) {
    string str;
    char temp[32] = "";

    sprintf(temp,"%c:%c:%c:%c:%c:%c", //line 1
        temp[0],temp[1],temp[2],
        temp[3],temp[4],temp[5],
    );

    str = temp;
    return str;
}
Posted
Comments
Richard MacCutchan 26-Nov-11 4:50am    
What is the point of the sprintf() command in your code, and where is the memory leak?
Chuck O'Toole 26-Nov-11 11:52am    
There is no leak in that code. How'd you determine that there was?

That's not a leak - that's a hanging reference.
C#
char temp[32] = "";
Creates an array of 32 characters on the stack.
C#
str = temp;
return str;
Returns the pointer to the array.
Ok, this is inline code, but when you exit that routine it is still a stack reference you are using. If you refer to that string outside the routine in which it was constructed, then it points to memory that is either unused, or is in use by a different function. Always allocate such data from the heap - not the stack.
 
Share this answer
 
Comments
Richard MacCutchan 26-Nov-11 5:17am    
str is a STL string so it can be returned successfully.
OriginalGriff 26-Nov-11 5:22am    
:DOH:
Coffeeeee....I need more cofffffeeeeeee....
There is no memory leak in function Change()

But check if the char* you passed to function Change() is pointing to dynamically allocated before calling function Change(char* pointer);

And you didn't used that char *pointer in you whole function, make sure logical correctness of his function.

you can use std::auto_ptr for avoiding memory leak.Boost smart pointers could be better alternative.

Good luck. :)
 
Share this answer
 
Comments
BrainlessLabs.com 27-Nov-11 23:48pm    
I agree. Memory leaks are tricky. Go and check for char* pointer in your code to determine how this is handled. You can use a smart pointer instead of it. But before using a smart pointer be clear of the ownership issue.
There is no memory leak in function Change()

But check if the char* you passed to function Change() is pointing to dynamically allocated before calling function Change(char* pointer); if it is dynamically allocated then don't forget to free/delete that.

And you didn't used that char *pointer in you whole function, make sure logical correctness of your function.

you can use std::auto_ptr for avoiding memory leak.Boost smart pointers could be better alternative.

Good luck. :)
 
Share this answer
 
Apart from the previously mentioned issues, your function will always return an empty string:
char temp[32] = "";

This reserves a char array on the stack and initializes it with the empty string, i. e. temp[0] = 0;

C++
sprintf("%c:%c:%c:&c:%c:%c", temp[0],
    // ...
);

This command copies temp[0] to temp[0], so it stays at 0. (of course, you might in fact have meant to copy pointer[0] into temp[0] here?)

C++
str = temp;

This assigns the string represented by temp to str. Since temp[0] == 0, temp represents the empty string, so str will be empty too.

Maybe you are mistaking the term 'memory leak' with the seemingly inexplicable vanishing of your input? Just so we can be sure we're talking about the same things: This is the definition of a memory leak[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900