C6262 Function uses '1048604' bytes of stack. Consider moving some data to heap.

Erri 21 Reputation points
2022-05-25T04:48:57.96+00:00

MB is a megabyte size is that my problem here? I need a big buffer what can I do about this error? should I allocate buffer instead? But then I'm using 1 megabyte of memory. allocating fixed the problem but is that what it means by using heap? Meaning allocate data instead of using a declared array like buffer. Thanks for your patience I did get the warning to disappear by allocating. Lastly a question about an html server. Do you send large html files all with one send with a buffer like my buffer or do you send multiple sends with a smaller buffer and what is the normal amount of data people send at one time? Thankyou for your help in giving light to this question. Also I think personally I should use a declared array for this instead because my hardrive is bigger then the amount of ram available on my computer. Maybe I should only get this error when compiling if there isn't enough disk space on my computer for all my arrays, I don't understand stack. If I want an array that big do I have to make it global or something? is there any documentation on the subject that might be worth reading? Thankyou for your patience.

void send_file(int socket, char *filepath, char *header){
        FILE *fileptr;
        char buffer[MB];
        if ((fileptr = fopen(filepath, "r")) == NULL ) {
            send(socket, http_ok, strlen(http_ok), 0); // Send OK
            send(socket, header, strlen(header), 0);
            send(socket, http_not_found, strlen(http_not_found), 0); //sends HTTP 404
        } else {
            send(socket, http_ok, strlen(http_ok), 0); // Send OK
            send(socket, header, strlen(header), 0);
            while (!feof(fileptr)) { //sends the file
                //memset(&buffer, 0, sizeof(buffer));
                fread(&buffer, sizeof(buffer)-1, 1, fileptr);
                send(socket, buffer, sizeof(buffer), 0);
            }
            fclose(fileptr);
        }
    }
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,526 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. YujianYao-MSFT 4,271 Reputation points Microsoft Vendor
    2022-05-25T06:20:20.573+00:00

    Hi @Erri ,

    A buffer is a contiguous area of computer memory that can hold multiple instances of the same data type, such as a character array. The buffer overflow means that when the computer fills the buffer with the number of data bits, it exceeds the capacity of the buffer itself, and the overflowed data is overlaid on the legal data.

    Usually, in an ideal situation, the program checks the data length and does not allow characters that exceed the buffer length. However, since the C language does not have any built-in bounds checking, writing to a character array beyond the end of the array will cause an overflow.

    You could try to use the following code to set the buffer size, and change the data type of sizeof(char) to what you need.

     char *buffer = malloc(sizeof(char) * 10);  
     get_bytes (buffer, sizeof(buffer));  
    

    The size of the data transmitted by the HTML server can be customized, depending on the settings of the server, the amount of data is usually not too large.

    The array that acts as a buffer is not necessarily set to global, it depends on the specific logic of your programThe array that acts as a buffer is not necessarily set to global, it depends on the specific logic of your program.

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.