Declaring a string in C

Asked

Viewed 607 times

3

When setting the size of a string and not using all the space in the reserved memory, after the 0 the rest will be released?

char nome[40] = "carlos";

In the above example I declare a string with size 40, but the contents of the variable only occupies 6. My question is whether the rest of the memory (33 bytes) reserved for string will be released.

  • 1

    You may not set a size: char name[] = "carlos";

3 answers

6


First, without the context of where this statement is, it is difficult to give a good answer. If it is global it is one thing, if it is local it is another. I’ll consider it local, but if it’s global, that’s not how it works.

The way you declared the variable will take 40 bytes and 39 can be used to put characters (strings always need 1 byte terminator). This is fixed no matter if you put less. And if you put anything else will prevent it, it will corrupt the memory.

You must be thinking that this is a waste, and in a way it is, but it’s not as much as you might think. How is this in stack (read more about) the space of it is already allocated when it starts running, so it does not change the general consumption of the application, the only occupation is that a part of the stack will be reserved for this variable and this space will be 40 bytes. Note that in the compilation will not allocate 40 bytes anywhere, only in the execution of the function this will occur.

But it is not the only consumption. The text carlos\0 will occupy 7 bytes in execution (and will occupy memory space when the executable is loaded). It will be in an area called Data Segment. When executing the function this data will be copied from the Data Segment to the stack, so it has a processing cost (unlike many people think C has processing hidden in operations that seem not to have).

There is an important point about this which is the fact that you can manipulate what is in stack. You can put another value there without higher costs (you don’t need to create another variable and copy there) and not going beyond 39 characters does not corrupt the memory, and this is another important point.

Are you wasting 33 bytes? I can’t say without context, but I don’t think so. It is prepared to work with up to 39 characters and it can be very useful to have this extra space, even if you don’t always use it. It would be a waste if I never use more than six characters, never change that value for more. It’s still not that serious if you don’t have it many thousands of times. In fact experienced programmers make use of a larger reserve very often, including because the space economy may require the use of the heap (you must have read about in link above) which is much more complicated to manipulate. Everything in computing is tradeoff, so people prefer to occupy a little more than stack than having to allocate at an expensive and complicated location.

So forget about this waste if you want to have space to put another larger text. And it has the advantage of being able to modify its content.

If you really are using more than you should for your problem then you can do so:

char nome[] = "carlos";

This will occupy 7 bytes in the Data Segment and 7 bytes in the stack every time you run the function that has this statement. It is better if you ensure that the text will never exceed 6 characters.

But if it will never exceed 6 characters it will probably never change, so why copy to the stack? If you say so:

char *nome = "carlos";

I put in the Github for future reference.

shall not make a copy. The string stays in Data Segment and nome will be just a pointer indicating that the text is in the Data Segment. And you cannot change anything in it, nor size, nor even change one character by another. This is the most economical form in space (occupies nothing in the stack) and processing, provided you can give up modification of it.

It is more economical in most cases, not this one, although it is almost the same. If you are using a 64bit architecture there will be an 8 byte pointer on stack to point to the Data Segment, which is more than the 7 bytes that the text would occupy. It would still be advantageous for the copy cost not to exist.

Finally, a pointer does not specify the size of string, its function is another. Do not declare the size being with array or pointer, as long as the data is initialized, will cause the size to be the same as the initialization, that is, an inference will occur, and will not waste anything (losing the flexibility of being able to have a larger text), if that is what you want.

0

Once declared char nome[40] will be allocated 40 char in memory at the time of compilation, this value cannot be changed, but you can use a pointer to specify the size of the vector in the variable declaration.

char nome[40] = "carlos"; // 40 bytes
char *nome2 = "carlos"; // 6 bytes
  • 2

    "this value cannot be changed", got weird; can’t change (re)allocating memory?

  • @Andersoncarloswoss this example was in a static context, and as far as I know C the way to reallocate is using realloc() when variables were declared using malloc() which was not in my case. If there is any way to reallocate statically allocated variables I don’t remember at the time.

  • 1

    It is worth mentioning that although valid, these two instructions do not represent exactly the same. A string nome is changeable and you can do something like nome[0] = 'M'; whereas the same is no longer correct for the string nome2. In addition to the string nome2 occupies 7 bytes because it needs at the same time of the terminator

-1

The way you did you’re assigning to the variable nome the variable carlos. I believe it’s not what you want.

In C the function is used strcpy, of <string.h>, to copy strings. Study on array and pointers to understand.

To initialize a string you can use:

char nome[40] = "carlos";

The above statement will reserve a memory area of 40 bytes, use only 7 bytes of this area (there is a terminator character ' 0' that indicates the end of the string) and the rest will be reserved for later operations, for example in a concatenation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.