1

I'm willing to lose some reputation for this question because I'm not understanding why my C++ program is not working. I'm learning about C++ pointers, and I kind of get the concept, but I'm not able to apply the syntax. I assume the lines of commented code are failing because I'm trying to access an invalid location in the memory, but where am I going wrong? The uncommented lines work

Program

#include <iostream>

using namespace std;

int main()
{
  /*  int * pointer;  // These commented lines don't work
    *pointer = 23;
    cout << "Pointer: " << pointer
         << "\n*Pointer: " << *pointer
         << "\n&Pointer: " << &pointer; */

  int *noPointer;
  int no_one = 1;
  noPointer = &no_one; //taking this line out gives a warning, but doesn't hurt program
  *noPointer = 10;

  cout << noPointer << " " << *noPointer;
    return 0;
}
5
  • You declare a pointer to an int and then try to assign an int to a location that is undefined. 'pointer' is not pointing to anything so assigning a value results in undefined behavior. Commented Dec 23, 2013 at 3:48
  • Your program works absolutely fine when I compiled it. It works on this online compiler too compileonline.com/compile_cpp11_online.php Commented Dec 23, 2013 at 3:51
  • You must first assign an lvalue to a pointer, before you can safely dereference it OR you could guess a valid memory location the lies in between your program. Commented Dec 23, 2013 at 3:52
  • @TejasPatel Reread the code and the question. It is the commented lines that do not work. The compiler, therefore, is not compiling those lines. I know that uncommented lines work. I was messing around, trying to get the pointer working. Commented Dec 23, 2013 at 3:58
  • @Crysis Yes Sorry, I got that now. Commented Dec 23, 2013 at 3:58

4 Answers 4

4

A pointer points at memory, but doesn't "own" memory. Worse, if not initialized, it points at null or somewhere in nomansland.

The confusion is that int * pointer can be assigned any value. It can be loaded with any address across the entire system. But not every address is a valid place to go reading or writing. 0 being the perfect example.

So taking int * pointer and then assigning something to it before pointing it at a valid location is just stuffing values in random unassigned memory.

While the compiler will compile this just fine, running this bit of code will usually cause a failure:

int * pointer = 0 ;
int badval = * pointer ;  // what exists at address zero?

* pointer= 100 ;  // write 100 to address zero, probably really bad.

Pointers need to point at something automatically allocated, or have memory assigned to them through new or some other system allocator.

Sign up to request clarification or add additional context in comments.

9 Comments

Here the pointer is pointing to already allocated location. So no new space need to be allocated.
This is tagged a C++ so don't introduce a C style memory allocation. It must be new not malloc
@woolstar here *noPointer points to integer variable no_one for which space is already allocated by declaration int no_one.
@TejasPatel yes he mentioned that that code works fine. he asked why the commented out code doesn't work.
@TejasPatel You might consider reading the question once more ?
|
3
int *pointer;

This declares a pointer to an int. The variable is local so it gets automatic allocation on stack. What happens is that the value contained by the variable (which is an address) contains garbage value, since you don't initialize with any valid memory address.

*pointer = 23

You dereference the pointer, so you try to set the value pointed by this address, but you never initialized the pointer to a valid address so you are trying to write 23 in a garbage memory address, which makes your program crash.

To be able to modify a pointed value you need to obtain its address through:

  • a reference to an existing variable (like in your example: pointer = &intVariable)
  • a dynamic allocation (eg int *pointer = new int())

Comments

2
noPointer = &no_one; // You are initializing the noPointer with an accessible address which is necessary.

If you do not initialize the pointer variable , it is pointing to some address which may or may not be accessible. So you have to initialize the pointer variable

Comments

2

Your pointer variable has unpredictable value (you did not set it, so it gets whatever is there on the stack), and thus points to some random memory location. Then you try assigning something to that random memory, probably resulting in a segmentation fault. The moral here is to always have your pointers initialized with either the result of & operator, or an explicit allocation with new.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.