The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+2 votes
126 views

My doubt is, In the stack, we will push [main()->PTN(3)->PTN(2)->PTN(1)->PTN(0)]       PTN = printTwoNumbers function call

Then according to answer PTN(0) won't pushed into stack, but at least to check if and return we should push it. What's the correct.

My answer is 5, given is 4

asked in DS by Boss (17.9k points) | 126 views
+1
5 looks corrrect. PTN(0) will be pushed.
0
Thanks @Anu Sir
+1
#include <stdlib.h>
#include <stdio.h>

void check(int depth) {
    char c;
    char *ptr = malloc(1);
    printf("stack at %p, heap at %p\n", &c, ptr);
    if (depth == 0) return;
    check(depth-1);
}

int main() {
    check(3);
    return 0;
}

On my machine I see:

So , 4 is the ans.

0
But why i didn't pushed for checking of 0?
+1

Now try

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

void check(int depth) {
    printf("\n%d\n",depth);
    if (depth == 0) return;
    check(depth-1);
}

int main() {
    printf("h");
    check(3);
    return 0;
}

Check : https://ideone.com/w0baKA 

0
output for this :321123 right?
0
what is  mean by  minimum size of stack to avoid stack overflow error?
+1
@Balaji you are printing space allocated for function calls only, but before that one main() call is already present in the stack.

Please log in or register to answer this question.



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

39,751 questions
46,767 answers
140,663 comments
58,538 users