Results 1 to 4 of 4

Thread: basic C++ question

  1. #1
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default basic C++ question

    Hi,

    I am not sure if this question is appropriate for a Qt forum, since it is a general c++ question.

    The following code won't compile:

    int *p;
    *p=7;
    cout<<*p;

    When I create the pointer p, the compiler assigns a random adress and a random int value for p. On the second line of code, I try to change the int value that the compiler has previously assigned for p and on the last line it should output this value. All the Qt compiler says is: 'the program has unexpectedly finished'. Any suggestions why this happens?


    Thank you

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: basic C++ question

    When you use pointers, think of two (2) variables, one of type T and the other one that store the address of the first variable.

    Your code doesn't compile because you try to dereference an uninitialized pointer:
    Qt Code:
    1. int var;
    2. int *p = &var; //the pointer store the address of var
    3. *p=7; //var will have the value 7
    4. cout<<*p;
    To copy to clipboard, switch view to plain text mode 

    A pointer by itself is pretty much useless, you need a variable to store the actual data. (even if the variable is allocated on the heap, and it doesn't have a name... the variable is still there)

  3. The following user says thank you to Zlatomir for this useful post:

    Maluko_Da_Tola (23rd August 2010)

  4. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: basic C++ question

    Also, pointers need something to point at. In your original code, you declare a pointer, but you don't allocate any memory for it to point at, as in

    Qt Code:
    1. p = new int;
    To copy to clipboard, switch view to plain text mode 

    Without this step, you can't assign anything to p because there is no memory to hold the value.

  5. The following user says thank you to SixDegrees for this useful post:

    Maluko_Da_Tola (23rd August 2010)

  6. #4
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    29
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: basic C++ question

    You use the 'new' operator if you don't want to have another named variable, eg. int *p = new int;

    When you create a pointer, the computer creates a space in memory to hold the address of a variable. It doesn't create any space in memory for the variable itself, if you see what I mean. You have to tell it 'create a space to hold the address of a variable AND create a space for the variable itself'. That's what new does. That's also why you wouldn't usually use 'new int' - because the computer will have allotted four bytes or whatever to hold the address of the integer you're creating. Creating the integer uses up another four bytes, meaning you are using double the memory.

    Hope that helps.

Similar Threads

  1. very basic Qt/c++ question
    By Maluko_Da_Tola in forum Newbie
    Replies: 2
    Last Post: 25th July 2010, 14:02
  2. A few basic question
    By salmanmanekia in forum Newbie
    Replies: 12
    Last Post: 17th June 2010, 07:46
  3. Basic question
    By giacomelli.fabio in forum Newbie
    Replies: 4
    Last Post: 18th December 2009, 00:12
  4. Basic QtScript question
    By jimboqt in forum Newbie
    Replies: 0
    Last Post: 23rd September 2008, 14:09
  5. Using QSA: A very basic question
    By yogeshm02 in forum Newbie
    Replies: 3
    Last Post: 26th January 2006, 07:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.