|
-
Oct 15th, 2002, 12:04 PM
#1
Thread Starter
Fanatic Member
linked lists and c-style string question
hello
if i had
NODE *first;
NODE *current;
current = first;
free(current);
will that erase first, or just the pointer to current?
and if i did free(first); then that will erase it right?
same thing with erasing a linked list, if i did
while(first->next!=NULL)
first=first->next
free(first)
that will end up erasing the list right?
now about string
if i had
char *mystring="blah\x00";
char *str2 = mystring;
free(str2);
i can still access mystring? like, will it only free the str2 pointer and not delete mystring?
im kinda confused about those, i think i have a few memory leaks in my program, i just wanna make sure so i dont go around freeing things that i shouldnt
thanks
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Oct 15th, 2002, 01:08 PM
#2
Member
A pointer stores the location in memory of your *insert here* (string, or node).
When you call free(), it releases the memory at the location you specify. So having two pointers pointing to the same thing, and you call free() with one of them... you'll see the same results in both. Oh yah, you should only call free() on an address returned from malloc().
This would work for free up your link list.
Code:
while(first != NULL)
{
current = first->next;
free(first);
first = current;
}
-
Oct 15th, 2002, 02:22 PM
#3
Frenzied Member
delete & new together
Code:
new int whatever;
.......
delete whatever;
free & malloc or calloc together
Code:
char *s;
s=calloc(100,1);
free(s);
void *p;
p = malloc(1000);
free(p);
You don't use free or delete on declared variables
ie.,
Code:
char tmp[100];
.................
free(tmp);
// does not work free returns an error.
-
Oct 15th, 2002, 02:53 PM
#4
Thread Starter
Fanatic Member
hmm, then how do i free the pointer to my original string? im kinda confused on that
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Oct 15th, 2002, 03:00 PM
#5
Frenzied Member
In the code you posted, your pointer "first" is a NULL pointer - it's aimed at nothing.
-
Oct 15th, 2002, 04:53 PM
#6
Thread Starter
Fanatic Member
no that was just an example
here, like in this:
Code:
string cAIBrain::GetResponse(char *strInText){
BLOCK *iter=first;
register int i=0;
bool match;
if(iter!=NULL){ //make sure the first one isnt nul
while(iter!=NULL){ //loop through the entire list
for(i=0;i<iter->say.size();i++){
match=FindPattern(strInText, iter->say[i]);
if(match)
return iter->resp[RndNum(iter->resp.size()-1)];
}
iter=iter->next;
}
}
return misc[RndNum(misc.size()-1)];
}
like, can i free iter, because wont that be there just sitting there? or is it automatically destructed at the end of the function call?
in certain places where im working with strings, i make a copy of the string passed to the function, then use strcpy() to make a copy of it then free the copy before i return a value..thats probably the best way right?
is there a good page you know of with good efficiency techniques? i wanna concentrate on elequent and effecient coding right now rather than learning new things
thanks
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Oct 16th, 2002, 09:18 AM
#7
This is one important thing you must understand:
Assigning one pointer to another doesn't copy the memory location the pointer points to!
If you let this sink in you'll see why you shouldn't free iter in your example.
In the previous example
char *mystring="blah\x00";
char *str2 = mystring;
free(str2);
mystring points to the statically allocated (in the exe embedded) string "blah\x00" (which, by the way, is terminated by two NUL characters, which is a waste of space, and a NUL character is simpler written '\0'). After the second line str2 points to exactly the same memory location. No copying is done except for a single memory address. The third line will fail: a statically allocated string cannot be freed.
Here are a few good ground rules:
1) For every call to malloc there must be exactly one call to free. No more, no less. Likewise, for every call to new there must be exactly one call to delete. (Some functions, like strdup, internally call malloc but never free, you need to call free then). This rule is completly independent of how often you copy this pointer or how much you manipulate the memory.
2) Whenever you have a literal string in your code, like in
char *str = "string";
note that this string cannot be modified. This means you must for example never try things like
str[3] = 'u';
To make the compiler warn you of such things you should always assign literal strings to const pointers:
const char * str = "string";
I'll add more if I think of any.
In your last example the outer if statement is redundant (if(iter != NULL)). Because a while loop checks for the condition before executing the loop body for the first time a NULL pointer would be catched there.
Now take a look at this snippet you posted:
Code:
while(first->next!=NULL)
{
first=first->next;
free(first);
}
What happens here? You have a linked list:
Code:
NODE NODE NODE NODE NULL
next-> next-> next-> next->
Now you start with first (I'll call it iter for less confusion) pointing to the first NODE. You check, the next pointer does not point to NULL, so you enter the loop.
There you set iter to the next pointer (you're missing a node!). Then you delete it. So far so good. BUT iter now isn't NULL, neither does it point to valid memory (it was freed). A bomb waiting to go off.
It does immediatly. The next condition checking tries to dereference the pointer (note that ptr->member is only a shorthand for (*ptr).member ), which points to invalid memory. The OS might or might not complain (DOS wouldn't, windows would), the data might or might not be there (it might have been overwritten by a different thread or, in DOS, by an interrupt handler suddenly popping to life). Anyway you have too much uncertanity and a good chance to have a hard-to-find bug that crashes the app every 200 runs.
Efficiency is closely connected with learning new things. You usually learn new things that make you more efficient.
Much to learn you still have.
Yoda
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 16th, 2002, 05:43 PM
#8
Thread Starter
Fanatic Member
ah ok, thanks for taking the time to explain that CornedBee, that helped alot.
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|