so after this line:
we put an address to a free block with a size of node variableCode:root = new node;
so after this line:
we put an address to a free block with a size of node variableCode:root = new node;
free() what you malloc().
i free every memory allocation(in this case "new" command)
just by adding
free()
in the end of a code
even if there have been many "new" or "malloc" or "calloc"
?
Last edited by transgalactic2; 10-27-2008 at 10:25 AM.
free() what you malloc() calloc() or realloc()
delete what you new
delete [] what you new[]
http://www.cplusplus.com/reference/c...dlib/free.html
http://www.cplusplus.com/doc/tutorial/dynamic.html
Last edited by robwhit; 10-27-2008 at 10:32 AM.
whats delete[]
?
i looked at the delete example the are marked
i saw these command on c++
are commands like >> cin
present in normal C ?
also it says
"Operators new and delete are exclusive of C++"
i need a linked list tutorial only for C language
i find always articles mixed with C++
i cant use C++ command
but on the other hand i know that concepts like "new" is the basis for linked list
where can i find such stuff only on C ?
Code:// rememb-o-matic #include <iostream> #include <new> using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; }
Last edited by transgalactic2; 10-27-2008 at 11:05 AM.
learn them both.
i read the melloc casting link
i understad that in first we needed to do casting to the type of pointer like:
but after some time iso C was updated and void pointer was createdCode:double *p; p = (double *)malloc ( n * sizeof ( double ) );
so we dont need to do casting just:
i use ansi c not iso CCode:double *p; p = malloc ( n * sizeof ( double ) );
what type of malloc code i need to use?
Last edited by transgalactic2; 10-27-2008 at 11:31 AM.
so in ansi C
i only act like that?
Code:double *p; p = malloc ( n * sizeof ( double ) );
Yes, only if you are using C++ do you need to cast malloc() [and in C++, you _SHOULD_ be using new/delete rather than malloc/free]. Support for malloc in C++ is still there so that it is possible to compile old code written in C with the C++ compiler for backwards compatibility.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Sorta... I only imagine your code is describing node as a datatype. malloc() takes in a numeric value of the number of bytes you wish to have allocated. So perhaps root = malloc(sizeof node)?
thanks
No problem. new is a little different in that new knows how big an object or array should be. Also, new calls the C++ ctor. Such things do not exist in C.