0

I'm trying to create cluster with dynamic array objects.

Struct definitions are following:

struct obj_t {
    int id;
    float x;
    float y;
};

struct cluster_t {
    int size;
    int capacity;
    struct obj_t *obj;
};

Function for adding object to cluster is:

void append_cluster(struct cluster_t *c, struct obj_t obj)
{
    if(c->capacity < (c->size + 1))
    {
        c = resize_cluster(c, c->size + 1);
    }
    if(c == NULL)
        return;
    c->obj[c->size] = obj;    //at this point program crashes.
    c->size++;
}

EDIT: Here is resize_cluster() function:

struct cluster_t *resize_cluster(struct cluster_t *c, int new_cap)
{
    if (c->capacity >= new_cap)
        return c;

    size_t size = sizeof(struct obj_t) * new_cap;

    void *arr = realloc(c->obj, size);
    if (arr == NULL)
        return NULL;

    c->obj = (struct obj_t*)arr;
    c->capacity = new_cap;
    return c;
}

EDIT 2: Here is cluster initialization:

void init_cluster(struct cluster_t *c, int cap)
{
    c = malloc(sizeof(struct cluster_t));
    c->size = 0;
    c->capacity = cap;
    c->obj = (struct obj_t*)malloc(cap * sizeof(struct obj_t));
}

I can't figure out why program crashes when I try to add the object to the array in cluster. Is accessing array this way wrong? If so, how should I access it?

5
  • 1
    Can we seeresize_cluster()? Commented Dec 1, 2017 at 15:18
  • 2
    How are you initializing your cluster_t? Commented Dec 1, 2017 at 15:27
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a minimal reproducible example. Commented Dec 1, 2017 at 15:30
  • I have added cluster initialization and resizing functions to question. Commented Dec 1, 2017 at 15:31
  • 2
    You are passing c by value to init_cluster(). So whatever you are passing as an argument remains uninitialized. You could change to: struct cluster_t * init_cluster(int cap) {....;return c;}. Commented Dec 1, 2017 at 15:33

1 Answer 1

3

The issue is the call to init_cluster(). The c parameter is passed-by-value, so whatever you are sending remains unmodified:

struct cluster_t * c;
init_cluster(c, 1);
// c is uninitialized!

One fix would be to pass a pointer to an object:

struct cluster_t c;
init_cluster(&c, 1);

Then remove c = malloc(sizeof(struct cluster_t)); from init_cluster();

Or, you could create an alloc_cluster function:

struct cluster_t * alloc_cluster(int cap)
{
    c = malloc(sizeof(struct cluster_t));
    c->size = 0;
    c->capacity = cap;
    c->obj = malloc(cap * sizeof(struct obj_t));
    return c;
}

And call it like:

struct cluster_t *c = init_cluster(1);
Sign up to request clarification or add additional context in comments.

1 Comment

Removing c = malloc(sizeof(struct cluster_t)); from init_cluster() works as I wanted. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.