1

Following is the method of dynamic memory allocation I learnt ,

int *p = new int; 

i.e.

pointer-variable = new data-type;

However in another programme of linked list, I saw a structure delaration

struct node
{
    int info;
    struct node *next;
}

And declaration of its instance was like

struct node *temp, *s;
temp = new(struct node);

I mean it should be wrong because according to the syntax it should not include struct , it should be like this

node *temp, *s;
    temp = new node ;

Where am I wrong,can anyone please guide me ?

This is the source code ,Refer to code at line no. 125 & 126.

Brijesh
  • 35
  • 6

3 Answers3

1

Your question has nothing to do with dynamic allocation, really.

When you say struct node { ... }; in C++, it creates two type names, node and struct node, both referring to the same type:

node x;
struct node y;  // x and y are variables of the same type

The reason for this slightly weird behavior is that C++ was based on C. In C, struct node { ... }; only creates a single type name, struct node. You have to use typedef manually to get a shorter name that does not include struct:

typedef struct node { ... } node;  // C

C++ wanted to make it easier to create short type names without having to type struct everywhere while at the same time keeping compatibility with existing C code.

(Also, there is a common unix function called stat, which takes a pointer to a struct also called stat:

int stat(const char *, struct stat *);

Here struct stat unambiguously refers to the type, not the function. C++ had to support this syntax to be able to call stat.)

Your code is written in a weird C-like style (which includes the struct keyword everywhere), but new does not exist in C, so it cannot have been actual C.

melpomene
  • 84,125
  • 8
  • 85
  • 148
-1
node *temp, *s;
temp = new node ;

This is the syntax for dynamic memory allocation in C++

However,

struct node *temp, *s;
temp = (node*)malloc(sizeof(struct node));  

This is the syntax used in C In C, the keyword 'struct' has to be written before the structure name.

keyword 'new' is not there in C.

Subhransu
  • 1
  • 2
  • `new` does not exist in C. – melpomene Jan 27 '19 at 06:39
  • The site where I looked stated it as c++ code. Link is here https://www.sanfoundry.com/cpp-program-implement-single-linked-list/ Please see the code at line 125 & 126 – Brijesh Jan 27 '19 at 06:43
  • `temp = malloc (sizeof *temp)` for C. – David C. Rankin Jan 27 '19 at 06:45
  • @Brijesh That code is pretty bad. There's a class that's not really a class (`single_llist` contains no member variables; it's really just a collection of functions that use a global variable), broken memory allocation checks (`temp = new ...; if (temp == NULL)`, but `new` cannot return `NULL`), and many other issues. – melpomene Jan 27 '19 at 06:53
  • @melpomene, Sir I am new to programming & I don't know much, I am just combing the web to learn from any site where I can get content. I don't have much resource to determine how efficient the code is. – Brijesh Jan 27 '19 at 06:57
  • @Brijesh I haven't even mentioned efficiency. "Just combing the web" is not a good way to learn; it means you'll also learn lots of bad habits and even incorrect code that you'll have to unlearn later. There's a [C++ book guide](https://stackoverflow.com/q/388242/1848654) on StackOverflow. – melpomene Jan 27 '19 at 07:12
-1

//USE THE CODE LIKE THIS.....

struct node *temp, *s;
temp = (struct node*) new(struct node);

// as temp is a struct node pointer so we need to typecast it before assigning..

Sohini
  • 1