Thread: memory allocation

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    6

    memory allocation

    hi. do i have to always allocate memory when i use arrays? for instance, i need to use an array of ints where length is a variable. i wrote "int a[n];" should i change it to "int a[n] = malloc(sizeof(int)*n);"?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cAsk
    do i have to always allocate memory when i use arrays?
    Of course, but if you just define:
    Code:
    int a[N];
    where N is a constant, then memory will be allocated, but you need not worry about manually managing the memory.

    Quote Originally Posted by cAsk
    i need to use an array of ints where length is a variable. i wrote "int a[n];" should i change it to "int a[n] = malloc(sizeof(int)*n);"?
    If you are compiling with respect to C99, or with respect to C11 where the feature is supported, or with respect to an earlier version of C where the feature is available as a language extension, then you can use variable length arrays, e.g.,
    Code:
    int a[n];
    where n is not a constant. However, such an array cannot be resized during its lifetime. Furthermore, because of the caveats about compiler support, you may choose to use the two other more portable options:
    • Allocate an array as large as you will ever need, then use as much as you need. Unfortunately, this may result in a colossal waste of space.
    • Use dynamic memory allocation with malloc/free and related functions. This allows you to resize the dynamic array with realloc. However, you would need to declare a pointer, not an array, e.g.,
      Code:
      int *a = malloc(sizeof(a[0]) * n);
      and then you should check that malloc did not return a null pointer, and remember to free.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    6
    thanks for the ellaborated and quick response
    btw, yes i use C99

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    You could also use

    Int *a = malloc(sizeof(*a) * n);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory allocation
    By cs32 in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 04:28 PM
  2. Memory Allocation
    By robid1 in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2004, 04:52 AM
  3. Memory allocation
    By ski_photomatt in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 01:52 PM
  4. memory allocation
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-09-2002, 01:23 PM
  5. Memory Allocation
    By Marcelo in forum C Programming
    Replies: 1
    Last Post: 10-26-2001, 08:43 AM