Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   going from c to c++ (http://www.velocityreviews.com/forums/t460007-going-from-c-to-c.html)

nimeshmaheshwari@gmail.com 01-24-2007 02:52 AM

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


Pavel Shved 01-24-2007 03:18 AM

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.


Ondra Holub 01-24-2007 08:49 AM

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


Rolf Magnus 01-24-2007 10:01 AM

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 04:45 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.