![]() |
going from c to c++
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 |
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. |
Re: going from c to c++
nimeshmaheshwari@gmail.com 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). |
Re: going from c to c++
nimeshmaheshwari@gmail.com 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. |
| All times are GMT. The time now is 03:03 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.