rippy
rippy

Reputation: 195

Is there a way create an array with a variable length in c?

Is there any way (other than malloc) for creating an array with a size that the user inputs?

Upvotes: 7

Views: 222

Answers (5)

David R Tribble
David R Tribble

Reputation: 12204

If you don't have VLAs or alloca(), here is an extremely kludgy, but portable, stack-based technique:

int foo(int size)
{
    if (size <= 64*1024)
    {
        unsigned char   arr[64*1024];
        return bar(arr, size);
    }
    else if (size <= 1*1024*1024)
    {
        unsigned char   arr[1*1024*1024];
        return bar(arr, size);
    }
    else if (size <= 64*1024*1024)
    {
        unsigned char   arr[64*1024*1024];
        return bar(arr, size);
    }
    else
        return -1;       // Assume it's too big
}

int bar(unsigned char arr[], int size)
{
    ...your code goes here...
}

int maincode(int size)
{
    // Invoke bar() indirectly, allocating an array
    // on the stack of at least 'size' bytes
    return foo(size);
}

I don't particularly recommend this technique, but it will give you differently-sized blocks of memory allocated on the stack instead of the heap.

Upvotes: 1

paddy
paddy

Reputation: 63451

I assume you're trying to avoid malloc because you don't know about realloc.

Essentially, you should be trying to do roughly what the C++ vector does. Once your array grows to a certain size, realloc it to twice its size.

realloc will grow your memory block if possible, and if not possible it will malloc a new one and copy the contents across.

Upvotes: 0

bspikol
bspikol

Reputation: 96

Well, this is pedantic, but you can write your own heap management code and call your memory allocation function something other than malloc(). I hope this answer is amusing rather than annoying.

Upvotes: 0

NIlesh Sharma
NIlesh Sharma

Reputation: 5655

It all depends on the compiler.

Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++. These arrays are declared like any other automatic arrays, but with a length that is not a constant expression. The storage is allocated at the point of declaration and deallocated when the brace-level is exited. For example:

 FILE *
 concat_fopen (char *s1, char *s2, char *mode)
 {
   char str[strlen (s1) + strlen (s2) + 1];
   strcpy (str, s1);
   strcat (str, s2);
   return fopen (str, mode);
 }

See this for more information.

Upvotes: 6

ArjunShankar
ArjunShankar

Reputation: 23670

One way is to use a VLA (C99 defines what are called 'Variable Length Arrays').

Here is an example:

#include <stdio.h>

int use_a_vla (int n)
{
  int vla[n]; /* Array length is derived from function argument.  */

  vla[0] = 10;
  vla[n-1] = 10;

  return 0;
}

int main (void)
{
  int i;

  scanf ("%d", &i); /* User input.  */

  use_a_vla (i);
}

Upvotes: 2

Related Questions