how to double an array size?

This is a discussion on how to double an array size? within the C++ Programming forums, part of the General Programming Boards category; yes they are. Well then there must be something wrong then, cause it works....

  1. #76
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    yes they are. Well then there must be something wrong then, cause it works.

  2. #77
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    Quote Originally Posted by Raigne View Post
    yes they are. Well then there must be something wrong then, cause it works.
    Sorry, you are mistaken. It's like you're swearing black and blue that 1+1 equals 3. All C++ programmers KNOW it is 100% completely impossible for that to work.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #78
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    well the following code produces 0 as a result
    Code:
    void DeletePtr(void* ptr)
    {
      delete ptr;
      ptr = 0;
    }
    
    int main()
    {
       int* t = new int(100);
       DeletePtr(t);
       std::cout << t << std::endl;
       system("pause");
       return 0;
    }

  4. #79
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    C:\Documents and Settings\Andrew\Desktop>temp
    0x3d3d78
    You have an interesting concept of zero.

  5. #80
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    Quote Originally Posted by Raigne View Post
    well the following code produces 0 as a result
    Code:
    void DeletePtr(void* ptr)
    {
      delete ptr;
      ptr = 0;
    }
    
    int main()
    {
       int* t = new int(100);
       DeletePtr(t);
       std::cout << t << std::endl;
       system("pause");
       return 0;
    }
    Nope, that exact program can never print 0 (except that technically null does not have to equal zero, but we can forget that for the purposes of this discussion as nobody here is using such an architecture). In fact just ran it myself and of course got some random hexadecimal address. Your compiler would have to be completely broken to get a zero out of that.
    Perhaps you're seeing the return value from main instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #81
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    hmm, dunno. I know this one is pretty much guaranteed to work in most cases.
    Code:
    template<class t>
    void DeletePtr(t*& ptr)
    {
      delete ptr;
      ptr = 0;
    }
    template<class t>
    void DeletePtrA(t*& ptr)
    {
      delete []ptr;
      ptr = 0;
    }

  7. #82
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by tabstop
    You have an interesting concept of zero.
    Well... if you ignore the part after and including the 'x', you did get 0

    Quote Originally Posted by iMalc
    except that technically null does not have to equal zero
    A null pointer constant does evaluate to zero though.

    Quote Originally Posted by Raigne
    I know this one is pretty much guaranteed to work in most cases.
    Well, now you fixed it to pass the pointer by reference
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 6 of 6 FirstFirst 123456
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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