Like Tree2Likes

const char *

This is a discussion on const char * within the C Programming forums, part of the General Programming Boards category; Hi I saw the definition of strcpy (here: strcpy - C++ Reference ) and I was wondering how long can ...

  1. #1
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265

    const char *

    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.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    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.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  4. #4
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    67
    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.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >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

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ssharish2005 View Post
    >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
    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.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >Your source assuming its constant string
    That means this

    Code:
    strcpy(des, "this part of the string would be in BSS (initialised section) but not in the heap. This is a constant string!" );
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ssharish2005 View Post
    >Your source assuming its constant string
    That means this

    Code:
    strcpy(des, "this part of the string would be in BSS (initialised section) but not in the heap. This is a constant string!" );
    ssharish
    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.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by commonTater
    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.

    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

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ssharish2005 View Post
    Agree on that.

    OP question
    >and I was wondering how long can be the string that I pass as source (const char * source)?

    ssharish
    Ok... so the correct answer is ... "No longer than can be accomodated by the size of the destination array"

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    The assumption that the source string is a string literal is unnecessary, so any answer based on that assumption is unnecessarily restrictive.
    CommonTater and _Mike like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ssharish2005 View Post
    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
    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.

  14. #14
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    It wasn't an assumption it was his question

    Quote Originally Posted by gavra
    >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

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ssharish2005 View Post
    It wasn't an assumption it was his question
    ssharish
    And you seem to be insisting upon giving him a wholly uninformed answer that will lead to buffer overflows and array bounds problems that can and will affect the stability of his code.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  2. Replies: 3
    Last Post: 11-15-2009, 03:57 AM
  3. Difference between const char * and char const *
    By explorecpp in forum C Programming
    Replies: 4
    Last Post: 08-09-2008, 04:48 AM
  4. Replies: 7
    Last Post: 04-28-2008, 09:20 AM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21