dynamic memory allocation in a struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • superdad
    New Member
    • Sep 2008
    • 1

    dynamic memory allocation in a struct

    Hello;
    I have 2 questions:
    1- I know you can allocate memory to an array dinamically in C and C++. But, can this be done if the array is part of a structure?
    2- Can the size of a dynamic array automatically grow up to a maximum or do you have to increase it when needed yourself?

    This is my exact problem:
    I want to have a table of a fixed number of rows like 30. These are for example nodes in a comupter network. Now, for each node, I want to keep track of the paths that it can be on. These would be in a column of the table named "paths". Each node may have hundreds of paths. So, I want to define the table as a struct and the "paths" as a big array. However, one node may end up with 300 paths and another with 2000 paths. I don't know this in advance.
    Moreover, For each path, I need to keep track of the nodes that it passes through. So, each path is also defined as an array of a length let's say 10.
    Below, I have specified how I am doing it now. But obviously, for large values I get an stack overflow error.

    typedef struct //A row of the global routing table
    {
    int node;
    int no_paths;
    path_info paths[600]; //Plan for 600 paths per node.
    }node_routing_i nfo;

    typedef struct
    {
    int path_id;
    int length;
    int prev_hops[10]; //maximum 10 hops per path
    }path_info;

    node_routing_in fo global_routing_ info[30]; //no_elem= no_nodes.

    P.S: If this cannot be done with structs, how can I do it with multi-dimentional arrays?

    Your help is greatly appreciated.
    Thanks
    Ali
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    If you are doing this in C++, you can use vectors. If you are doing it in C, you will have to stick with dynamic arrays. Vectors grow in size automatically when they get filled up. So if this in C++, you can do:

    [CODE=cpp]
    typedef struct //A row of the global routing table
    {
    int node;
    int no_paths;
    vector<path_inf o> paths; //As many paths as you want.
    }node_routing_i nfo;
    [/CODE]

    But this looks like C, so:

    [CODE=cpp]
    typedef struct //A row of the global routing table
    {
    int node;
    int no_paths;
    int no_paths_alloca ted;
    path_info *paths; //Dynamic array of paths
    }node_routing_i nfo;
    [/CODE]

    You will have to check whether there is enough space left whenever you add a path, and allocate more if there isn't. I will just give you the code for this. Warning: I wrote it on the spot and haven't tested it.

    [CODE=cpp]
    // Check whether there's room to add another path.
    if (global_routing _info.no_paths >= global_routing_ info.no_paths_a llocated) {
    // There isn't.

    // Make a pointer to ten more paths than there already were.
    path_info *temp_paths = malloc(sizeof(p ath) * (no_paths_alloc ated + 10));

    // Copy the old paths into it.
    for (int i = 0; i < global_routing_ info.no_paths; i++)
    temp_paths[i] = global_routing_ info.paths[i];

    free(global_rou ting_info.paths ); // Delete the old paths.

    // Give the routing info object the new paths.
    global_routing_ info.paths = temp_paths;

    // And don't forget to change no_paths_alloca ted.
    no_paths_alloca ted += 10;
    }

    // Continue as if nothing had happened.
    [/CODE]

    This is what the vector object does for you, behind the scenes. Use one if you can.

    Hope this helps.
    Last edited by boxfish; Sep 22 '08, 03:26 PM. Reason: Trying out [CODE=cpp] tags to make comments show up.

    Comment

    Working...