Thread: how to double an array size?

  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,318
    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,336
    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,318
    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
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 04:09 AM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM