-2

I've started learning the linked list concept. I've came across some code. It would be very helpful if somebody explain what happens with the code below. I've tried searching but couldn't find a proper answer.

 struct node *new1;
 new1 = (struct node*)malloc(sizeof(struct node));
Allan
  • 110
  • 9
  • [Obligatory](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)... – ggorlen Jun 25 '19 at 17:44
  • 3
    @Allan Nothing will happen.:) In C this statement new1 = (struct node*)malloc(sizeof(struct node)); is equivalent to new1 = malloc(sizeof(struct node)); – Vlad from Moscow Jun 25 '19 at 17:44
  • 1
    Not really sure what's being asked here. – CoffeeTableEspresso Jun 25 '19 at 17:45
  • 1
    Nothing in the buffer changes, you are just pointing new1 to whatever garbage happens to be in it. – Dave S Jun 25 '19 at 17:45
  • 2
    In C casting of pointers is always performed at compile-time. All it does is telling the compiler "I know this thing is of type A, but I want it to be type B. Don't interfere, ***I know what I'm doing!***" That last emphasized part is really important to keep in mind for all casting. – Some programmer dude Jun 25 '19 at 17:45
  • 1
    @Someprogrammerdude Not always. Casting between integer and floating types performs data conversion. – Barmar Jun 25 '19 at 17:46
  • oh.... so new 1 is pointing a garbage – Allan Jun 25 '19 at 17:46
  • @Someprogrammerdude not strictly true. `float` to `int` casts involve stuff happening at run time. – CoffeeTableEspresso Jun 25 '19 at 17:46
  • 2
    But pointer type casts work as you describe, it just describes the intent. – Barmar Jun 25 '19 at 17:46
  • @Barmar not if you are just assigning a pointer to the address of it. – Dave S Jun 25 '19 at 17:47
  • 1
    @DaveS LIke I said, pointer casts don't change anything. – Barmar Jun 25 '19 at 17:48
  • @Barmar Rephrased. – Some programmer dude Jun 25 '19 at 17:49
  • Sorry, I meant to reply to @CoffeeTableEspresso who described `int a = (int) float b` rather than `int* a = (int*) & float b` – Dave S Jun 25 '19 at 17:50
  • @DaveS I meant the following will involve instructions at runtime: `int b = …; float a = (float) b;`. `float *` to `int *` doesn't need instructions at runtime. – CoffeeTableEspresso Jun 25 '19 at 17:57
  • I guess I should mention that pointer casts _could_ also involve a cast at runtime according to the standard. If you're on a word addressable machine with smaller `int *`s than `char *`s, casting an `int *` to a `char *` will involve "padding out" the `int *` to be the same size as a`char *`. This isn't likely to be an issue in practice though. – CoffeeTableEspresso Jun 25 '19 at 17:59

2 Answers2

2
struct node *new1;

This allocates a local variable, probably on the stack, of type "pointer to struct node".

new1 = (struct node*)malloc(sizeof(struct node));

This allocates a suitably-aligned chunk of memory large enough to hold a struct node, probably from the heap, and sets new1 to point to that newly-allocated chunk. The chunk will remain allocated until the process terminates or the block is freed. The cast is not necessary.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 1
    Right. It's a matter of preference. Personally, I prefer it because it makes it easier to port the code to C++ and it makes it easier to catch a mismatch between the number of bytes allocated and the use of the pointer. But I'm in the minority. – David Schwartz Jun 25 '19 at 17:49
  • @DavidSchwartz “it makes it easier …” — There’s an objectively better way to avoid this mismatch, which I’m sure you’re aware of, so why not mention it? `struct node *new1 = malloc(sizeof *new1);` – Konrad Rudolph Jun 25 '19 at 18:43
  • @KonradRudolph It's personal preference. I find that much less readable. When I look at the parameter to `sizeof`, what I want to know is the type, and I find it highly annoying and distracting when I'm directed elsewhere to figure it out. It's not always in the same line, and even when it is, being forced to rescan is unpleasant. This mismatch is now easier to detect but at the cost of the code being less readable. – David Schwartz Jun 25 '19 at 18:54
  • @DavidSchwartz It’s less (= not at all) preference and more habit. To be clear, the difference is that “preference” is routinely used to pretend that something is subjective when it’s really not. You may not be *used* to this idiom but there’s no doubt that it encapsulates the required information more concisely, clearer, and with less irrelevant distraction. It’s ridiculous that highly intelligent programmers argue this such points. Case in point, you’re not forced to “rescan” anything; that’s pure post-hoc rationalisation. – Konrad Rudolph Jun 25 '19 at 19:09
0

Memory returned by malloc initially has no type. When a pointer is then used to write to that memory, the memory (up to the size of the given type) becomes an object whose type is the type of the dereferenced pointer.

dbush
  • 205,898
  • 23
  • 218
  • 273