Size of a String

5

A char in the C language takes up 1 byte in memory.Ex:

char ch;//a variável ch esta ocupando 1 byte na memória

And a vector of char better known as string , its size in bytes will be counted according to the vector number. Ex:

char ch[4];

Is this variable occupying 4 bytes in memory, or is the count another?

    
asked by anonymous 30.09.2016 / 16:21

3 answers

9

Let's conceptualize correctly. Variables are placeholders to store something. Just this, nothing more than reserved in some way.

This variable will store 4 bytes in memory. If the intention is to have a default C string, then it can be up to 3 characters long and will have a terminator at the end, which is the character 0 , also called null and commonly represented by 'strlen()' .

In this case we know the size of it during development. And note that nothing guarantees that the string will be properly formed. Nothing guarantees that it will only have these 3 characters. What effectively determines the size of the string is the terminator. So if you create 10 characters and then put the final null will work and functions dealing with strings will assume you have 10 characters.

There you can ask how it puts 10 characters where it was reserved for 3. This is a huge mistake. But language leaves. It is the programmer's problem to turn around and do it right. In that case the content will invade the space of another variable will produce result at the least strange, possibly disastrous and unsafe.

The function normally used to determine the effective size of a string in C, and this is one of the major defects of the language, is the function string that counts all characters until you find the null terminator.

This can be seen in:

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

int main() {
    char ch[4];
    strcpy(ch, "abc");
    printf("ch tem %d bytes\n", sizeof(ch));
    printf("ch conta com %d caracteres\n", strlen(ch));
    strcpy(ch, "texto longo");
    printf("ch conta com %d caracteres\n", strlen(ch));
}

See running on CodingGround or CppShell .

Note that in C ++ it is usually more appropriate to use %code% . It has some disadvantages and several advantages. Another form should only be used if there is good justification.

    
30.09.2016 / 16:28
3

Complementing the response .

You can know how many bytes each variable occupies in memory. There are two ways to do this, the first is simply placing the reserved words inside the sizeof() operator. The second way is to declare variables and paste it into the sizeof() operator.

First:

#include <stdio.h>

int main(void)
{
    printf("Char: %d bytes\n", sizeof(char));
    printf("Int: %d bytes\n", sizeof(int));
    printf("Float: %d bytes\n", sizeof(float));
    printf("Double: %d bytes\n", sizeof(double));

    return 0;
}

Now, in addition to showing how many bytes each variable occupies, show its address. Monday:

#include <stdio.h>    

int main(void)
{
    char caractere;
    int inteiro;
    float Float;
    double Double;

    printf("Caractere: %d bytes \t em %d\n", sizeof(caractere), &caractere);
    printf("Inteiro: %d bytes \t em %d\n", sizeof(inteiro), &inteiro);
    printf("Float: %d bytes \t em %d\n", sizeof(Float), &Float);
    printf("Double: %d bytes \t em %d\n", sizeof(Double), &Double);

    return 0;
}

See working on Ideone:

30.09.2016 / 16:43
0
  • An array of characters ("char vector") no is known as "string". It is an array of characters ...

  • This character array (char ch [4]) occupies 4 bytes or 4 memory locations in today's computers (eventually, computers may exist or have existed where this is not true, but nowadays this is mere historical curiosity).

  • 01.10.2016 / 01:50