Confused with new keyword again...

int x = new int;
Why is this invalid?

What does new actually do?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;


struct Hello{
	Hello(int num){
		*x = num;
	}
	public:
		int *x = new int;
		void printNum(){
			cout << x << endl;
		}
};

Hello *createHello(int input){
	Hello y(5);
	
	return &y;
}

int main() {
	Hello *hello = createHello(5);
	hello->printNum();
	
	return 0;
}


Warning: address of local variable 'y' returned.

What did I do wrong?
The warning you have quoted is about line 20. There, you return that address of y, which will no longer exist once the function exits, resulting in a returned address pointing to memory you don't own.

As a secondary issue, you aren't probably handling the pointer in your Hello object. You need to use new in the constructor and also make sure to use delete in the destructor. You'll also need to avoid ever reassigning that pointer or you'll end up with a memory leak.

Finally, I suspect your printNum() method was meant to print the value at x, not the memory address itself.
What does new actually do?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int x;  // <- this creates an integer named 'x'

int* ptr; // this creates a pointer (to an integer), named 'ptr'

ptr = new int;  // <- 'new int' dynamically creates a new integer.  Since it's dynamic,
  // it doesn't have a name.  For this example, let's call this integer 'foo'.
  // Once 'foo' is created, 'new' returns a pointer to it, which we assign to ptr.
  // So now, ptr "points to" foo.

// At this point we have 3 variables:
//  x  (integer)
//  ptr (pointer to integer)
//  foo (integer)


x = 4;  // <- this assigns 4 to our 'x' integer

*ptr = 5;  // <- this assigns 5 to whatever 'ptr' points to.  Since 'ptr' points to 'foo', this
   //  assigns 5 to 'foo'


// Since dynamically allocated memory is not automatically cleaned up (unless you use smart
//   pointers -- which you should be doing), it is necessary to tell the computer when we're
//   done with that memory so it can be freed.  Therefore every object created with new must
//   be deleted with delete:

delete ptr;  // <- contrary to how this reads, this does not delete ptr.  It actually deletes
  //  whatever ptr points to.  Since ptr points to foo, this actually deletes foo.
  //  Note that we can't simply say 'delete foo;' because 'foo' doesn't actually have a name
  //   and therefore cannot be accessed directly through code. 




int x = new int;
Why is this invalid?


new int will create a new integer (foo) and return a pointer to that new integer. You are trying to assign that pointer to a variable that is not a pointer. Therefore it's a type mismatch and you get a compiler error.



Zhuge answered your other question.
Last edited on
It is important to note that dynamically allocated memory isn't particularly meant to be used with standard library data types as depicted in Disch's example (although, keep in mind, his example is an extremely good one at showing what dynamically allocated memory does).
The following link is to a comment (also by Disch) on an older thread of mine. That comment and the following ones explain exactly where dynamic memory is suitable:
http://www.cplusplus.com/forum/general/140850/#msg744075
Topic archived. No new replies allowed.