-2

The line:

struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));

Why does the new struct being instantiated involve malloc (allocating the blocks of memory for the size of the struct) ?

Also, is the re-declaration of struct redundant?

Wouldn't the following accomplish the same task?

Node* newNode = new Node; 

MODE CODE BELOW:

#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int data;
    struct Node* next;
};

struct List
{
    struct Node *head;  // pointer to head node of list
};

//creates a new list Node
struct Node* newListNode(int data)
{
    struct Node* newNode =
            (struct Node*) malloc(sizeof(struct Node));
    newNode->dest = dest;
    newNode->next = NULL;
    return newNode;
}  
Ubez
  • 145
  • 2
  • 9
  • 4
    Because it is written in C and C doesn't know `new`, and may require `struct` all over the place. – juanchopanza Mar 11 '14 at 21:24
  • 5
    This isn't *idiomatic* C++ but it is *legal* C++. Probably the code was written by (1) someone who knew C better than C++, or (2) by someone who wanted to use this source code in a C library as well as C++, or (3) some similar situation. **Ask the person who wrote the code if you want to know why they wrote it that way**. We're not mind readers. – Eric Lippert Mar 11 '14 at 21:25
  • this code looks like pure C to me – Bryan Chen Mar 11 '14 at 21:27
  • 2
    If it were pure C then it wouldn't (or at least shouldn't) have the casts on the result of malloc. – Paul R Mar 11 '14 at 21:30
  • @Paul: Rather say "wouldn't need". I'd argue that C code *should* be written for conformance to the C++ rules. A C++ compiler is one of the cheapest lint tools you can get and generally quite effective at finding type safety violations which the C compiler happily accepts. – Ben Voigt Mar 11 '14 at 21:34
  • @Ben Voigt: the cast can [hide a serious problem though](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) (forgetting to `#include `) so it should not be used in C. – Paul R Mar 11 '14 at 21:36
  • @PaulR: Even C compilers have warnings for usage of undeclared functions. And C++-compiler-as-lint will reject it as an outright error. – Ben Voigt Mar 11 '14 at 21:38
  • @BenVoigt: There are some fundamental incompatibilities with C and C++. Just for example, the lack of the tag namespace in C++ means that the following valid C code won't compile as C++: `struct a { int x; }; struct b { int y; }; typedef struct b a; int main() { a z; z.y = 5; }` – dreamlax Mar 11 '14 at 21:48
  • @dreamlax: That's legal C code, but not good code. IMO, this is a case to say "thank you" to the C++ compiler for catching the confusing abuse of the name `a` associated with multiple structures. – Ben Voigt Mar 11 '14 at 21:54
  • 2
    @BenVoigt: Regardless, it is not always suitable to compile C as C++. Fundamental differences in semantics such as the previously mentioned tag namespace make compiling C as C++ suitable in trivial situations at best. `const` identifiers at file scope have external linkage in C, but not in C++. `sizeof('a')` produces different results, etc. etc. – dreamlax Mar 11 '14 at 22:09
  • @dreamlax: Indeed there are a fair number of subtle and not-so-subtle differences. And taking advantage of any of them just makes it that much harder when you have to find a new programmer for your team, or read the code when you're tired, etc. The only C++-incompatible features of C that I can name that add more value than any workaround are VLAs and designated initializers, and I still question whether they add enough value to justify losing the C++ type checker. – Ben Voigt Mar 11 '14 at 22:16

1 Answers1

1

You asked

Wouldn't Node* newNode = new Node; accomplish the same task?

No, this is not equivalent in some very important ways. If part of the interface (contract) of this function is that it returns a pointer that can be passed to free(), then implementing it with new would violate that contract.

Additionally, the repeated redundant use of the struct keyword suggests that an effort has been made to ensure this code compiles correctly both as plain C and as C++.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Actually, the repeated redundant use of the struct keyword suggests an effor to ensure this code compiles correctly in a C++ compiler. C compilers cast automatically void* to any pointer type. – ebasconp Mar 11 '14 at 22:36
  • @oopscene: This isn't C code. According to the question, it appears in a C++ compilation unit. – Ben Voigt Mar 11 '14 at 22:38