-1

I am having trouble allocating nodes for my graph. Here are my structures defined in my header:

typedef struct Graph {
    struct Node**  _node_list;
    int                   _sz;
} Graph;

typedef struct Node {
    char    *code;
    char    **_outgoing_arcs;
    int     _sz;
} Node;

and here are my allocation functions:

Graph* allocGraph(int sz)
{
    Graph *g;
    g = malloc(1000);
    g->_sz = 0; 
    return g;
}

Node* allocNode(char *c, Graph *g)
{
    Node *n;

    n = malloc(1000);
    strncpy(n->code,c,3); 
    g->_node_list[g->_sz] = n; 
    g->_sz += 1; 

    return n;
}

I can allocate graphs just fine, but when I try to allocate nodes my program crashes. Did I not do strncpy correctly? It always takes input of strings 3 long "ABC", "FOO" ...

user2747058
  • 121
  • 8
  • 2
    I don't see you allocating space for `n->code` or `g->node_list`. Or make them point anywhere valid. Perhaps you should take a few steps back, [find a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and read more about how dynamic memory allocation works in C? – Some programmer dude Mar 30 '17 at 11:49
  • allocating space for g and n doesnt allocate space for their variables? – user2747058 Mar 30 '17 at 11:50
  • Can you please explain what you think `g->_node_list[g->_sz] = n` does? – StoryTeller - Unslander Monica Mar 30 '17 at 11:51
  • Yes you allocate space for the *variable*, but it doesn't make the variables *point* to any memory, least of all what you just allocated. Just storage space for e.g. a single `char*` which is `sizeof(char*)` bytes large (typically 4 or 8 bytes). – Some programmer dude Mar 30 '17 at 11:52
  • You should really re-read the section on pointers and memory allocation in your book. Or if it's not good enough, throw it away and find a proper book. There's a question archived somewhere on stackoverflow which has a good list of books! – Nim Mar 30 '17 at 11:52

2 Answers2

0

Node

There is no allocation for memory where your char points to.

Your allocation for Node has been done. But your element *code points no-where or to undefined memory.

You have to additionally allocate memory for this, so:

g->code = malloc(4);  //Allocate memory for 3 characters + 0 at the end.
strncpy(g->code, c, 3);

Graph

Another problem is allocation of pointers for your nodes in graph:

//Allocate memory for pointers. Array of pointers.
h->_node_list = malloc(max_number_of_nodes * sizeof(Node*))

And for both, instead of using malloc(1000) in your cases, use:

g = malloc(sizeof Graph); //For graph
g = malloc(sizeof Node);  //For node

Even better approach is to clear memory after you allocate it.

g = malloc(sizeof Graph); //For graph
memset(g, 0x00, sizeof Graph);

g = malloc(sizeof Node);  //For node
memset(g, 0x00, NodeGraph);

Now your members will be reset and you can start set other pointers and allocations.

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
0

You are assigning values to g->_node_list[g->_sz] but you didn't allocate memory for it. In fact g->_node_list is undefined so it will likely crash the program if you write data to the location it points to.

Hellmar Becker
  • 2,824
  • 12
  • 18