about heap allocation

This is a discussion on about heap allocation within the C++ Programming forums, part of the General Programming Boards category; Still i don't understand what is the meaning of : char * ch=new char[3]; Is it that: i can store ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    about heap allocation

    Still i don't understand what is the meaning of :
    char * ch=new char[3];

    Is it that: i can store one string which length is 3 characters
    or: i can store 3 strings that their sizes are not important.

    Which is right?

    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > Is it that: i can store one string which length is 3 characters
    Actually, it's two chars and a \0
    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
    Jan 2006
    Posts
    61
    Thnaks for answer
    But why does this code work?
    There is no error
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
     
      char * ch=new char[3];
      ch="iaisiaiaisiiai";
      cout<<ch;
      return 0;
    } ///:~

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > But why does this code work?
    Well it only "works" because you don't call
    delete [ ] ch;

    Also, "works" is generally assumed to only mean "produces expected output and doesn't crash". This is somewhat different from "bug free".

    Your assignment does not copy the string to the allocated memory, it just assigns a new pointer (and loses the pointer new returned in the process).

    If you allocated enough space, then you would do this
    strcpy ( ch, "iaisiaiaisiiai" );
    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.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    thanks salem.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Note that even if you don't allocate enough space, even the use of strcpy might appear to work. However, this is what is called undefined behavior and means that your program might crash or might appear to work, and you don't really know which one it will be at any time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory allocation from stack and heap ?
    By Bargi in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 12:37 AM
  2. stack vs heap memory allocation
    By gongchengshi in forum C Programming
    Replies: 9
    Last Post: 11-18-2007, 11:17 AM
  3. stl containers allocation in heap or stack?
    By sawer in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2006, 03:08 PM
  4. allocation on heap, scalar/non scalar types
    By curlious in forum C++ Programming
    Replies: 10
    Last Post: 12-30-2003, 04:16 AM
  5. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 04:03 AM

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