Thread: dynamically allocating an array

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    50

    dynamically allocating an array

    Im kind of new to C, and I have a question about dynamically allocating arrays.

    I created an array, int array[n], but this array cant be bigger than 10, so the user will have to enters a number less than 10, so the array is a fixed size. It also warns the user if he entered n > 10.

    But now i want to ask for the size of the array n from the user and dynamically allocate memory for that size of array And by dynamically allocating the array, so there will be no need to warn about size limits.

    I think it's similar to previous part, do i just create the array like " new array[n]"?

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The keyword new is not part of the C language
    malloc is the function you need malloc - C++ Reference
    Declare the array as a pointer and then malloc the space you need

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you are using "new", then you are using a C++ compiler, not a C compiler. They are frequently put into the same package of compilers, but they AREN'T the same, and will lead to a lot of confusion.

    C programs should have a file extension on the filename of dot c (.c), whereas C++ programs should have a file extension on their filename of dot cpp (.cpp).

    To dynamically allocate an array, you use malloc() in C, "new" does nothing in C. If you want to ask about C++, go to the C++ forum.

    Include <stdlib.h> file in your program, and read up on malloc() in your textbook or help feature.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    oh ok, yeah i am using C, not C++, so malloc it is

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Also look up realloc() - if you need to resize after the first malloc() - and free(), unless you enjoy the prospect of leaking.

    Incidentally, malloc() doesn't eliminate the need for checking size limits. The amount of memory available via malloc() is finite .... large, but still finite.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by grumpy View Post
    Also look up realloc() - if you need to resize after the first malloc() - and free(), unless you enjoy the prospect of leaking.

    Incidentally, malloc() doesn't eliminate the need for checking size limits. The amount of memory available via malloc() is finite .... large, but still finite.
    If you use realloc remember to increase the size of the array (which has some advantages in comparison with the linked lists you are going to learn in the future ) to it's double size.It is vital for the performance of your program to always increase by x% (a percentage quantity) every time the array is full and not by a constant quantity.In first case the complexity remains O(n), but in the second case it approaches O(nē) <--that's a critical difference!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamically allocating a 2d array
    By ali.franco95 in forum C Programming
    Replies: 3
    Last Post: 10-18-2011, 09:28 AM
  2. Dynamically allocating array size
    By JFonseka in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 06:24 AM
  3. Dynamically allocating 3D array
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 02-26-2008, 04:07 PM
  4. Dynamically Allocating a 2D Array.
    By LightsOut06 in forum C Programming
    Replies: 17
    Last Post: 10-09-2005, 12:55 AM
  5. dynamically allocating a 2dim array
    By agerealm in forum C++ Programming
    Replies: 14
    Last Post: 03-10-2004, 02:40 PM