Size of a character array according to its addressing

Asked

Viewed 161 times

2

Is the size of a character array given by its address or by the value of the variable? Example:

char[1000] = "Exemplo Teste"

The size of this variable is 1KB due to its addressing char[1000] or her size is 13 bytes due to its content?

  • C reserves the memory space according to the size you set in [], in case it will have 1KB. in C++ do not know inform.

3 answers

2


The terms are wrong but for what you want to know the answer is that there is a 1,000 byte space reserve in memory. Although this is detail of implementation, in all existing implementations will be reserved on stack.

Note that this is not 1KB, at least not in the way people know it. Even strictly speaking it is, because 1 KB actually has 1000 bytes, but when people use KB they actually want to use Kib which is the equivalent of 1024 bytes.

Remember that you can only put 999 characters if you want to follow the pattern of string of the C, because of the terminator.

The variable declaration (in the bracketed part) determines this placeholder and the assignment is effectively occupying the space.

  • Maniero, I would like to make a variable char with size of 20 Kbytes, so I should declare `char[20000] = "Fill the text here with 20,000 characters"? By the answer of Wellington, by the time I declare the variable this way, it would already be occupying 20 KB.

  • It’s very simple, the reserved space will be what you put between the brackets, if you want 20000 characters you have to put 20001 there. If you want Kbytes there have to put more. All this is in my answer. It actually seems like a strange thing to want to do and it doesn’t seem to make sense to do it, but I don’t know what its context is.

  • I want to test the performance of a hash algorithm for data with different sizes, 10 Kbytes and 20 Kbytes, the problem is that each character is 1 byte, if I have to fill the variable with 20000 characters it will be a lot of work, so I asked if when I put the 20000 in brackets already allocates the size of 20000 bytes to variable, without having to fill it with the 20000 characters, I hope to have been clearer now.

  • 3

    I think you’re trying to do something too sophisticated for your current knowledge, you’re stumbling into very simple details. He has no difficulty in doing what he wants, but that is another matter, and again, he must first learn the basics and then think of more complex things. I suggest looking for a quality course that gives you the foundation (I can not indicate one), it does not seem that you are learning productively.

  • @Mutante can do this vector fill of characters in a line (or two), but I believe that once written, it will be a puzzle to try to read this single line. In a more verbose way, you can write in 4, then it is readable even for those who have basic knowledge in programming (and does not need to be in C). This, of course, thinking of a random fill or else of "random entropy" but reproducible.

0

Friend, from what I saw in the comments what you want to know is whether when you do it:

char vetor[20000];

This space is automatically stored in memory for this vector.

Well, the primitive type char houses 1 byte per character, that is, if you make a vector with 20,000 space, this vector will store 20,000 space in memory for it to be used. This means that every time you start the program this vector will take this space of 20000 bytes and store them, regardless of their content. To avoid making such a large vector size without need you can make a dynamic space allocation through the malloc function, so every time you start the program the function allocates that space dynamically, keeps it, and when you finish and program identify the free:

vetor = (char *) malloc(20000*sizeof(char));
.
.
.
.

free(vetor);

It will free up all unused space, making the program lighter. I hope this was the question.

0

The size of it is according to the space you allocate in memory. As it is a vector that you have already reserved the space will be according to the number you entered, in this case it is 1000.

Browser other questions tagged

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