Thread: about heap allocation

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

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

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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, 12:17 PM
  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, 05:16 AM
  5. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 05:03 AM