-6

I'm new in c++.

There is a struct named node.

Why is

node* a = NULL;

a = new node;

possible, but

double* d = NULL;

d = 12.0;

is not? I just don't get WHY this doesn't work...

Thanks a lot

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
Gabonica
  • 63
  • 2
  • 2
    `new` returns a pointer, `12.0` is not one. – MrPromethee Jun 08 '19 at 13:04
  • Because `12.0` isn't a pointer? – πάντα ῥεῖ Jun 08 '19 at 13:04
  • 3
    `d = new double(12.0);` –  Jun 08 '19 at 13:04
  • 6
    You should get [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn from it. – Yksisarvinen Jun 08 '19 at 13:04
  • You can assign 0 because it's integer. You cannot asign double to a pointer no matter to what the pointer points to. `char* ptr = 0.0` doesn't work either. `char* ptr = 0` will. – Quimby Jun 08 '19 at 13:27
  • Your code is not creating a `double` in the "same way". The "same way" (as the example with a `node`) would be `double *d = NULL; d = new double;` (`d` will point at a dynamically allocated `double, and the value of `*d` will be uninitialised) or `double *d = NULL; d = new double(12.0);` (which will initialise `*d` to the value of `12.0`. – Peter Jun 08 '19 at 13:52
  • Note that `a = node();` ”doesn’t work” in exactly the same way. – molbdnilo Jun 08 '19 at 14:04
  • @Quimby "*You can assign 0 because it's integer"* - more accurately, because the standard specifically allows an integer *literal* 0 to be assigned to a pointer, for backwards compatibility with earlier C++ versions that use 0 for NULL. You can't assign any other integer literals, or an integer variable, to a pointer without using a cast. – Remy Lebeau Jun 08 '19 at 15:38
  • @RemyLebeau Yes, thank you, that would be more correct. – Quimby Jun 08 '19 at 15:39

4 Answers4

4

Here you go:

double* d = NULL;

d = new double;

*d = 12;

Remember to delete d when you are done with it.

Richard Critten
  • 2,138
  • 3
  • 13
  • 16
  • So it's necessary to use "new" in this context? – Gabonica Jun 08 '19 at 13:06
  • 1
    @Gabonica Why are you using raw pointers at all? – πάντα ῥεῖ Jun 08 '19 at 13:08
  • I'm not using them in a project, it's for a lecture. I just don't get why creating a whole new object using the pointers name works, but creating a double without the word new doesn't. In addition, I don't get why it is possible to create a pointer to double and give it a value by using "&", but it is not possible to give a pointer to node a value (an already existing node) by using "&". I'm at the very beginning and I'm still figuring out how pointers work exactly... – Gabonica Jun 08 '19 at 13:12
  • @Gabonica No. You don't need to use new. What you need is 1. An object 2. The pointer must point to the object 3. Then you can indirect through the pointer to access the object. New is just one way of creating an object. Defining a variable is another. – eerorika Jun 08 '19 at 13:14
  • 2
    Please don't teach them to use NULL, use nullptr. – Quimby Jun 08 '19 at 13:26
0

You can create struct or doubles with new. You can create structs or doubles without new. There's no difference between structs and doubles (in this regard).

node* a = new node;
double* b = new double(12.0);

node c;
double d = 12.0;

a and b have been created with new, c and d have been created without new.

john
  • 85,011
  • 4
  • 57
  • 81
  • Ahh, ok So if i want to create an object, that's possible with a pointer to an adress using the new-operator. If I want to create a variable, thats not possible with a pointer but by definition. Right? – Gabonica Jun 08 '19 at 13:19
  • More or less, but variables are objects too. The important difference here is the *lifetime* of the object created. Objects created with `new` live until they are `deleted`. Objects created by declaring variables live until the variable goes out of scope. – john Jun 08 '19 at 13:24
0

for type double you are initializing to NULL and then making it to point to 12.0, which is totally wrong. To write into the memory that your pointer points to you have to deference it: *d and to deference d you have to make sure that it actually points to a valid point in memory. using a new must come with a delete when you are finished. You may use smart pointers instead to not to deal with memory leaks. Here is an example:

#include <memory>
#include <iostream>

int main() {
    std::unique_ptr<double> a;
    a.reset(new double);
    *a.get() = 12;
    std::cout <<*a.get();
}

see the compile here: https://godbolt.org/z/zHfE8O

Oblivion
  • 7,176
  • 2
  • 14
  • 33
0

In this code snippet

node* a = NULL;

a = new node;

the type of the expression new node is node *. So the left and right sides of the expression statement

a = new node;

has the same type. That is you can assign an object of the type node * with another object of the same type node *.

In this code snippet

double* d = NULL;

d = 12.0;

the type of the variable d is double * but the type of the float literal 12.0 is double.

So the left and right sides of the expression statement

d = 12.0;

have different types and there is no implicit conversion from the type double to the type double *.

So the compiler will issue an error.

If you want to write a value to a memory pointed to by the pointer d you have at first to allocate the memory where you are going to write the value 12.0.

You can do it the following way

double* d = new double;

*d = 12.0;

Or you can do this in one line

double *d = new double( 12.0 );

Consider a situation with the structure node.

Let's assume that node is declared the following way

struct node
{
    double value;
    node *next;
};

and there is a declaration

node *a = NULL;

So this statement

a = new node;

only allocates memory to the node but does not sets values for its data members.

So to set values for the node you can write after allocating memory

( *a ).value = 12.0;
( *a ).next  = NULL;

or (that is the same)

a->value = 12.0;
a->next  = NULL;

All this also can be done in one line

node *a = new node { 12.0, NULL };
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335