Hi
I saw the definition of strcpy (here: strcpy - C++ Reference)
and I was wondering how long can be the string that I pass as source (const char * source)?
Isn't it supposed to be limited?
source points to a constant char right?
Thank you (:
Hi
I saw the definition of strcpy (here: strcpy - C++ Reference)
and I was wondering how long can be the string that I pass as source (const char * source)?
Isn't it supposed to be limited?
source points to a constant char right?
Thank you (:
gavra.
Why limited!!!! It depends on, how much you can get the allocation done for destination string.
Yes source will be always constant. Think logically.
strcpy(destination,"String to copy");
S_ccess is waiting for u. Go Ahead, put u there.
Yes there is a limitation.... strlen(source)+1 must not exceed the size of the char array allocated to store the result. That is, if you have a 100 character array and you are copying to array[0] you cannot copy more than 99 characters. Of course if you are doing strcpy() into array [30] you are then limited to 69 characters, etc. There's no babysitting here, C will not adjust the buffer for you, so you have to be mindful of your buffer sizes at all times.
C-strings ends with a null-character ('\0'). That's why you don't need to give the length as a argument to the function. Strings that doesn't end with a '\0' is not a C-string and could indeed be of unlimited (unknown may be a better word) length in strcpy point of view.
strcpy pseudocode:
Code:while (source_string[index] != '\0') destination_string[index] = source_string[index++];
Last edited by Fader_Berg; 06-22-2011 at 06:21 AM.
>and I was wondering how long can be the string that I pass as source (const char * source)?
Your source assuming its constand string it can be as big as yhour the data BSS(initialised section) of your process memory block.
ssharish
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein
Actually on a 32 bit system it can be up to 2gb or the limit of memory, on a 64 bit system that expands to several terrabytes (2^63-1) or the limit of available memory.
The real limit is the size of the prepared memory block declared either with char array[x] or malloc(x *sizeof(char)) ... if you get outside those bounds you launch yourself into undefined behaviour and probable program crashes. So the OP's limit is not memory or range, it's the amount of memory set-aside for that string.
>Your source assuming its constant string
That means this
ssharishCode:strcpy(des, "this part of the string would be in BSS (initialised section) but not in the heap. This is a constant string!" );
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein
And if des is declared as char des[10] ?
You seem to be missing the point... the limitation is the destination, not the source... Copying more than 9 characters in to des[10] is going to cause a buffer overflow that might --make that "will"-- adversely affect the program's stability.
Agree on that.Originally Posted by commonTater
OP question
>and I was wondering how long can be the string that I pass as source (const char * source)?
ssharish
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein
the source string can as long as the BSS data section provided. And its upto the user how much data to be copied and big enough to accommodate the whole source data.
ssharish
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein
The assumption that the source string is a string literal is unnecessary, so any answer based on that assumption is unnecessarily restrictive.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes it is ... but the correct answer remains... "No longer than can be accomodated by the desitnation char array".
When that condition cannot be met... the solutions are either A) copy to fit the array or B) make a bigger array.
At no time is it acceptable to simply blind copy a string of unknown length into a buffer of unknown size... that's how seg-faults and Access Violations happen.
Last edited by CommonTater; 06-22-2011 at 10:41 AM.
It wasn't an assumption it was his question
ssharishOriginally Posted by gavra
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein