Allocating Arrays on the Free Store

This is a discussion on Allocating Arrays on the Free Store within the C++ Programming forums, part of the General Programming Boards category; I'm attempting to make a random name generator - for this I was going to pull in about a hundred ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    13

    Allocating Arrays on the Free Store

    I'm attempting to make a random name generator - for this I was going to pull in about a hundred names from a txt file into an array, and access the array using a random number. If this is a clumsy way of doing things and anyone has any better ideas, this would be really appreciated.

    But the problem I'm having is that the array is of course an array of strings. Which makes it an array of arrays of chars. And when I try to create this on the free store, I can't keep track of the chars.

    Here's the code I'm using to experiment:
    Code:
        char** array;
        array = new char*[50];
        
        char * s;
        s=new char[20];
        strcpy(s,"Lemmings.");
        //doesn't like  char s[20];
        
        int e;    //e will access the elements of 'array'. In each of these elements will be placed a string.
        e=0;
        
        for (e;e<5;e++){
            
            strcat(s,"A");
            
            //putting the string in the array.
            *array+=e;    //*array-the value pointed at by the array pointer- [0] is moved along to [e].
            *array = new char[10];    //a new character pointer (ie, string) is created at array[e];
            strcpy(*array,s);    //copy the string s into the element (*array) of the array (array).
            
            cout<< *array <<"end.\n";    //print the element (the line pointed to by array).
            
            *array-=e;        //array pointer moved back to array[0]
            
            
        }      
        e=1;
        *array+=e;  
        cout<<*array;
    Now I'd expect the final line to print "LemmingsA", that being the string that was created at array[1]. But it just returns garble. And I don't know why.

    Any help on this would be helpful.
    Last edited by evilkillerfiggi; 12-04-2005 at 05:37 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    But the problem I'm having is that the array is of course an array of strings. Which makes it an array of arrays of chars.
    You should be using a string type anyway, and then you would have an array of string types--not char arrays. You also wouldn't have to use strcpy() and strcat() to perform string operations, you could just use assignments and string addition:
    Code:
    string str = "hello" + " world";
    This works fine for me:
    Code:
    char * s;
    s=new char[20];
    strcpy(s,"Lemmings.");
    //doesn't like  char s[20];
    Add a cout statement and see for yourself:
    Code:
    char * s;
    s=new char[20];
    strcpy(s,"Lemmings.");
    //doesn't like  char s[20];
    cout<<s<<endl;
    As for this:
    Code:
    char** array;
    array = new char*[50];
    ...
    *array+=e;
    'array' is a pointer to the first element of the array, where array[0] is the first element of the array, and therefore *array is the first element itself or array[0]. array[0] is a pointer, and it points to the first character of a char array. So, if you advance array[0], or *array, by 1 then you move the pointer array[0] to the second character in the char array--not the next element of 'array' which is array[1].

    You can easily test that to see what is happening:
    Code:
    char** array;
    array = new char*[50];
    
    array[0] = "hello";
    array[1] = "world";
    
    *array+= 1;
    cout<<*array<<endl;
    That is equivalent to this:
    Code:
    char** array;
    array = new char*[50];
    
    array[0] = "hello";
    array[1] = "world";
    
    array[0] += 1; //*array is the same as array[0]
    cout<<*array<<endl;
    If you want to advance a pointer from array[0] to array[1], then you have to do something else. Here's how:
    Code:
    char** array;
    array = new char*[50];
    
    array[0] = "hello";
    array[1] = "world";
    
    char** parray = array;
    parray++;
    cout<<*parray<<endl;
    Last edited by 7stud; 12-05-2005 at 06:47 AM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    Okay, thanks very much! I wasn't aware char pointers could be set to strings that easily. But it all works now, so thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lots of freeware
    By major_small in forum General Discussions
    Replies: 58
    Last Post: 1 Week Ago, 11:27 PM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Free store vs. heap
    By CondorMan in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2006, 12:41 PM
  4. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 01:05 PM
  5. how to make arrays s->name store string ?
    By Adisonz in forum C Programming
    Replies: 8
    Last Post: 12-24-2001, 12:08 PM

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