Click to See Complete Forum and Search --> : how do i allocate a dynamic char* in C ?
namgor
06-25-2002, 05:01 PM
I want to declare a data type of char*, but I do not know the # of characters I will be needed. Is there anyway to increase the size of such variable as I go along? (e.g. when i run out of space)
I can't work around by doing this:
char* a = new char [myInt];
because by the time of declaring this statement, I still dont know the size of the char*. btw, I need to use char* data type.
any advises?
It would help if you said what you were doing, but here are four solutions to your problem that I can think of:
1. You don't have to allocate the pointer right away. If you will know how much room you will need at some point, you can allocate it then. For example, if you had a file with 1 letter on a line, you would know how much to allocate by finding out how many lines there were in the file, first.
2. If you are doing something like reading in text at the command prompt, you can set an arbitrary max value that the array is set to. For example, only 255 characters can be entered in a DOS command. After that, it either prevents you from typing or overwrites the last character.
3. If you need to store one character at a time and you need to have unlimited space, then the first two solution won't work. In this case, you could create a linked list of characters and use that.
4. Another way to do step 3 would be to use the C++ Standard Template Library (STL). This contains a bunch of useful templates for programmers so that they don't have to reinvent the wheel. However, it is probably better to learn how to invent the wheel before using them :)
namgor
06-25-2002, 05:27 PM
thx for quick and informative reply. but all 4 sol'n probably can't help my problem.
I am not allowed to use STL.
Basically I m keeping track of name that is already input to a string. So I keep on appending them to a char* , and i scan the string to determine whether it has been entered or not
Thx :)
biosx
06-25-2002, 05:51 PM
You use the stdlib.h functions malloc(), calloc(), and realloc(). Look in a man page or a book to see their use. If you need any other help just ask.
I also wanted to point out something I saw in one of your other posts. In your piece of code here:
char* trims(char* input) {
char ret[500];
int i = 0;
for (j = 0; j < strlen(input); j++) {
if (input[j]!=' ') continue;
strcat (ret, input[j]);
}
return ret;
}
YOu already know that you can't return ret this way. However you can use the strdup() function in string.h to work around the static-memory-loss. strdup() actually uses malloc() internally. Check that out if you have time too.
good luck!
Sheep
06-26-2002, 01:42 AM
What are you doing that you can't use the STL??? Seems suspicious considering that STL is the standard template library...Just my opinion (coming from an STL junky).
I'm guessing he can't use the STL either because he needs to write the program in pure C, or this is part of an assignment for a class. I'm leaning toward the second, since he used the word, "allowed" :)
nam - From what you said, I'm not sure why solution #3 wouldn't work. If you could elaborate a bit more, it would be easier to figure out what you are trying to do.
biosx
06-26-2002, 11:52 PM
Originally posted by Sheep
What are you doing that you can't use the STL??? Seems suspicious considering that STL is the standard template library...Just my opinion (coming from an STL junky).
STL is C++.. The subject line says he's codin C