Allocation overflows memory in recursive function in C

Asked

Viewed 672 times

1

I created a program that keeps allocating 4 in 4 bytes of memory successively through a recursive function:

#include <stdio.h>
#include <stdlib.h>
#define BUF 2

void overflow(int payload){
    payload = payload - 1;
    int stack = (int)malloc(payload * sizeof(int));
    payload = payload + 1;
    overflow(payload);
}


int main(void){
    overflow(BUF);
    return 0;
}

It turns out that it stops working before it can consume all the memory this would be some kind of control of Windows itself to ensure the integrity of the system or is programming error even?

How could I get around this and let the program consume memory all the way to the top and when I get to the top stop allocating memory?

I’m using windows 7 64 bits

  • Stop working you mean it closes alone? Appears any message if run via CMD?

  • yes, it can only allocate up to 90% of the memory and to

  • Okay, but you just answered "yes" ... I did two questions, I do not know if the yes was for first or second. So it closes alone? Yes or no? Have you tried running your program by calling/compiling via CMD? For example: c:\Users\projeto>g++ meu.cpp -o meu.exe && meu.exe. If you tried which error message appears when it ends?

  • does not return me any error in compiling this code this functional seems to me that something related to language same dynamic allocation data structure and such it runs normal but when it arrives at 90% to allocate memory and windows cancels the execution of the program

  • Have Antivirus on your PC?

  • 1

    I believe your problem may be stack overflow, due to several recursions involved. It seems to me that the standard size is 1 Mbytes: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686774(v=vs.85). aspx

  • Here gave this problem: https://i.stack.Imgur.com/iolXN.png

  • @Guilhermenascimento does not have Antivirus only windows defend that this enabled

  • I think the problem was in recursion even I put inside a while and it worked

  • What version of mingw are you using? So I can test.

  • @Guilhermenascimento 6.3.0

Show 6 more comments

2 answers

2


It is programming error and stack crash because recursion has no end. If it switched to a loop and worked, it is because the loop has a condition that terminates it, otherwise it would have no end, even if there would be no stack burst. But at some distant moment heap also.

#include <stdio.h>
#include <stdlib.h>
#define BUF 2

void *overflow(int payload) {
    if (--payload) {
        int *stack = malloc(payload * sizeof(int));
        overflow(payload);
    }
}

int main(void) {
    overflow(BUF);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Why did you put a pointer and declared a pointer in the stack variable at the return of the void function? I don’t understand.

  • 2

    @Assanges do not remember why I put in the return, I think it was unintentionally, no need. malloc() returns a pointer, so the variable you will receive has to be a pointer. It has compiler that accepts a pointer to be played on an integer, but this is not usually suitable.

0

Try the following code, taken from a response in Stackoverflow:

main() {
  int Mb = 0;
  while ( malloc(1<<20)) ++Mb;
  printf("Allocated %d Mb total\n", Mb);
}

(Declare stdio and stdlib for this example)

  • 3

    Unfortunately, I believe that does not answer the question the post.

  • i did inside a while and it worked as expected stopped my machine needed restart run by own and risk #include <stdio.h>&#xA;#include <stdlib.h>&#xA;&#xA;int main(void){&#xA; while(1){&#xA; int stack = (int)malloc(1 * sizeof(int));&#xA; }&#xA; return 0;&#xA;}&#xA;

Browser other questions tagged

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