Dynamic memory allocation

Hi all!
I have a two-dimensional array of char strings (char[N][M]). The first dimension is known(number of strings - N), but the last one depends on the size of a string user will enter. Therefore I'm advised to allocate memory dynamically, but so far I've got no idea how to declare an array not knowing the last dimension. Compiler won't allow me to do so. The only tools that I have are pointers, arrays and new[] operator. I'm not allowed to use STL or more advanced stuff.
I was thinking about declaring a really huge array and then after I know sizes of all strings I could resize it, but this doesn't seem logical too me.
Any help would be appreciated.
If you know how to use new you surely know how to do this.
You basically need an array of pointers (which are in some ways the same as arrays.) For each element of the array, dynamically allocate a new array.
char* dynamic = new char[anyvar];
You determine the length of the strings and then size the cstrings to that amount (taking into account the null at the end). Also keep in mind that you can probably just assign it:
char[0] = "yourstringhere";
but I don't use cstrings much so I can't say for sure.
You can just declare to about size eighty and that will hold a fair number of normal strings.
I seem to miss the point. In declaration like
char* dynamic = new char[anyvar];
how do you define anyvar integer?
anyvar means you can use anyvar. That's the main advantage of dynamic memory allocation at earlier programming skills. You can use a variable as the array size. Normally the size must be determinate at compile time.
EDIT: Let me direct you to this which may help your DMA: http://www.cplusplus.com/doc/tutorial/dynamic/
Remember to delete[] that which you new[].
Last edited on
I do understand that anyvar could be any variable calculated during runtime. What I don't understand (and I didn't find the answer in the link you provided) is that how do I allocate new char[anyvar] not knowing anyvar beforehand. anyvar can't be determined before user input and in order to get this input we should assign it to some kind of variable/array which means we spend twice as much memory (so we have to allocate memory for user's input, then calculate the length of entered string(which is anyvar), and only then we can dynamically allocate a new array). Is that what you mean?
THAT'S WHAT I SAID.
anyvar does not have to be a constant expression. It can be a variable. You can dynamically allocate an array based on a variable size instead of a constant.
You are going to have a tough time determining how long it is anyway, so just create an array of char* and then take the user input as a c-string and assign it to the char*. The compiler should be able to handle the rest.
cstrings are outdated and std::string is much better. You don't get any stupid size issues there.
Last edited on
tummychow wrote:
DMA
Direct Memory Access?

Anton: I would suggest you use an std::string to get the input. If you can't (for whatever reason), here's something you can do:
1
2
3
4
5
6
7
8
char *getUserInput(){
    char input[4096];
    //get input
    size_t l=strlen(input);
    char *r=new char[l+1];
    memcpy(r,input,l+1);
    return r;
}

4K characters seems like a reasonable limit for user input, but you may want to extend it.
Thanks a lot for your snippet, helios
There's just one problem. In the task I was assigned it is said that allocating memory dynamically based on user input is more efficient than holding all the strings in a large array. But in the code you provided you allocate 4KB and you won't be able to free this memory. Is that efficient?
P.S. Unfortunately I'm not allowed to use std::string
P.P.S Again, any examples would be highly appreciated, as I'm completely stuck.
DMA - dynamic memory allocation.
4k is pretty intense... how about maybe 400? That's enough for anything short of a paragraph or something.
And yes you can free the memory. You call delete[] on the memory:
delete [] newptr;
This destroys the memory at the address of newptr, rendering newptr's address invalid.
That (line 2) is stack memory. It's automatically freed when the function returns.

Creating an array that's about the size of user input would basically involve replicating the behavior of std::string and std::getline(). That is:
1. Create an array.
2. Get characters until the array is full or there are no more characters.
3. If there are no more characters, go to step 6.
4. Create a larger array, copy the first array to the new array and delete the first array.
5. Go to step 2.
6. You're done.
Anton, keep in mind that, unless efficiency is a real problem for your prog, it won't make a difference if you use 4000 or 400000, as long as your computer has memory to store all those characters.
Data accuracy is one thing but don't worry too much about the overhead incurred by your code unless that's one of the main aspects to take into account.
OK, thank you guys for your help.
Just one more point. tummychow said:
it won't make a difference if you use 4000 or 400000, as long as your computer has memory to store all those characters

So why do we care so much to free memory after allocating it dynamically and prevent any memory leaks if we can declare an array of 400000 bytes for the accuracy's sake and not to worry much about the space occupied?
a) You can't move around stack arrays as easily.
b) Stack space is very limited.
Because heap memory does not free itself. If you have variables on the stack they will destroy themselves as soon as they go out of scope. heap not so. It stays in existence until the end of your prog unless you delete it. Not deleting it is bad practice - it creates a memory leak. Not only is it bad style but it creates risky programming if you actually need to keep the deletion in mind for some project in the future.
And 400000 chars is a lot. That's at least a million bytes and more like 1.6mil. (1.5MB?)
Topic archived. No new replies allowed.