0

How to allocate memory dynamically for struct array pointer. Ex: I have a struct array pointer which is of size 1024 but if i need to increase the size how to do it? I need the arr size to be defined dynamically. For the initialization, I kept the size to 1024. I need to increase the space later when it is more than allocated , how to increase it ??

1
  • Can you post structure declaration / definition ? Commented Feb 2, 2013 at 6:21

2 Answers 2

1

realloc(). Something like

struct foo *ptr;
ptr = malloc(sizeof(*ptr) * 1024);

// later
struct foo *tmp = realloc(ptr, sizeof(*ptr) * 2048);
if (tmp) {
    ptr = tmp;
} else {
    // something nasty happened
    free(ptr);
    exit(-1);
}
Sign up to request clarification or add additional context in comments.

2 Comments

like I have something like struct abc{}; abc *arr[1024]; now I need to change arr[1024] to 2048
You can't do that. One similar thing you can do is variable-length arrays, but it's still not identical.
1

For dynamic memory allocation, using array implemented using linked lists will help. You can use malloc() function to allocate memory to an existing structure node.

If you want the array in a structure object to increase its memory, then you can use linked list instead of array inside structure.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.