Will this cause a memory leak? Would the best way to be like :Code:#include <stdio.h> #include <malloc.h> char* Hello(); int main() { printf("%s\n",Hello()); return 0; } char* Hello() { char* pHello = (char*)malloc(6); pHello = "HELLO"; return pHello; };
Is there ANY other way I can return a character string from a function without it losing its memory address, apart from using std::string? Sorry, just haven't quite grasped the concept..Code:#include <stdio.h> #include <malloc.h> char* Hello(); int main() { char* sHelloString = Hello(); printf("%s\n",sHelloString); free(sHelloString); return 0; } char* Hello() { char* pHello = (char*)malloc(6); pHello = "HELLO"; return pHello; };
And if I get a memory leak, what is the worst that could happen outside of my program? Because last time I did something before I learned about the free() call, I made a program that had LOTS of memory leaks and my computer started acting weird, stuff wouldn't copy,mouse would randomly click etc and I had to reboot. Just a coincidence? Or from memory leaks. Thanks.




Reply With Quote