pointers problems

This is a discussion on pointers problems within the C++ Programming forums, part of the General Programming Boards category; I have created a float pointer the following way. float *tempPtr; tempPtr = new float(14); I then display what is ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    26

    pointers problems

    I have created a float pointer the following way.

    float *tempPtr;
    tempPtr = new float(14);

    I then display what is in the tempPtr.

    tempPtr[0]
    tempPtr[1]
    .....
    .....
    tempPtr[12]
    tempPtr[13]

    In the first 13 (0 thru 12) positions I see stuff like 1.63204e-37, which is OK but in the 14 (13) position I see =-NaN.

    Does anybody know what -Nan is? If I try and initialize just that last location my job goes down with an error.

    Thanks.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    NaN - Not a Number.

    To dynamically allocate an array, you should use square brackets.
    Code:
    float *p;
    p = new float[14];
    
    ...
    
    delete [] p;
    Your code is allocating a single float and initialising it with the value 14. Since the "array" element 14 does not exist, your attempt to write to it is causing some kind of access violation.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by adrianxw
    NaN - Not a Number.

    To dynamically allocate an array, you should use square brackets.
    What does

    Code:
    float(14);
    do then ? Obviously it seems to compile so it must have some kind of meaning for the compiler

    edit: thanks Adrianxw!
    Last edited by darksaidin; 07-25-2003 at 09:14 AM.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I was editing my last post to clarify that!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    THE BRACKETS That was it. I guess it just takes a second pay of eyes to see the simple things.

    Thanks again.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by darksaidin
    What does

    Code:
    float(14);
    do then ? Obviously it seems to compile so it must have some kind of meaning for the compiler
    It calls the default constructor for a single float. In other words it initializes the one float it creates to 14.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    int i1 = 1;
    int i2(1);

    both of these are the same.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

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, 05:23 AM
  2. problems passing pointers
    By doormat in forum C Programming
    Replies: 9
    Last Post: 04-11-2004, 05:38 PM
  3. Class Pointers
    By ventolin in forum C++ Programming
    Replies: 8
    Last Post: 04-04-2004, 07:07 PM
  4. Array of Pointers + Deleting An Object = Problems
    By Nereus in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2004, 12:16 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21