set_element* make_set(int key)
{
         set_element* node = (set_element*) malloc(sizeof(set_element));
         node->parent = node;
         node->rank = 0;
}

In the above code there was no return statement and the function was used like this

elem1 = make_set(f1);

And the code was working as expected. This is very funny. I am not how the hell this was working.

Recommended Answers

All 4 Replies

When a function returns, it uses whatever is in the %rax register as its return value. Your function may inadvertently be placing the right value in %rax.

Note: I won't rely on this behaviour.

>>And the code was working as expected. This is very funny. I am not how the hell this was working.

What compiler are you using? Most recent compilers will produce either an error or a warning because of that omission. If you ignored that warning, then shame on you ;)

I am working with g++. I always remove warning before running it. I don't know what happened. Thanks anyways.

I am working with g++. I always remove warning before running it. I don't know what happened. Thanks anyways.

Turn on warnings; they are useful. If you actually bother to read and understand compiler warnings, it will help you understand what you're doing when you write code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.