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?