Thread: A little confused about how strigns work!

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    5

    Question A little confused about how strigns work!

    I know that a string is basically a pointer to the first character, and the next memory adresses will contain the next characters until the \n indicates the end of the string. But do we need to allocate it? the string is just in the memory and it can easily be overwritten right? So I believe that this is how C works: if you define the array with size 5 than compiler allocates suitable amount of memory for it, otherwise if we want a pointer - than how does a compiler know the amount of memory requested? It will probably allocate the the minimum - 1 byte?...

    Thank you very much!

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    824
    Nope... the last byte in memory which marks the end of string is ZERO ('\0'), not '\n'.

    If your string has a variable size, than you need to allocate enough space to contain every char, including the final '\0'. For "constant" sized strings you could do:
    Code:
    char str[] = "fred";
    Which will alocate 5 chars (including the last '\0'). And str can be used as the pointer to the first char in the array (&s[0]).

    As for overwrite the literal strings. it's not possible, unless you do something like the fragment of code above. There I'm declaring an array and initializing with 5 chars... But if you do:
    Code:
    char *str = "fred";
    Here I'm declaring a pointer which points to a literal "string" (an array of 5 chars, '\0' included). But, since operating systems like Windows or Linux allocates this literal in a read-only page, any attempt to overwrite any char will cause a general protection fault:
    Code:
    str[1]='R';  // this will fail

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    490
    Quote Originally Posted by weareandrei View Post
    I know that a string is basically a pointer to the first character, and the next memory adresses will contain the next characters until the \n indicates the end of the string. But do we need to allocate it? the string is just in the memory and it can easily be overwritten right? So I believe that this is how C works: if you define the array with size 5 than compiler allocates suitable amount of memory for it, otherwise if we want a pointer - than how does a compiler know the amount of memory requested? It will probably allocate the the minimum - 1 byte?...

    Thank you very much!
    C strings are terminated by 0 (nul), not newline.

    In C, strings are basically just arrays of 8 bit ascii values arranged in memory. There's jsut a little bit of special syntax to allow you to declare a string with double quotes.

    The memory can be on the stack, on the heap (allocated with malloc), or in the global memory space, which is where string literals (strings with double quotes) usually go.

    Often C programmers refer to char *s loosely as "strings", because that's the normal way of passing a string about and manipulating it. But a pointer to a strign is not in fact a string, the pointer points to the first character in the string. You then either increment by one or use array syntax to get the next character.

    You have to be sure that you have enough memory if you are building a string from component parts. That's why you will frequently see declarations like

    Code:
    char filename[1024];
    in C. 1024 characters is a ridiculously large size for a filename. It's chosen as a safe limit. (This sort of thing also gets C a bad name).

    The compiler will make sure that string literals have enough memory, but otherwise it won't help you. You have to tell it how much memory the string needs explicitly.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Feb 2021
    Posts
    5

    Question

    Quote Originally Posted by flp1969 View Post
    If your string has a variable size, than you need to allocate enough space to contain every char, including the final '\0'. For "constant" sized strings you could do:
    Code:
    char str[] = "fred";
    Which will alocate 5 chars (including the last '\0'). And str can be used as the pointer to the first char in the array (&s[0]).
    So If I do:
    Code:
    char *name;
    Then I could practiacally have an unlimited space? Because name points to some location 1000 for example:
    [1000] [1001] [1002] [1003] [1004] [1005] [1006] [1007] [1008] [1009] [1010]

    I can set name to 'a' then it will be:
    [1000](a) [1001] [1002] [1003] [1004] [1005] [1006] [1007] [1008] [1009] [1010]

    But I could continue and add as many as I want...
    [1000](a) [1001](l) [1002](e) [1003](x) [1004](a) [1005](n) [1006](d) [1007] [1008](e) [1009](r) [1010] (\0)

    And this is just 10 memory locations, I could go mad and take 10 000 locations in a row? But what if for example location [1002] will already be taken by another variable?? For example:
    char *letter; This will take location [1002] and make it (c);
    what then happened if I tried to put my string into a variable that *name points to?
    Thank you!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,276
    Quote Originally Posted by weareandrei
    Then I could practiacally have an unlimited space?
    Perhaps you meant "theoretically" rather than "practically". In practice, you of course do not have unlimited space since it is limited both by hardware and the fact that the space allocated needs to be contiguous (e.g., you'll use malloc to allocate the space first).

    Quote Originally Posted by weareandrei
    But I could continue and add as many as I want.
    Only if you say, call realloc first so as to have that space available to you.

    Quote Originally Posted by weareandrei
    And this is just 10 memory locations, I could go mad and take 10 000 locations in a row?
    Yes, if there is a contiguous stretch of 10000 bytes of memory that can be allocated.

    Quote Originally Posted by weareandrei
    But what if for example location [1002] will already be taken by another variable?? For example:
    char *letter; This will take location [1002] and make it (c);
    Unlikely: typically, local variables will be allocated in a memory space that we would call the "stack", whereas what we allocate with malloc would be from a memory space that we would call the "heap".

    My feeling is that you think that with just a pointer to memory at a start address, you can consume as much memory as you want from that point. Nope, you can't: that would eventually result in out of bounds access. You can only consume as much memory as has been allocated from that start address.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why doesn't this work (confused)
    By Albinoswordfish in forum C Programming
    Replies: 8
    Last Post: 04-21-2009, 06:24 AM
  2. A little confused about this
    By manav in forum C++ Programming
    Replies: 20
    Last Post: 05-29-2008, 10:47 PM
  3. Confused
    By Kelvin in forum C Programming
    Replies: 13
    Last Post: 07-12-2002, 05:35 AM
  4. Confused !!!
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 06-11-2002, 12:30 PM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM

Tags for this Thread