Thread: array of zero elements

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    array of zero elements

    Hello everyone,


    Sometimes, we allocate array of zero elements. I am wondering for what regualr purpose will we do that?

    I have tested that in Visual Studio 2008, it runs ok.

    Code:
    int main()
    {
    
    	char* buf = new char [0];
    
    	delete[] buf;
    
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Maybe you don't know what the length is until runtime, and it's simpler if you don't have to treat zero length as a special case.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe you don't know what the length is until runtime
    But in that case wouldn't it be simpler to just initialise buf to 0? After all, when you do know the length, then you can use new[].
    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

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks laserlight,


    Quote Originally Posted by laserlight View Post
    But in that case wouldn't it be simpler to just initialise buf to 0?
    No, compile does not pass.

    Code:
    int main()
    {
    
    	char buf1[0];
    
    	return 0;
    }
    1>d:\visual studio 2008\projects\test_array3\test_array3\main.cpp(4) : error C2466: cannot allocate an array of constant size 0
    1>d:\visual studio 2008\projects\test_array3\test_array3\main.cpp(4) : error C2133: 'buf1' : unknown size


    Thanks robatino,


    Good idea!

    Quote Originally Posted by robatino View Post
    Maybe you don't know what the length is until runtime, and it's simpler if you don't have to treat zero length as a special case.

    regards,
    George

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, compile does not pass.
    No, I meant:
    Code:
    int main()
    {
    
    	char* buf = 0;
    
    	delete[] buf;
    
    	return 0;
    }
    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

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi laserlight,


    I am not sure whether delete the address of zero will cause undefined behavior -- maybe crash?

    Actually, we usually write code like,

    Code:
    if (buf)
    {
        delete[] buf;
    }
    Quote Originally Posted by laserlight View Post
    No, I meant:
    Code:
    int main()
    {
    
    	char* buf = 0;
    
    	delete[] buf;
    
    	return 0;
    }

    regards,
    George

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am not sure whether delete the address of zero will cause undefined behavior -- maybe crash?
    Deleting a null pointer is always harmless.

    Actually, we usually write code like,
    Usually we don't, unless writing code for a pre-standard compiler.
    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

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by laserlight View Post
    But in that case wouldn't it be simpler to just initialise buf to 0? After all, when you do know the length, then you can use new[].
    I meant that zero might be one of the possible values of the length, in which case not allowing zero-length dynamic arrays would mean requiring a branch in the code depending on whether the length is zero or not, with one branch having new[] and the other not.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks robatino and laserlight,


    My question is answered.

    Quote Originally Posted by robatino View Post
    I meant that zero might be one of the possible values of the length, in which case not allowing zero-length dynamic arrays would mean requiring a branch in the code depending on whether the length is zero or not, with one branch having new[] and the other not.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding More Array Elements?
    By Vermillion in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2008, 10:02 PM
  2. coping elements not in array
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 07:53 PM
  3. way to check 3 elements of array and set 4th
    By Syneris in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2006, 11:30 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM