user3014911
user3014911

Reputation: 169

Weird malloc behaviour

I have encountered a weird malloc behaviour and was hopping someone can shed some light on it.

Here is one function:

struct flowNetwork * createGraph(){
    struct flowNetwork * fN = initFlowNetwork();
    insertAdjMatrix(fN->adjMatrix, 0, 3, 0, 10);
    insertAdjMatrix(fN->adjMatrix, 0, 2, 0, 12);
    insertAdjMatrix(fN->adjMatrix, 0, 1, 0, 5);
    insertAdjMatrix(fN->adjMatrix, 1, 4, 0, 6);
    insertAdjMatrix(fN->adjMatrix, 2, 5, 0, 11);
    insertAdjMatrix(fN->adjMatrix, 4, 5, 0, 5);
    insertAdjMatrix(fN->adjMatrix, 3, 5, 0, 5);
    insertAdjMatrix(fN->adjMatrix, 3, 7, 0, 5);
    insertAdjMatrix(fN->adjMatrix, 4, 5, 0, 5);
    insertAdjMatrix(fN->adjMatrix, 5, 7, 0, 10);
    insertAdjMatrix(fN->adjMatrix, 5, 6, 0, 8);
    insertAdjMatrix(fN->adjMatrix, 7, 8, 0, 16);
    insertAdjMatrix(fN->adjMatrix, 6, 8, 0, 9);
    return fN;
}

Notice the second line calls a function which will return a pointer to a flowNetwork struct. Here is the code for the fuction:

struct flowNetwork *  initFlowNetwork(){
     struct flowNetwork * N = (struct flowNetwork *)malloc(sizeof(struct flowNetwork));
     N->adjMatrix = initAdjMatrix();
     int i;
     for (i = 0; i < NODES; i++)
     {
        N->visitedNodes[i] = 0;
        N->parent[i] = -1;
     }
}

Notice that I never returned a pointer (I originally forgot to add it and noticed this later). Despite not having a return the code work perfectly as if I did have a return pointer statement.
Does anyone know why this works?

Upvotes: 1

Views: 81

Answers (3)

Marcus M&#252;ller
Marcus M&#252;ller

Reputation: 36433

Does anyone know why this works?

pure luck. In fact, C says that "forgetting" the return statement in a function with a non-void return type results in undefined behaviour, i.e. anything might happen, your program might crash, your house might burn down, your compiler might start its own instance of SkyNet...

The point here is that your compiler probably just does this as a means of being nice. I think it shouldn't. Try compiling with -Wall, you will see a lot more warnings.

On x86, this won't happen accidentially. The return value is typically stored in the CPU register %eax, and if you don't explicitely return the pointer, there's no reason it should be in that register. However, the last one to write to %eax in your function is malloc, and since the return value of malloc, the address of the newly allocated space, is the same as the return value you want to produce, namely the address of your new flowNetwork this happens to work. Shudder!

EDIT to be clearer: what I mean with "the compiler is nice" is that it does not scream into your face, telling you you've made a terrible mistake, not that it's magically return the right value.

Upvotes: 4

clearlight
clearlight

Reputation: 12625

You are returning whatever is on the stack which could be a pointer to some useable memory that you're trashing with unpredictable side effects.

Solution: Don't do that.

Upvotes: 0

avinash pandey
avinash pandey

Reputation: 1381

Whenever there is a function which is supposed to return something and it contains a local variable.Local variable occupies the top position in the function call stack.When such a function return without the return statement value on the top of the stack is poped.In your case it is N. Refer to this link

Upvotes: 0

Related Questions