going from c to c++

Discussion in 'C++' started by nimeshmaheshwari@gmail.com, Jan 24, 2007.

  1. Guest

    Hi

    I need some help, in syntax, going from c to c++

    how will the following syntax be written in c++ using the new statement

    if ((new_table->table = malloc(sizeof(list_t *) * size)) == NULL) {
    return NULL;

    here are the struct definitions

    struct createList
    {
    int data;
    createList *list;
    };

    struct createTable
    {
    int table_size;
    createList **table;
    };

    new_table is an instance of createTable.

    Any help will be appreciated

    thanks

    nimesh
     
    , Jan 24, 2007
    #1
    1. Advertising

  2. Pavel Shved Guest

    going from c to c++ requires something more than learning syntax

    > I need some help, in syntax, going from c to c++

    One of the main misleadings in going from C to C++ is thinking that
    they differ by syntax only :-D.
    Feel free to use malloc in C-like code -- there is absolutely no sence
    in new statement in your code. Or use classes like std::list ot
    std::vector instead.
     
    Pavel Shved, Jan 24, 2007
    #2
    1. Advertising

  3. Ondra Holub Guest

    napsal:
    > Hi
    >
    > I need some help, in syntax, going from c to c++
    >
    > how will the following syntax be written in c++ using the new statement
    >
    > if ((new_table->table = malloc(sizeof(list_t *) * size)) == NULL) {
    > return NULL;
    >
    > here are the struct definitions
    >
    > struct createList
    > {
    > int data;
    > createList *list;
    > };
    >
    > struct createTable
    > {
    > int table_size;
    > createList **table;
    > };
    >
    > new_table is an instance of createTable.
    >
    > Any help will be appreciated
    >
    > thanks
    >
    > nimesh


    If you are compiling code whic uses malloc with C++ compiler, you have
    to typecast result of malloc to correct type, because malloc returns
    void*. You do not need to do anything else (in this case).
     
    Ondra Holub, Jan 24, 2007
    #3
  4. Rolf Magnus Guest

    wrote:

    > Hi
    >
    > I need some help, in syntax, going from c to c++
    >
    > how will the following syntax be written in c++ using the new statement
    >
    > if ((new_table->table = malloc(sizeof(list_t *) * size)) == NULL) {
    > return NULL;


    new_table->table = new createList*[size];

    A null pointer check is not needed, since operator new never returns a null
    pointer. It throws an exception if it can't allocate.

    > here are the struct definitions
    >
    > struct createList
    > {
    > int data;
    > createList *list;
    > };
    >
    > struct createTable
    > {
    > int table_size;
    > createList **table;
    > };
    >
    > new_table is an instance of createTable.
     
    Rolf Magnus, Jan 24, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer
    Replies:
    1
    Views:
    604
    VB Programmer
    Jul 10, 2003
  2. Navin
    Replies:
    3
    Views:
    654
    Navin
    Jul 28, 2003
  3. Paul

    I am going to kill mysql

    Paul, Aug 4, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    2,642
    Kevin Spencer
    Aug 5, 2003
  4. Kathy Burke
    Replies:
    6
    Views:
    631
    Munsifali Rashid
    Aug 13, 2003
  5. Jay Douglas
    Replies:
    0
    Views:
    407
    Jay Douglas
    Aug 28, 2003
Loading...

Share This Page