Linked Lists
-
Hi all, I have a linked list of typedef struct someStruct{ DatasourasRex someData; someStruct *nextElement; }someStruct; so I have a list where 1 of these points to the second and a second points to the 3rd. I wish to remove the second so I have just set the pointer to point to the 3rd. But Id imagine this would give me problems with memory leaks maybe? I have used malloc each time to give me the memory, so how do I deallocate? Thanks in advance
-
Hi all, I have a linked list of typedef struct someStruct{ DatasourasRex someData; someStruct *nextElement; }someStruct; so I have a list where 1 of these points to the second and a second points to the 3rd. I wish to remove the second so I have just set the pointer to point to the 3rd. But Id imagine this would give me problems with memory leaks maybe? I have used malloc each time to give me the memory, so how do I deallocate? Thanks in advance
Use free There's a link to an example at the bottom of the page. In Italy for thirty years under the Borgias they had warfare, terror, murder, bloodshed - but they produced Michelangelo, Leonardo da Vinci and the Renaissance. In Switzerland they had brotherly love, five hundred years of democracy and what did that produce - the cuckoo clock! -- Harry Lime
-
Use free There's a link to an example at the bottom of the page. In Italy for thirty years under the Borgias they had warfare, terror, murder, bloodshed - but they produced Michelangelo, Leonardo da Vinci and the Renaissance. In Switzerland they had brotherly love, five hundred years of democracy and what did that produce - the cuckoo clock! -- Harry Lime
NEVER use free unless you use malloc and NEVER use malloc if you're using C++. Christian Graus - Microsoft MVP - C++
-
Hi all, I have a linked list of typedef struct someStruct{ DatasourasRex someData; someStruct *nextElement; }someStruct; so I have a list where 1 of these points to the second and a second points to the 3rd. I wish to remove the second so I have just set the pointer to point to the 3rd. But Id imagine this would give me problems with memory leaks maybe? I have used malloc each time to give me the memory, so how do I deallocate? Thanks in advance
1. NEVER use free or malloc in C++ 2. Never write your own linked list class for production, use std::list. 3. It's good to write one though, to learn. If you want to remove an item from the list, you delete the someStruct in question, and you take the item that is before it, and change it's nextElement pointer to the item after it. Christian Graus - Microsoft MVP - C++
-
1. NEVER use free or malloc in C++ 2. Never write your own linked list class for production, use std::list. 3. It's good to write one though, to learn. If you want to remove an item from the list, you delete the someStruct in question, and you take the item that is before it, and change it's nextElement pointer to the item after it. Christian Graus - Microsoft MVP - C++
Christian Graus wrote: 1. NEVER use free or malloc in C++ Unless you're overloading operator new :) Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
NEVER use free unless you use malloc and NEVER use malloc if you're using C++. Christian Graus - Microsoft MVP - C++
-
1. NEVER use free or malloc in C++ 2. Never write your own linked list class for production, use std::list. 3. It's good to write one though, to learn. If you want to remove an item from the list, you delete the someStruct in question, and you take the item that is before it, and change it's nextElement pointer to the item after it. Christian Graus - Microsoft MVP - C++
Hi might be doing his college project rite, so using list and other std templates would defeat the perpose of using the list.
-prakash
-
Why? I always automatically use 'new' and 'delete' in C++, but i'm just curious what your 'motivations' are for not using 'malloc' and 'free' in C++. Er zit een korstje op mijn aars.
new
anddelete
use constructors/destructors and let you make full use of OO,malloc
andfree
only control memory. Elaine :rose: The tigress is here :-D -
Why? I always automatically use 'new' and 'delete' in C++, but i'm just curious what your 'motivations' are for not using 'malloc' and 'free' in C++. Er zit een korstje op mijn aars.
If you are using objects, which have constructors and destructors,
malloc()
andfree()
will not work. Thenew
anddelete
operators will, however. Allocating room for other types such asint
,char
, anddouble
will work just fine withmalloc()
andfree()
.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-
Hi might be doing his college project rite, so using list and other std templates would defeat the perpose of using the list.
-prakash
Mr.Prakash wrote: Hi might be doing his college project Yes, that's why I said, it's a good idea to write these things to learn, but once he enters the real world, he shouldn't be using his own list class. Christian Graus - Microsoft MVP - C++