a link list code question..

This is a discussion on a link list code question.. within the C Programming forums, part of the General Programming Boards category; so after this line: Code: root = new node; we put an address to a free block with a size ...

  1. #16
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so after this line:

    Code:
    root = new node;
    we put an address to a free block with a size of node variable

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    free() what you malloc().

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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 11:25 AM.

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    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 11:32 AM.

  5. #20
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    whats delete[]
    ?

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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 12:05 PM.

  7. #22
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    learn them both.

  8. #23
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by robwhit View Post
    Code:
    struct list_el {                         //type struct list_el
    typedef struct list_el item;         //item is alias for struct list_el
    void main() { http://faq.cprogramming.com/cgi-bin/...&id=1043284376
          curr = (item *)malloc(sizeof(item)); http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    i read the melloc casting link

    i understad that in first we needed to do casting to the type of pointer like:
    Code:
    double *p;
    p = (double *)malloc ( n * sizeof ( double ) );
    but after some time iso C was updated and void pointer was created
    so we dont need to do casting just:
    Code:
    double *p;
    p = malloc ( n * sizeof ( double ) );
    i use ansi c not iso C

    what type of malloc code i need to use?
    Last edited by transgalactic2; 10-27-2008 at 12:31 PM.

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by transgalactic2 View Post
    i read the melloc casting link

    i understad that in first we needed to do casting to the type of pointer like:
    Code:
    double *p;
    p = (double *)malloc ( n * sizeof ( double ) );
    but after some time iso C was updated and void pointer was created
    so we dont need to do casting just:
    Code:
    double *p;
    p = malloc ( n * sizeof ( double ) );
    i use ansi c not iso C

    what type of malloc code i need to use?
    ANSI C is ISO C. And void pointers were in the language from the beginning, if I recall correctly.

    In C proper, malloc should never be casted.

  10. #25
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so in ansi C
    i only act like that?
    Code:
    double *p;
    p = malloc ( n * sizeof ( double ) );

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  12. #27
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by robwhit View Post
    learn them both.
    i was told that in C
    when we make malloc to the root its as if we are doing the C++ command "new"

    root=malloc(node);

    is it correct?? (the malloc =new and how i wrote it)

  13. #28
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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)?

  14. #29
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

  15. #30
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Tutorial sample code question
    By Kalleos in forum C Programming
    Replies: 2
    Last Post: 01-16-2009, 12:20 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 06:46 PM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21