ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I was writing a program which included a type char ** variable. When I finished using it I tried to delete it for later use with the following code:
for (i = 0; chr[i]; i++)
delete chr[i];
Then I ran the program in gdb debuger, after the lines above, I tried to print out the memory addresses of the variable with the indexes I had used, what I had were still real memory addresses like 0x804e038 and 0x804e058, and they contain some freaky contents like "0 \004\b". Shouldn't their addresses be 0x0 after the deletion? How can I delete them completely so that I can reuse the pointer without worrying about the previous values?
I'm not sure about the delete operator in c++, but I imagine it's very similar to the free() function in c. Free does not change the value of a pointer, it only marks the memory as available for reallocation. Generally, I see calls to free in the form of:
Originally posted by NeoAnderson thank you, by adding chr[i] = NULL; it works. btw, why the addresses of chr[3] and chr[4] were not 0x0 but 0x11 and 0x31 before I used them?
malloc does not init allocated memory, have a look at calloc
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.