0

I just wanted to ask that is there any way to dynamically allocate memory to an existing array of elements at runtime in C? For example.. if i declared int arr[25]; then I would be able to take maximum of 25 integers in my array.. But what if the user wants to enter some more integers.. but we dont know in advance how many? Also I dont want to waste the memory by assigning something like int arr[500]; just to be sure that the user doesnt exceed the upper bound of my array :( I'm really confused and sad as I'm unable to find a solution for this.. Does C even support such kind of thing? If not then in which programming language would it be easier to tackle a problem like this? P.S-> I'm new to programming so I'm sorry if this is a noob question. :/

WhozCraig
66.5k11 gold badges82 silver badges148 bronze badges
asked Sep 3, 2014 at 17:14
3
  • 3
    You've tagged the question with both C and C++, but your question text only mentions C. The correct answers for C and C++ will differ enormously. Which one are you asking about? Please edit your question to remove the wrong tag. Commented Sep 3, 2014 at 17:17
  • possible duplicate of C dynamically growing array Commented Sep 3, 2014 at 17:17
  • If you are new to C, I would strongly recommend C Primer Plus by Steve Prata. The 5th edition is about 30ドル just about everywhere (amazon.ca/C-Primer-Plus-5th-Edition/dp/0672326965) and has everything you need to get up and running quickly. I read an old version of it over a long weekend years ago. Fantastic intro to C. Commented Sep 3, 2014 at 17:42

3 Answers 3

3

you need to do dynamic memory allocation using malloc()/realloc() and release it using free() once you are done.

int* a = malloc(25*sizeof(int));
//use a like array...a[0]...to a[24]
// realloacte if you need to grow it 
a = realloc(a,50);
free(a);

The above logic should be used in C. If you are writing into C++, you should use STL std::vector<T>

answered Sep 3, 2014 at 17:17
Sign up to request clarification or add additional context in comments.

Comments

2

if it is an array you might actually want to check out the calloc() function:

void* calloc (size_t num, size_t size);

It can be used like so:

/* calloc example */
#include <stdio.h> /* printf, scanf, NULL */
#include <stdlib.h> /* calloc, exit, free */
int main ()
{
 int i,n;
 int * pData;
 printf ("Amount of numbers to be entered: ");
 scanf ("%d",&i);
 pData = (int*) calloc (i,sizeof(int));
 if (pData==NULL) exit (1);
 for (n=0;n<i;n++)
 {
 printf ("Enter number #%d: ",n+1);
 scanf ("%d",&pData[n]);
 }
 printf ("You have entered: ");
 for (n=0;n<i;n++) printf ("%d ",pData[n]);
 free (pData);
 return 0;
}

If you need to resize the array later look at the realloc function. (and yes it does keep the original data if succesfull)

answered Sep 3, 2014 at 17:22

2 Comments

dont cast malloc or calloc results, include proper header files
depends whether your compiler is a C or really a C++ compiler. Drop casts for true C compiler.
1

There is a class in c++ that you may find useful, which is called a vector. You can add to the front of a vector: myVector.push_front(myElement) and to the end of a vector: myVector.push_back(myElement). If this type of functionality is very important to you, I would recommend using a c++ vector.

Alternately, you can use the malloc() function in c to request a specific amount of memory at runtime: char *five_chars = malloc(sizeof(char) * 5). Just be sure to call free(five_chars) when you're done with the memory.

answered Sep 3, 2014 at 17:18

1 Comment

sizeof(char) is redundant beacuse it is always 1.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.