简体   繁体   中英

Function returns intended value even without the return statement

I have here a function get_stack that instantiates a stack and should return a pointer to the stack, but doesn't. Still, the program continues and updates the stack correctly.

#include <stdio.h>
#include <stdlib.h>    
#define STACKLIM 1000


struct stack {
    char data[STACKLIM];
    int top;
};

typedef struct stack* Stack;

Stack get_stack() {
    Stack s = (Stack)(malloc(sizeof(struct stack)));
    s->top = -1;
    //return s;
}

void push(Stack s, char val) {
    if(s->top == STACKLIM) {
        printf("ERROR: Stack Overflow\n");
    }
    else {
        s->top += 1;
        s->data[s->top] = val;
    }
}

void display(Stack s) {
    int i;
    printf("Stack -> ");
    for(i = 0; i <= s->top; i++) {
        printf("%c ", s->data[i]);
    }
    printf("\n");
}

int main() {
    Stack d = NULL;
    d = get_stack();
    push(d, 'a');
    display(d);        
    return 0;
}

It's like the return statement does not matter. What could be the reason for this? I am using gcc 4.5.2 on an RH5 machine.

Whether or not the compiler diagnoses it, it's always an error to fall off the end of a function (ie reach the closing } ) without returning a value if the function has a non- void return type. It's Undefined Behavior .

Now, Undefined Behavior can manifest itself in many different ways. It can result in your program crashing, receiving the wrong value, erasing your hard drive, sending a nasty email to your mother, or appearing to work successfully with no other ill effects . All of these behaviors are permitted by the C language standard. Just because the code may appear to work correctly, that doesn't mean it's actually correct and working.

If you crash when you invoke Undefined Behavior, you should consider yourself lucky, since those are usually easy to diagnose. But if your program silently corrupts memory and then later crashes in an "impossible" situation, you're going to be in for one heck of a difficult debugging job.

Moral of the story: compile your code with a high warning level (eg use the -Wall compiler option; possibly also -Wextra and/or -pedantic if you can bear it; -Werror also strongly recommended). Any compiler worth its salt will catch this kind of error, but often times not at the default warning level .

It was answered before: Function returns value without return statement .

The gist of it: when doing d = get_stack(), the system looks into some place in memory where it expects to find the return value. Whatever trash is in that place in memory - counts as the return.

It may be that "eax register" or a slot in the call stack. You are just lucky that the return from malloc() got stuck in that spot. Add a few extra commands into get_stack() and the miracle will disappear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM