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. :/
-
3You'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.Angew is no longer proud of SO– Angew is no longer proud of SO2014年09月03日 17:17:35 +00:00Commented Sep 3, 2014 at 17:17
-
possible duplicate of C dynamically growing arraySneftel– Sneftel2014年09月03日 17:17:38 +00:00Commented 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.Cloud– Cloud2014年09月03日 17:42:09 +00:00Commented Sep 3, 2014 at 17:42
3 Answers 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>
Comments
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)
2 Comments
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.