new char vs new char[size]

This is a discussion on new char vs new char[size] within the C++ Programming forums, part of the General Programming Boards category; I think people mix up this: char *buf = new char; with this: char *buf = new char[256]; They mix ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    812

    new char vs new char[size]

    I think people mix up this: char *buf = new char;
    with this: char *buf = new char[256];
    They mix up number of elements with number of bytes.

    Code:
    char *buf = new char[256];
    this means allocating 256 element.
    file.append(buf); and here you are using only one element of 256 and you can assign as many byte as you like to it.

    Whereas here i can assign as many characters as i want with only one element,
    this code's working:

    Code:
    #include <iostream>
    using namespace std;
    
    int  main()
    {
       char *buf2 = new char;
       strcpy(buf2, "hellooooooooooooooooooooooo!");
       cout  << buf2 <<"\n";
       delete buf2;
    
       return  0;
    }
    Do i see it right or am i missing the point?
    I see all the time this.
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    It might be "working" today, but your 2nd code is absolutely broken.

    The lack of an immediate crash is not an indication that all is well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Hmm, thanks Salem!

    I wonder whats the use of 'char *buf2 = new char;' then.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Ducky View Post
    Hmm, thanks Salem!

    I wonder whats the use of 'char *buf2 = new char;' then.
    The user of that would be to create a pointer to a single new char... Although I can't really think of any reason you'd want to do that in practice though. But there are bound to be some.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Ducky View Post
    Hmm, thanks Salem!

    I wonder whats the use of 'char *buf2 = new char;' then.
    To dynamically allocate a single char.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Quote Originally Posted by cpjust View Post
    To dynamically allocate a single char.
    Ok, im not gonna ask why would we wanna do that.

    Thanks to both of you, though!
    Compiler MSVC++ 2013 with Code::Blocks.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,656
    Quote Originally Posted by Ducky View Post
    I think people mix up this: char *buf = new char;
    with this: char *buf = new char[256];
    They mix up number of elements with number of bytes.

    Code:
    char *buf = new char[256];
    this means allocating 256 element.
    file.append(buf); and here you are using only one element of 256 and you can assign as many byte as you like to it.
    When working with pointers or arrays of char, the elements are characters (which I assume you're calling "bytes") by definition.
    Quote Originally Posted by Ducky View Post
    Whereas here i can assign as many characters as i want with only one element,
    this code's working:
    Salem has already addressed this part - I'll just expand a little. The form of operator delete has to match the form of operator new (i.e. if you allocate using operator new [], it is mandatory to release with operator delete[]). Doing otherwise gives undefined behaviour.

    One of the joys of undefined behaviour is that anything is allowed to happen. One of the many possibilities among "anything" is "seems to work". Unfortunately "anything" also means that something else could happen next week following a change of compilers, libraries, input conditions, or phase of the moon.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Hehe, thanks Grumpy!
    Compiler MSVC++ 2013 with Code::Blocks.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I'm actually rather curious if anybody can think of a situation one would actually want to use "new char". I can't think of one, in any case I can think of you can simply use a char on the stack or as return value.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Quote Originally Posted by EVOEx
    I'm actually rather curious if anybody can think of a situation one would actually want to use "new char". I can't think of one, in any case I can think of you can simply use a char on the stack or as return value.
    The usual reasons to have a dynamically allocated single object (e.g., something polymorphism related, extending the lifetime of an expensive to copy object beyond a function's scope) do not apply, but it would be an unnecessary exception to not allow new char. It may also be the case where a function template may have the expression new T for a template parameter T, and it could be the case that the corresponding template argument is a char.
    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

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    It may also be the case where a function template may have the expression new T for a template parameter T, and it could be the case that the corresponding template argument is a char.
    Good point. I understand that it would be crazy not to allow "new char", I was just curious about if it would ever be used. Your template function idea I didn't think about and it was a perfect solution to my question . But it was just out of curiousity Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 08:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 10:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM

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