Hopefully you understand why functions like strcpy are bad? And the correct alternatives that should be used for memory-safety :)
|
Unluncly you are overstimating me...
I simply need more specific functions, so this is the reason of my own functions. Some of the alias of "strremove" calls strcpy to make a copy of "source" to a "temp".
I didn't know that sctrcpy can be expansive for memory... seeing the fact my program requires accuracy of usage of memory (I need to allocate the memory to a great quantoty of datas) I will consider to write my own strcpy function
1 2 3
|
char x[50];
char * xptr;
xptr = &x; //THIS (&x) is an address
|
That is the address of the first element x[0]; Not the address of the whole array as it were.
|
Thank you very much for info :)
I knew that &x means the address of 1st element of array, but I dind't consider this fact you marked.
Using char arrays is going to cause you a fair bit of hassle with memory allocation and de-allocation.
|
Yes I know it, but, seeing the fact that C++ does not have a "string" type, I prefer to use directly arrays.
e.g You declare save as new char [100]. Does strremove modify and re-allocate? or simply move characters along and null terminate?
|
obliouvsly the 2nd. I know that you will vaste some memory, but it doesn't matter, becouse I require to use 1-2 strings... not much more...
I can probably reallocate memory, but I think it can be dangerous.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
char * string = new char [100];
...
//string = "hello world"
strremove(string,"ld"); //an other alias of strremove
//------------------
// if I need resizing string I can do a think like
strremove(char * source, const char * key) {
char * temp = new char[strlen(source)+1];
strcpy(temp, source)
//here routine for truncation in temp (I truncate temp in this example)
delete [] source; //it can be dangerous
source = new char[strlen(temp)+1]; //it can be dangerous and unreadable
//by the function who calls strremove... (or I'm wrong?)
strcpy(source, temp);
delete [] temp;
}
|
However, for my scope, I NEED to maintain the array dimension of string (it is long to explain why). So I will not consider to resize array dimension (Seeing also the fact that I no obtain so many mamory... I will use not much strings)