Thread: AString allocation

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    AString allocation

    Hi

    I have a class, with a private data member char *a. In the constructor, how can I allocate 20 bytes for char *a, and then set it to a parameter?

    Code:
    class A
    {
          private:
             char *a;
    };
    
    A::A(char *b)
    {
         //would like to allocate 20 bytes, and set b = a;
    }
    
    //desctructor
    A::~A(void)
    {   
         delete[] a;
    }
    Thanks
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    a = new char[strlen(b) + 1];
    strcpy(a, b);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  3. Allocation from "static array"
    By Petike in forum C Programming
    Replies: 6
    Last Post: 10-12-2008, 03:17 AM
  4. redundant allocation
    By George2 in forum C++ Programming
    Replies: 22
    Last Post: 03-06-2008, 06:43 PM
  5. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM