Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In C: Created a node, forgot return the pointer to the node, but code still runs as if I returned it

I was implementing some data structures for fun in C, and I was comparing the speed to other data structures I implemented. This one is a closed addressing hash table.

This is the code I used to create a node

hashNode *createNewNode(int data) {
    hashNode *node = calloc(1, sizeof(hashNode));
    node->data.key = data;
    node->isSet = true;
    node->next = NULL;
}

This is the function I wanted to time.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for (int i = 0; i < 5; i++) {
    hashNode *node = createNewNode(arr[i]);
    InsertNode(node, map);
}

(arr is just the first 5000 numbers shuffled)
As you may have noticed the function to create a node doesn’t have a return value, still despite this the node is initialized correctly and all the numbers that were supposed to be in the table were inserted, and only those. How could that happen?

This gimmik only works in VS Code, I tried running it in Visual Studio but it (correctly) doesn’t initialize the node. Does anyone know what is happening?

Edit: okay, I probably didn’t express myself correctly I’m sorry. My question is, how does it work? I know it’s undefined behaviour, but it doesn’t look like something that is supposed to work, yet it worked correctly 5000 times out of 5000 and even if I put some printf here and there it keeps working correctly

>Solution :

If you fail to return a value from a function that is defined to do so, and you then attempt to use the function’s return value, this triggers undefined behavior in your code.

With undefined behavior, there are no guarantees regarding what your code will do. It could crash, it could output strange results, or is could (as in your case) appear to work properly.

Additionally, making seemingly unrelated changes to your code (such as adding an unused local variable or a call to printf for debugging) can change how undefined behavior manifests. Compiling with different optimization settings or with a different compiler can also yield differences.

What might be happening in your case is that the value of node could be sitting in a register, and that register just happens to be the one where the function’s return value would be placed. But again, it’s essentially luck that it’s working this way.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading