Maximum size of a character vector

1

Is there any maximum quantity, that a variable of type char supports character?

For example, could I use char texto[1000]; ?

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main(){

    setlocale(LC_ALL, "Portuguese");

    FILE *file;
    file = fopen("copyright.txt", "r");

    char pagina1[928];

    while(fgets(pagina1, 928, file) != NULL){
        printf("%s", &pagina1);
    }
    fclose(file);
return 0;
}
    
asked by anonymous 22.11.2018 / 19:06

2 answers

3

The type char always has size of 1 byte, and therefore a character, as long as it is a single byte encoding ( char does not support multibyte characters ).

A sequence of char can have the maximum number of bytes available in the address allowed by the operating system and is free at that time, but depending on where this sequence is allocated may be a problem.

In your example you are allocating stack , which under normal conditions, depending on the operating system, has 1 MB available only for every application. It is a very dynamic memory (to my sadness many consider it static) and its management is automatic / a>, then the part used effectively grows and decreases frequently, but it is not appropriate to abuse, if it is to allocate many large vectors it is better to opt for heap , unless you know well what you are doing or understand the tricks to grow the stack beyond the standard size, understanding the consequences of it. 1KB or less is not to be a problem in a simple example like this, could have had a recursive call, or had other large components in the call stack.

In heap you can allocate several megabytes or even gibabytes without major problems, but if you go beyond the amount available in RAM, there will be swap and the application will be extremely slow, so contain the use, but that's an obvious sense. In 32-bit architectures the maximum addressable is usually 2GB. In 64 bits it is virtually impossible to get the address bursting, but it can burst the RAM (traditional, soon we will have persistent non-volatile memories of great capacity, there is another story, much of what we have learned until today will not be worth more :)).

Otherwise "the program does not want to read the file" is too vague for us to help. But the code has some problems, and this should be preventing normal execution.

    
22.11.2018 / 19:18
4

The answer to this question is not so obvious, and understanding what happens implies understanding what you are doing.

First: a char variable holds a single byte , which for the Latin alphabet and arabic digits normally equals a single character - (but that depends on the encoding used.)

The variable you are using is a char[] - this is a character pointer. What goes "in" to the variable itself is a memory address, which in normal PCs is usually an address of 8 bytes (64bit); The number that goes between the brackets, as in char minhavariavel[1000] , is an amount of bytes that the compiler leaves reserved in memory for its variable. The maximum value depends on the CPU architecture (for the first x86 16-bit PCs as well as the 8-bit machines, that number was 2 ^ 16 = 65536, for example). The maximum size for this depends on the structure of the program as it is defined by the compiler, and with the limit given by the operating system - on PCs with modern systems, one can think about 1MB for this. I just tested here, 5MB works, 10MB, the program crashes, on a 64bit Linux)

On a PC running a 64-bit OS, this number is limited by the compiler, because of the structure it uses to organize the program into memory, but if the memory is requested from the Operating System, at exercise time, the size of a char vector can be as large as you like, with the computer memory limit. (Nm PC with 4GB, you separate a 300-400MB for the operating system and programs it uses, and could have a process using 3.6GB without sweating a lot.If you are manipulating text files, as it stands, roughly corresponds, to the text equivalent to 1000 bibles) -

But if you have a program that will work with this amount of data in memory it is generally worth using a dynamic memory allocation mechanism.

In short - no, there is no limit to the amount of bytes you can allocate to a vector of type char , except the memory of your computer.

And the error you're having "not being able to read the file" is probably related to the error in the printf call as I pointed out in the comment, just get the & more.

    
22.11.2018 / 19:24