• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C++ New/delete plus arrays

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

CyberFed

Member
ok heres what im doing, i have a temporary storage array temp[100] (any large number to hold user input)

i then have the user type in a sentence and store it in this array.

I then calculate how much of the array was actually used (call it NUM) and use that number to create a dynamically allocated array of size[NUM], i then copy the string over to the new vector and it works fine that array is now exactly only the size of the typed string (so if the user types in "hello" the new string is only 6 characters long vs the 100 for temp)

my question is i want to find a way so that if i put the user input into a function and call it say 3 different times, i can create 3 new dynamically allocated arrays (or not just 3 but however many times the user input function is called)

example

Hello world (1st input creates a dynamic arrayof size 12)

Hi (2nd input creates a dynamic array of size 2)
and so on...

my problem is in my code i have to define a name for the new array i.e.

char *vec;

vec = new char [size];

but when the user inputs the 2nd, 3rd, or Xth string, how can i create a name for the dynamic array?

I know this is a bit confusing hope someone out there can make sense of my rants
 
I'm not quite sure what you want to do here. If you are trying to get variable names that are determined at run time, that is not possible.

What I think you want is a function that returns a character array containing a line of user input each time its called. So that would look something like this:
Code:
char *get_input() {
    char *buf, *str;
    buf = new char [100];
    // somehow read input into buf
    str = new char [strlen (buf) + 1];
    // copy buf into str
    str [strlen (buf)] = '\0'; //ensure array is null-terminated
    delete buf;

    return str;
}

You'll have to replace some of the comments with whatever library function you want to use to accomplish those tasks (reading input and copying the array). I could write it all out for you in C if you want, but I don't know that much C++. Every time you call this function, it will return a pointer to an array of characters that holds some input from the user. Here's an example showing how you could use this function to echo all read input back to the screen:

Code:
void echo() {
    char *input;
    for (;;) {
        input = get_input();
        std::cout << input << std::endl;
        delete input;
    }
}

Hope that helps.
 
What you need is a two dimensional dynamic array. This will allow you to access the strings like an array. Example, inputs[1] will be a pointer to the second string entered.

Code:
char buffer[100];
int num_inputs;
char** inputs;
char** new_inputs;

num_inputs = 0;
inputs = NULL;
while (input_available()){
    get_input(buffer);

    num_inputs++;
    new_inputs = new char*[num_inputs];
    if (inputs != NULL){
      memcpy(new_inputs, inputs, num_inputs-1);
      delete [] inputs;
      inputs = new_inputs;
    }

    inputs[num_inputs-1] = new char[strlen(buffer)+1];
    strcpy(inputs[num_inputs-1], buffer);
}

/* Don't forget to delete everything later. */

All the memory management stuff can be handled by STL to create much simpiler code. The vector class will still allow you to access the data like an array (although it will do it through an overloaded operator.) Example, inputs[1] will return a pointer to the second string entered.


Code:
#include <vector>
#include <string>
using namespace std;

...

char buffer[100];
vector<string> inputs;

while (input_available()){
    get_input(buffer);
    inputs.push_back(string(buffer));
}

/* Everything will be automatically deleted later. */
 
If you know how many inputs will be generated, then you can just create a fixed array of character pointers. You need a 2 dimensional dynamic array if you need unlimited capacity.

The other possibility, which is more efficient than a 2 dimensional dynamic array IF you do not need random access into the array is a linked list. Regardless of the size, you never need to reallocate and copy the information into a new array.

This is done by creating a node struct that has 2 data fields, one for the data in question (in this case a char *, and one for a pointer to the next node, a node *. Each time you add a new data item, just create a new node on the node * of the last node used. You use pointers to keep track of the beginning of the list and where you are in the list when you're reading it. The disadvantage is random access to the elements is not available, as you can only read the elements in order. You must go through the list to get to the item you want.

The absolute easiest way to do this is to use a vector of char *. Every time you need another one, just do my_vector.push_back(mycharacterpointer). I don't know if you can use STL though in your class, as they may want you to understand fundamentals first.
 
Back