1

I have an char array of fixed size in C application. I am passing that array to some function and from there I am passing it to multiple functions. So that the array gets filled in some of the functions based on some condition. Since I am sending a fixed size array I am facing problem when I copy data to it if the size is more than the array size. I know that I have to make that char array dynamic but as I said that array gets filled in multiple functions and size will be different. So do I need to dynamically allocate the array wherever it gets filled? Consider the array gets filled in 30+ different functions. Or is there a way to do a minimal modification?

5
  • Use std::vector...you would time for other stupid things. Commented Nov 14, 2014 at 5:59
  • Your question title says C and tag says C++.. Don't confuse people. Commented Nov 14, 2014 at 6:00
  • vector is always best solution in such situations Commented Nov 14, 2014 at 6:01
  • 2
    "array gets filled in 30+ different functions." This doesn't sound very encouraging. Encapsulate more. Commented Nov 14, 2014 at 6:07
  • @n.m. I have around 30 modules in my project. So I am passing the array from main function to these modules. Modules will be called based on user selection. Commented Nov 14, 2014 at 6:11

4 Answers 4

3

As your question title says C, IMO the best approach will be to decalre a pointer of that particular variable type in your main() function, pass the address of that pointer [essencially a double-pointer] to other functions and allocate memory dynamically.

You can keep on passing the pointer to all other functions. Inside every called functions, measure the amount of memory required to put the data [from that particular function] and use realloc() to increase the available memory.

As mentioned by UncleO, the required pointer should be the pointer to array [i.e, a double pointer]

EDIT


  1. For the very first time allocating memory to that pointer, you can use malloc() or calloc().

  2. From next time onwards, to extend [resize] the amount of memory, you need to use realloc()

Sign up to request clarification or add additional context in comments.

6 Comments

so do I need to check the memory in all the called functions?
No, you need to have some growing function which does the check.
@impulse if you can allocate the exact amount of memory required form each function, then you don't need to check for available memory. You can assume realloc() to be working as appending . Check my edit.
You should talk a little more about realloc() and its syntax.+1
realloc() can change the location of the pointer, so this won't work.
|
0

You don't pass an array to a function in C, although it appears that way. What gets passed is a pointer to the first element of the array. The pointer is passed by value. That is, the value of the pointer (the memory location) is copied into a local variable. The contents of the array can be changed using this local variable.

If you use malloc() or realloc() with this local variable, then the pointer that was "passed in" won't be affected. realloc() may resize the memory, but it can also free that memory and allocate some new memory to the local variable.

If you want to change the array pointer, then you should pass in a pointer to the pointer. The thing the pointer points to is what is changed. This is a bit more cumbersome. But this way you can allocate more memory is needed.

#include <stdlib.h>    

char* arr;

void changeit(char** arrptr)
{
    *arrptr = realloc(*arrptr, 20*sizeof(char));
}

void main (void)
{
    arr = malloc(10*sizeof(char));

    changeit(&arr);
}

Comments

0

To function that do not alter the array, pass the array address and size.

int foo1(const char *array, size_t size, ...)

To each function that does not change the array size, pass array address and array size

int foo2(char *array, size_t size, ...)

To functions that may alter the array size, pass the address of the address of the array and the address of the size.

int foo3(char **array, size_t *size, ...)

Code could put these two variables together

typedef struct {
  size_t size;
  char array;
} YetAnotherArrayType;

Comments

0

Chux,

There's a little typo at then end of your post. I think you mean:

typedef struct {
  size_t size;
  char* array;
} YetAnotherArrayType;

You didn't make array a pointer type.

If the poster is handcrafting a container, the classic solution is to track both a size and a capacity. In that model you allocate the array to some initial a capacity and set size to 0. You then track its population causing it grow by some chunk size or factor each time it fills up. Frequent reallocation can be a massive performance drain and by the sounds of the program in question such behaviour seems likely.

Comments

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.