dynamic memory question

This is a discussion on dynamic memory question within the C++ Programming forums, part of the General Programming Boards category; can anyone tell me why why you have to use something like " Code: char ** codes; codes = new ...

  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
    32,851
    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, 07: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
  4. 2D array in dynamic memory
    By VanJay011379 in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 08:34 PM

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