Why isn't this example working?

This is a discussion on Why isn't this example working? within the C++ Programming forums, part of the General Programming Boards category; I copied this example from the book and no matter what i input the output memory address is always: 18495548 ...

  1. #1
    Ipsec Espah
    Guest

    Why isn't this example working?

    I copied this example from the book and no matter what i input the output memory address is always: 18495548

    Enter last name: Fredeldumpkin
    Fredeldumpkin at 18495548
    Enter last name: Pook
    Pook at 18495548

    Instead of:

    Enter last name: Fredeldumpkin
    Fredeldumpkin at 0x004326b8
    Enter last name: Pook
    Pook at 0x004301c8

    Code:
    #include <clx.h>
    #include <iostream>
    #include <cstring>
    #pragma hdrstop
    //---------------------------------------------------------------------------
    #pragma argsused
    using namespace std;
    char * getname(void);
    int main(int argc, char* argv[])
    {
            char * name;
            name = getname();
            cout << name << " at " << (int *) name << "\n";
            delete [] name;
    
            name = getname();
            cout << name << " at " << (int *) name << "\n";
            delete [] name;
            cin.get();
            cin.get();
            return 0;
    }
    char * getname()
    {
            char temp[80];
            cout << "Enter last name: ";
            cin >> temp;
            char * pn = new char[strlen(temp) + 1];
            strcpy(pn, temp);
            return pn;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Try:

    cout << name << " at " << ios::hex << (void *) name << "\n";

  3. #3
    Ipsec Espah
    Guest
    That made both of the memory addresses 1618495548

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Why should they be different? You allocate the first piece of memory available, then free it, so next time you allocate, it is quite reasonable it will allocate the same address.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    I read a book that referred to the addresses in hex form, yet from my 486 to my 1.8Ghz Pentium 4 I've yet to recieve a hex value when I print out the address. If it's not that big of a deal that it's not in hex I wouldn't worry about it.

  6. #6
    Ipsec Espah
    Guest
    Cool, i didn't think it was working because the addresses in the book were in hex and different... but if you guys say its working, then i'll take your word for it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 07:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 07:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

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