Where does the memory space required for each element in a C string array come from?

Asked

Viewed 125 times

3

In C, you can group a set of string's, which are arrays of char's within a array without having to define 2 dimensions for this?

That’s why you use a array pointer char's and simply defines the string's you want as elements of it (correct me if I said some nonsense so far).

I can use that array normally. What I do not understand is the logic of the space that each of these elements occupies in memory. If each char of string'are within the array pointer occupies 1 byte, as a string with 5 letters can occupy only 4 bytes in memory?

Example:

#include<stdio.h>
#include<stdlib.h>
int main() {
    char* lista[5] = {
        "coffe",
        "tea"
        "soda"
        "water",
        "juice"
    };
    printf("%d\n",sizeof(lista[0])); //A saída gerada foi "4"
    return 0;
}

1 answer

3


You don’t understand pointers. And I start by saying that not everyone has 4 bytes, this only occurs on 32-bit architectures, and this if there is no specific trick done by the compiler with some special option.

A pointer is a value that will point to a datum. For some reason you think the datum is in the pointer location. Every object by reference has two completely different parts, the value itself that is somewhere in the memory and the pointer that references that address where the value itself is. It is possible for several pointers to have the same value and therefore point to the same data.

This data being in any part of the memory. The most common are the stack (automatic memory) and the heap (dynamic memory. But there is another that little is said to be the static memory (it is not the stack as many think, this non-static).

The static area of the memory, as the name implies, cannot be modified and it is organized according to the generated executable. Then when your executable loads in memory the code and data contained in it are available for application access somewhere. Among these data are some literals, mainly the texts written in the code.

So you have there 5 texts that go next to your code in the executable. They are there in memory, the compiler has reserved a space for this.

Your array has 5 pointers. Note that the type of your array is char *and not only char, so the type is read as "pointer to char". Can you understand that its content is a type by reference as I explained before, and is composed of two parts? A clear part is the pointer and the other part you will have at least 1 character. No array only the pointer is stored.

Where is this pointer pointing? Simple, to the static area where each of these strings that are in your code.

When you take each of the elements of array is not picking up the texts, is picking up the pointers, and they all have the same size (4 bytes where you are testing). You’re not getting the texts.

And when it says to show the size it is of course the pointer and not the text, that even if you want the size the correct is the use of strlen(), otherwise you will not get the desired result.

If you want to get the size of the text do:

#include <stdio.h>
#include <string.h>

int main() {
    char* lista[5] = {
        "coffe",
        "tea"
        "soda"
        "water",
        "juice"
    };
    printf("%zd", strlen(lista[0]));
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

So you pass the pointer to the function that knows how to count how many characters it has in it (goes until I find a terminator), what can be inefficient when you don’t need to know the size.

Recommended reading.

Browser other questions tagged

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