whats wrong with this code? run-time error

Hi
Twilight zone on review here...what am I doing wrong? Compiles fine, but throws run-time error (has to closed).

Want to assign 7 to ptr...

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>

using namespace std;

int main()
{
	int x = 5;
	int * ptr;
	*ptr = x;

	return 0;
}



run-time error
I'm not getting a run-time error. Works for me.
Using cpp.sh with all 3 warnings on:

In function 'int main()':
 9:10: warning: 'ptr' is used uninitialized in this function [-Wuninitialized]


Always initialise your variables. If you have warnings, then you a problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>

//using namespace std;

int main()
{
	int x = 5;
	int* ptr = &x;
	//*ptr = x;

       std::cout << "ptr points to this value " << *ptr << "\n";

	return 0;
}
Last edited on
closed account (48T7M4Gy)
Another way ...
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>

int main()
{
	int x = 5;
	int *ptr = nullptr; // initialized but still to be decided
	ptr = &x;
	
	std::cout << x << ' ' << ptr << ' ' << *ptr << ' ' << &x << '\n';

	return 0;
}
And yet another way?

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main()
{
    int x = 25;
    int *p_ptr = new(int);
    *p_ptr = x;
    std::cout<<*p_ptr<<std::endl;
    
return 0;
}


Can someone explain what I did to make it work?
int *p_ptr = new(int);

I don't really understand myself. I know that *p_ptr was somehow initialized here.
Last edited on
What I was initially confused on was the way you declared the dynamically allocated int, by saying "new(int)" instead of "new int" or "new int()". After quickly running your code I think your syntax is just another valid way of writing the two ones that I knew of.

Since that's just a quick lookup my guess is that you're question is just what the new operator does and you haven't yet learned about dynamic memory. I'm sure you can find plenty of tutorials or forums about it on this site; http://www.cplusplus.com/doc/tutorial/dynamic/ seems to be a pretty good start. I don't think I can summarize dynamic memory concisely enough for one post if you haven't learned about it yet so I recommend reading the tutorial a bit and then asking specific questions if you have any. I also should make sure you're clear on when *p_ptr is initialized since you asked; "int *p_ptr = new(int)" initializes p_ptr, and "*p_ptr = x" initializes *p_ptr.
Thx for your reply. I've gone over dynamic memory but was thrown off in this case with the above. Given *p_ptr = new(int) am I creating some object on the heap, or initializing the pointer? Here it seems to instantiate the type(int).. But I don't know for sure since I am professing my relative ignorance in this post.

Its interesting:
The Ideas Man
int x = 5;
int* ptr = &x;

Kemort
int x = 5;
int *ptr = nullptr; // initialized but still to be decided
ptr = &x;

In both cases you had to reference x.

In the code in question, you don't have to reference x, just assign value x to *p_ptr.:

int x = 25;
int *p_ptr = new(int);
*p_ptr = x;
std::cout<<*p_ptr<<std::endl;



If I am understanding your confusion correctly I think the best way to clear it up would be to draw a memory diagram. Both of the initial cases create a new object of type int and value 5. They then instantiate a pointer to an int and set the pointer's value to the address of x so it points to the exact same object in memory (that specific int that has name x), not some other int that merely has the same value as x.

With your code, you again create an object with type int and a value (this time 25), and again instantiate a pointer to an int. The line of code that instantiates it ( int *p_ptr = new(int); ) properly initializes the value of p_ptr, which is the specific spot in memory that the pointer points to, but the value of *p_ptr is still undefined. So to answer your question, you do initialize a pointer and create some object on the heap, but the value of that object on the heap is undefined.

When you then say "*p_ptr = x", the value of x is copied into the value of the object that p_ptr points to. This now initializes the value of *p_ptr and results in p_ptr pointing to an object that has the same value as x, but is not pointing to the actual x object.

To me it seems like you could benefit from reviewing two key distinctions: the difference between a pointer's value and the value of the object it points to (this was the issue in the original post), and whether a pointer is set to point to a copy of an existing object or the actual object itself (which is the difference between your code and the code of IdeasMan and Kemort).
These sort of problems are why C++ has references. The problem with pointers is that they can be made to point at anything, including nothing. Whereas a reference must refer to a variable and cannot be re-seated to refer to a different variable. However references can go out of scope and one cannot have a container of references, nor a reference to a pointer. One can have a pointer to a reference, or use std::reference_wrapper, or use smart pointers.

So references are less error prone, and that is why their use is encouraged. The use of raw pointers is discouraged, along with the use of new and delete.

http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
Got most of it. I almost have this, so please bare with me.
I want to focus just on this:
int *p_ptr = new(int);
I understand the left side of the expression, but don't understand the right. On the left we are initializing p_ptr type int. I understand that. But on the right new(int) - a new what? Integer?
Last edited on
new - as any tutorial or textbook will tell you - returns a pointer to the object for which memory has been allocated. In other words, it returns the address of the memory that's just been allocated.

In this case, you're allocating memory for a single int.

Perhaps you might find the following helpful:

http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
A * p_ptr = new A()

int *p_ptr = new(int);

In the first case I am allocating heap memory for object A pointed to by p_ptr. I can create and access member functions, etc.

In the second case, [quote]you're allocating memory for a single int[quote]. I've been looking at these two expressions as the same but that is incorrect.
Last edited on
Thx. When the pointer to the object for which memory has been allocated in this example how does it "tie" or "know" which object to be tied to. What you are saying makes sense: I am allocating memory for a single int. Which int object would that be?

It doesn't "tie" it to any existing object. It's a new object. It doesn't have any magical relationship to any other object you might already have defined.

A * p_ptr = new A()

I know that the object is A is the object that allocated memory is allotted to (sorry, redundant).

No. A is a class, not an object. A is the type of the object that's been created.
int *p_ptr = new(int);

What is the object here?
I got it, thx

*p_ptr points to allocated memory that has no name...
Topic archived. No new replies allowed.