1

Node.h

typedef struct Node Node;                                                           
struct Node{
    int rank;
    int marked;
    size_t nSize;
    size_t nCapacity;
    char * name;
    Node * predecessor;
    Table * weights;
    void (*print)(Node * toPrint);  ///< print function for printing all info
    Node ** neighbors;
};
void init_node(Node ** node, char * name, void (*printNode)(Node * n));  

Node.c

void init_node(Node ** node, char * name, void (*printNode)(Node * n)){                                                                                                                                                                                                            
    *node = (Node *) malloc(sizeof(Node));
    if ( node == NULL ){
        assert(NULL);
    }
    (*node)->rank=0;                                                                                                                                                                                                                                                               
    (*node)->marked=0;                                                                                                                                                                                                                                                             
    (*node)->nSize=0;                                                                                                                                                                                                                                                              
    (*node)->name=name;                                                                                                                                                                                                                                                            
    (*node)->predecessor=(Node *)malloc(sizeof(Node));                                                                                                                                                                                                                             
    if ( (*node)->predecessor == NULL ){                                                                                                                                                                                                                                           
        assert(NULL);                                                                                                                                                                                                                                                              
    }                                                                                                                                                                                                                                                                              
    (*node)->nCapacity = INITIAL_CAPACITY;                                                                                                                                                                                                                                         
    (*node)->neighbors=(Node **)calloc((*node)->nCapacity, sizeof(Node *));
    if ( (*node)->neighbors == NULL ){
         assert(NULL);
    }
    (*node)->weights = create(strHash, strEquals, strLongPrint );
    (*node)->print = printNode;
}

main.c

    for (size_t i = 0; i < TEST_AMOUNT ; i++){
        char str[TEST_AMOUNT + 1] ="";
        sprintf(str, "%zu", i);
        Node * n = malloc(sizeof(*n));
        init_node(&n, str, printNode);
        nodes[i] = *n;
        nodes[i].print(&nodes[i]);
    }

    printf("First: %p Second: %p\n", (void *)&nodes[0].name, (void *)&nodes[1].name);
    printf("\n\nCreated an array of %d Nodes\n\n", TEST_AMOUNT);
    for (size_t i = 0; i < TEST_AMOUNT; i++){
        nodes[0].print(&nodes[0]);
    }

So I have this node class, and I keep having this issue when I create a node then initialize it in a for loop like above then assign that value to the array. The array then contains TEST_AMOUNT number of the same node rather then a series of nodes labeled 0-TEST_AMOUNT. I don't seem to have this problem when I create a node and initialize it outside of the for loop, and was curious as to what was causing this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mike Rinos
  • 43
  • 1
  • 9
  • 1
    [don't cast malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Mar 25 '17 at 05:33
  • Where is the rest of your program? See [mcve] – sigjuice Mar 25 '17 at 05:41
  • 1
    `(*node)->name=name;` --> `(*node)->name=strdup(name);`. Also You have many memory leak. – BLUEPIXY Mar 25 '17 at 05:43
  • Also note that a `struct` has no concept of `self` (without an additional layer of OOP), so beyond an educational exercise, why would you want to allocate a pointer to store the same print function in every node when you will have to pass a pointer to the node in a call to the print function to begin with? – David C. Rankin Jan 23 '18 at 04:49

1 Answers1

1

You don't need to call malloc() before calling init_node. init_node() allocates the space for the node, and assigns to the caller's variable -- that's why you have to pass the address of the variable.

You don't show the declaration of the nodes array, but it should be declared as an array of pointers. Then you'll need to indirect through these pointers to access the data in the nodes.

Node *nodes[TEST_AMOUNT];

for (size_t i = 0; i < TEST_AMOUNT ; i++){
    char str[TEST_AMOUNT + 1] ="";
    sprintf(str, "%zu", i);
    init_node(&nodes[i], str, printNode);
    nodes[i]->print(nodes[i]);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612