0
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.

4
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by gusano79
1

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.

Edited by gerard4143: n/a

0

>>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 ;)

0

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

1

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.