question

Erri-2940 avatar image
0 Votes"
Erri-2940 asked RLWA32-6355 commented

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


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++
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

YujianYao-MSFT avatar image
0 Votes"
YujianYao-MSFT answered RLWA32-6355 commented

Hi @Erri-2940,

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.







· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

sizeof(buffer) will evaluate to the size of a char*, not to the amount of allocated memory. You need to specify the same value in the second argument of get_bytes that you specified in the call to malloc.

0 Votes 0 ·

Also, for VC++ the UCRT library function _msize will return the number of bytes for a buffer allocated on the heap. Although this function is only documented to be applicable to heap allocations made by malloc, calloc or realloc it will also work for heap allocations made by C++ new since the VC++ implementation uses malloc to do the allocations.


0 Votes 0 ·