Search code examples
cpointersstatic-memory-allocation

C reference static memory from initially allocated array


Im have software requirement not to use dynamic memory, on top of that I trying to make kd-tree with k_dimensions, k=1 (like regular array), k=2 (2 dimensional), and so on.

In my kd_tree.h header, I have a representation of a tree (snippet):

 /*
     * Representation of a kd tree
     */
    typedef struct tree_
    {
        struct tree *left; 
        struct tree *right; 
        //DEFAULT_NUMBER_OF_DIMENSIONS
        float * info;
        float distance_to_neighbor;
    } tree;

In my kd_tree.c implementation, Im attempt to allocate/reference static memory pre allocated in array for new nodes (snippet):

#include  "kdtree.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>



static int current_number_of_kd_tree_nodes=0; 
static int current_number_of_data_points=0; 
static int k_dimensions=0;
//pre-allocated tree nodes array 
static tree tree_space [100];
//pre-allocated data_point array
//TODO: later make sure data_points is at least sizeof(t_memory)/sizeof(tree) * k_dimensions
static float data_points_space [1000];


/*=============================================================================
Function        new_node
Description:    given data create a tree 
==========================================================*/
tree *
new_node (tree * root, float data[], int k_dimensions)
{
    if (NULL == root)
    {

        //dynamic memory allocation is NOT allowed, malloc(sizeof(tree));
        root = &tree_space [current_number_of_kd_tree_nodes];

         //dynamic memory allocation is NOT allowed, root->info = malloc(k_dimensions * sizeof(float));
        for (int i=0;i<k_dimensions;i++)
        {

        //TODO: later deal with fragmentation when you remove nodes
        //HOW DO I set address of the data_points array to root->info pointer
        //info+i represents info[i] 
        root->info = &data_points_space[current_number_of_data_points+i];
        }
        //too keep track of what range of the 
        current_number_of_data_points = current_number_of_data_points + k_dimensions;  

        //initialize array 
        if (!root)
        {
            printf ("insert_tree(),Out of Memory\n");
        }
        //set max dimensions
        set_k_dimensions (k_dimensions);
        root->left = NULL;
        root->right = NULL;
        for (int i = 0; i < get_k_dimensions (); i++)
        {
            root->info[i] = data[i];
        }
        current_number_of_kd_tree_nodes++;
    }

    return root;
}

Im getting crash now. Is this the correct way to reference address of data_points array root->info = &data_points_space[current_number_of_data_points+i];?

Thanks


Solution

  • Just a quick quest. It looks like you want your 'root->info' to be used as an array of data points. In this case, set it to a pointer to the first element of the array.

    I.e. replace the loop:

    for (int i=0;i<k_dimensions;i++)
        {
    
        //TODO: later deal with fragmentation when you remove nodes
        //HOW DO I set address of the data_points array to root->info pointer
        //info+i represents info[i] 
        root->info = &data_points_space[current_number_of_data_points+i];
        }
    

    by a single assignement:

    root->info = &data_points_space[current_number_of_data_points];