Thread: Pointer Question.

  1. #1
    The Programming Dutchman
    Join Date
    Jan 2008

    Question Pointer Question.

    Hello all,

    first i would like to say that iŽam Dutch, so if my english Grammar sucks or you dont understand what i mean. tell me .

    I have a little question about C++ and pointers. I started with the beginners tutorials on and now it explains pointers.

    could somebody explain me what this part of the tutorial (see Quote) means, I think it says, it is very dangerous for a program to use a pointer without declare a value to it(see code below). this program return the value 1474660693 to me, I think this is a random value from something that is stored in the memory location. is this what the writer of the tutorial(see Quote below) ment?

    #include <iostream>
    using namespace std;
    int main()
    	int *p;
    Notice that in the above example, pointer is initialized to point to a specific memory address before it is used. If this was not the case, it could be pointing to anything. This can lead to extremely unpleasant consequences to the program. For instance, the operating system will probably prevent you from accessing memory that it knows your program doesn't own: this will cause your program to crash. If it let you use the memory, you could mess with the memory of any running program--for instance, if you had a document opened in Word, you could change the text! Fortunately, Windows and other modern operating systems will stop you from accessing that memory and cause your program to crash. To avoid crashing your program, you should always initialize pointers before you use them.

    Thank for you help:


  2. #2
    Registered User
    Join Date
    Jan 2009
    yes that exactly what he mean
    because when you declare apointer without value assignment it points to random place in memory contain (garbage). so that number 147.... is some worthlress information you dont need.

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Zurich, Switzerland, Switzerland
    may be you can try writing value in to it:
    *p = 10;
    See what happen...

  4. #4
    The Programming Dutchman
    Join Date
    Jan 2008
    @ ali07:

    Ok, Thanks it is clear for me now


    Mmm, i dont want to mess up my fresh linux install ,But I think it would not work because, like the writer mentioned: the most mordern OS would prevent the program to access "unknown" Memory.

    Thanks all for your help.


  5. #5
    Join Date
    Oct 2007
    Inside my computer
    The answer is that the behavior is undefined. It may work, it may not. It may crash, it may not. The standard doesn't say, so basically "anything" can happen, which is exactly why you should not do it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM