Weird Problem With Pointer

This is a discussion on Weird Problem With Pointer within the C++ Programming forums, part of the General Programming Boards category; Hey i have a program and i overloaded the + operator and for this one set of numbers -888+-112 i ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    109

    Weird Problem With Pointer

    Hey i have a program and i overloaded the + operator and for this one set of numbers -888+-112 i get the wrong number, i ran GDB and this is what i get maybe someone can see why c is getting set to the weird number of 134489 when temp is showing its 0.

    Code:
    538             if(count == c.numDigits)
    (gdb) print count
    $11 = 3
    (gdb) print c.numDigits
    $12 = 3
    (gdb) 
    $13 = 3
    (gdb) n
    541                 temp = c.digits;
    (gdb) print temp
    $14 = (int *) 0xb7f1221f
    (gdb) print *temp
    $15 = -273227
    (gdb) print c
    $16 = (BigInt &) @0xbfe13498: {digits = 0x80502a0, numDigits = 3, 
      positive = false}
    (gdb) print c.digits[2]
    $17 = 0
    (gdb) print c.digits[1]
    $18 = 0
    (gdb) print c.digits[0]
    $19 = 0
    (gdb) 
    $20 = 0
    (gdb) n
    544                 c.numDigits++;
    (gdb) print *temp
    $21 = 0
    (gdb) n
    547                 c.digits = new int [c.numDigits];
    (gdb) n
    550                 for(i = 1; i<=c.numDigits; i++)
    (gdb) print c
    $22 = (BigInt &) @0xbfe13498: {digits = 0x8050008, numDigits = 4, 
      positive = false}
    (gdb) n
    552                     c.digits[c.numDigits-i] = temp[c.numDigits-i];
    (gdb) 
    550                 for(i = 1; i<=c.numDigits; i++)
    (gdb) print c.digits[3]
    $23 = 134489

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >c.digits[c.numDigits-i] = temp[c.numDigits-i];
    You might try printing temp[3], temp[2], temp[1], and temp[0]. I would expect temp[0] would be equal to 0, because *temp is 0.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    what do u mean? i temp is equal to 0 but yet c.digits[3] is 134489 for some reason. I thought i could get rid of that by making everything equal to 0 in c.digits.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i temp is equal to 0
    It looks like temp is actually an array consisting of numDigits elements though, because I see this:
    Code:
    temp[c.numDigits-i]
    And if you print *temp, I think this is equivalent to temp[0], at least that's true in C++. It's hard to decipher your debug output, but it looks like you only print *temp (or temp[0]), so maybe temp[1] thru temp[3] contain something besides 0.

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. Problem with Pointer as return Argument
    By gregorsart in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 09:35 AM
  3. Input File HELP, weird problem
    By gravity-1 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2005, 08:43 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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