Thread: dynamic memory question

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    dynamic memory question

    can anyone tell me why why you have to use something like
    "
    Code:
    char ** codes;
     codes = new char*[num_codes];
     for (i = 0; i < num_codes; i++)
    	{ codes[i] = new char[AMOUNT];	}
    "
    for allocating memory yet

    Code:
    "codes = new char[num_codes][string_length];  "
    gives you an error like
    "cannot convert "`char (*)[6]' to `char**'" in assignment"


    the background of this is that we have to write a "set" class to represent a set of codes each code is a 5 character c-string.

  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
    If you get the type right, then you can do the latter quite happily
    Code:
    char (*codes)[num_codes];
    codes = new char[num_codes][string_length];
    This is a pointer to an array.

    A char** is a pointer to a pointer, and needs to be allocated in a different way.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    thanks. that worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Newbie question: pointers, other program's memory
    By xxxxme in forum C++ Programming
    Replies: 23
    Last Post: 11-25-2006, 01:00 PM