going from c to c++

N

nimeshmaheshwari

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
 
P

Pavel Shved

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.
 
O

Ondra Holub

(e-mail address removed) 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).
 
R

Rolf Magnus

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top