new operation

This is a discussion on new operation within the C Programming forums, part of the General Programming Boards category; What is the equivalent of new in C?. I apologize for these questions. I am not that familiar with linked ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    56

    new operation

    What is the equivalent of new in C?. I apologize for these questions. I am not that familiar with linked lists.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    malloc() is the equivalent of new , and free() is the equivalent of delete . For example in C:
    Code:
    head = malloc( sizeof(*head) );
    And there's also calloc().

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    calloc() is meant for allocating arrays. It also sets the allocated memory to zero, while malloc() doesn't.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char *str = malloc(sizeof(char) * 80);
    
    strncpy(str, argv[0], 80);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    Be careful with that code. If strlen(argv[0]) is greater than 80 then str will end up without a terminating '\0'.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to change this simple opeation into power operation..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 12-20-2008, 02:17 PM
  2. Replies: 5
    Last Post: 12-04-2008, 07:15 PM
  3. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  4. Help w/ program
    By Newbie96 in forum C++ Programming
    Replies: 3
    Last Post: 02-24-2003, 01:55 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

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