LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2005, 10:13 PM   #1
NeoAnderson
Member
 
Registered: May 2002
Location: Providence, RI
Distribution: Ubuntu
Posts: 78

Rep: Reputation: 15
pointer usage


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?
 
Old 09-07-2005, 10:21 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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:
Code:
char *a=malloc(5);
free(a);
a=NULL;
 
Old 09-07-2005, 10:48 PM   #3
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Code:
for (i = 0; chr[i]; i++) {
  delete chr[i];
  chr[i] = NULL;
}
 
Old 09-08-2005, 12:51 AM   #4
NeoAnderson
Member
 
Registered: May 2002
Location: Providence, RI
Distribution: Ubuntu
Posts: 78

Original Poster
Rep: Reputation: 15
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?

Last edited by NeoAnderson; 09-08-2005 at 12:54 AM.
 
Old 09-08-2005, 01:00 AM   #5
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Quote:
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
getting realtime info on memory usage-cpu and harddrive usage steering Linux - Newbie 5 03-03-2005 08:43 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 04:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration