1

Think of a pointer-datatype, for instance to a floating-pointer number.

typedef float* flPtrt;

How would I allocate an array of 3 elements in the local scope? I guess using malloc and free withing the same scope produces overhead, but what's the alternative?

void foo() {
    flPtrt ptr = malloc(sizeof(float)*3);
    // ...
    free(ptr);
}
Niklas R
  • 16,299
  • 28
  • 108
  • 203

4 Answers4

2

If 3 is known at compile time and is small enough, you can declare a local array and use it as a pointer

void foo() {
    float array[3];
    flPtrt ptr = array;
}

If the size is bigger or variable, you have to use dynamic memory as in your example.

Amarghosh
  • 58,710
  • 11
  • 92
  • 121
  • maybe I'm wrong, but I think the whole point of the question is the type that `typedef` aliases can be changed and you want to only fix the `typedef` not the rest of the program. – ouah Feb 02 '12 at 23:40
1

If you know the required size of the array ahead of time, you could just allocate it as a stack variable and avoid heap memory management.

Otherwise, the approach you outlined is appropriate and there is not really an alternative.

Adam Holmberg
  • 7,245
  • 3
  • 30
  • 53
1

Use an array.

void foo(void) // note that "void foo()" is obsolete
{
    float data[3];
    float *ptr = data;
    // ...
}
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • 1
    That `free(ptr)` will be problematic – Drew Dormann Feb 02 '12 at 23:38
  • is `void foo()` not supported by every compiler, or what is the reason for using `void foo(void)` instead? That's actually what I searched for, but I wondered if there was something else. Unfortunately, this is more unnatural syntax, and using `malloc()` and `free()` does always give me a feeling of that is an overhead for allocating memory on the heap and freeing it again. – Niklas R Feb 03 '12 at 13:32
1

I think what your'e looking for is the alloca() function. I'm not sure it's standard C, but it exists in GNU, and it worked on my visual studio. So this is how you use it:

int n = 5;
int* a = (int*) alloca(sizeof(int) * n);

It creates an array of elements on the stack (rather than on the heap with malloc). Advantages: less overhead, no need to free manually (when you return from your method, the stack folds back and the memory is lost) Disadvantage: If you want to return a pointer from a method NEVER use alloca, since you will be pointing at something that no longer exists after exiting the function. One can also argue that the stack is usually smaller than the heap, so if you want larger space use malloc. See more here

Ginandi
  • 843
  • 8
  • 20
  • 1
    Note that Microsoft does not support C99, so you can't use `int a[n];` like Mysticial has suggested, although IMHO it is a good alternative on other platforms – Ginandi Feb 03 '12 at 00:04
  • That's what I searched for. However, I think I will stick to `malloc()` and `free()` or to `float arr[3]; flPtr ptr = arr` due to deprecation and warnings I've read about `alloca()`. – Niklas R Feb 03 '12 at 13:43