I need to have an array of structs in a game I'm making - but I don't want to limit the array to a fixed size. I'm told there is a way to use realloc to make the array bigger when it needs to, but can't find any working examples of this.
Could someone please show me how to do this?
Start off by creating the array:
structName ** sarray = (structName **) malloc(0 * sizeof(structName *));
Always keep track of the size separately:
size_t sarray_len = 0;
To increase or truncate:
sarray = (structName **) realloc(sarray, (sarray_len + offset) * sizeof(structName *));
Then set the size:
sarray_len += offset;
Happy to help and hope that helps.
The realloc function can be used to grow or shrink an array. When the array grows, existing entries retain their value and new entries are uninitialized. This may either grow in-place, or if that was not possible, it may allocate a new block elsewhere in memory (and behind the scenes, copy all the values over to the new block and free the old block).
The most basic form is:
// array initially empty
T *ptr = NULL;
// change the size of the array
ptr = realloc( ptr, new_element_count * sizeof *ptr );
if ( ptr == NULL )
{
exit(EXIT_FAILURE);
}
The multiplication is because realloc expects a number of bytes, but you always want your array to have the right number of elements. Note that this pattern for realloc means you do not have to repeat T anywhere in your code other than the original declaration of ptr.
If you want your program to be able to recover from an allocation failure instead of doing exit then you need to retain the old pointer instead of overwriting it with NULL:
T *new = realloc( ptr, new_element_count * sizeof *ptr );
if ( new == NULL )
{
// do some error handling; it is still safe to keep using
// ptr with the old element count
}
else
{
ptr = new;
}
Note that shrinking an array via realloc may not actually return memory to the operating system; the memory may continue to be owned by your process and available for future calls to malloc or realloc.
From http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/
/* realloc example: rememb-o-matic */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int input,n;
int count=0;
int * numbers = NULL;
do {
printf ("Enter an integer value (0 to end): ");
scanf ("%d", &input);
count++;
numbers = (int*) realloc (numbers, count * sizeof(int));
if (numbers==NULL)
{ puts ("Error (re)allocating memory"); exit (1); }
numbers[count-1]=input;
} while (input!=0);
printf ("Numbers entered: ");
for (n=0;n<count;n++) printf ("%d ",numbers[n]);
free (numbers);
return 0;
}
Related
In C, I want to fill a dynamic array with each character of a file in each of his boxes.
But instead, when I print my array I have:
0 = []
1 = []
2 = []
3 = []
4 = []
5 = []
6 = []
7 = []
8 = []
9 = []
I have no compilation errors, but valgrind said that I have a:
Conditional jump or move depends on uninitialised value(s)
in my main at the printf.
That's strange, because even with an initialisation in my main:
array_valid_char = NULL;
valgrind keeps getting me that error.
And even if I change to:
printf("%d = [%d] \n", i, array_valid_char[i]);
the display is the same.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int* f(char* name_file_in)
{
FILE *file_in;
int* array_valid_char = malloc(10 * sizeof(int*));
int read_char;
file_in = fopen(name_file_in,"rb");
if(file_in)
{
while ((read_char = fgetc(file_in)) != EOF)
{
*(array_valid_char++) = read_char;
}
}
if(file_in){fclose(file_in);}
return array_valid_char;
}
int main(int argc,char* argv[])
{
int *array_valid_char = malloc(10 * sizeof(int*));
array_valid_char = f(argv[1]);
for (int i = 0; i < 10; i++)
{
printf("%d = [%c] \n", i, array_valid_char[i]);
}
return(0);
}
What is wrong with my code?
You have to keep track of the beginning of the allocated memory which you didn't. (That original allocated chunks address should be returned ).
int* array_valid_char = malloc(10 * sizeof(int*));
int *st = array_valid_char ;
...
return st;
Also you have memory leak - you can omit the malloc in main().
Also you need to free the dynamically allocated memory when you are done working with it.
free(array_valid_char);
And also the memory allocation part would be
int* array_valid_char = malloc(10 * sizeof(int));
or
int* array_valid_char = malloc(10 * sizeof(*array_valid_char));
You want array of int.
Among other points check the return value of malloc and handle it properly.
The correct way to code would be to index into the allocated memory and check whether we are reaching limit of what we allocated - if it is so then reallocate.
Many of us confine ourselves with the thought that sizeof( *ptr)*10
is only good enough for the clear syntax etc but knowing that sizeof returns size_t when multiplied this with other value it is less likely to overflow as in opposed to writing the other way round(which was having int arithmetic) that's a benefit. (chux)
For example: sizeof(something)*int*int will do result in operation with size_t value which will less likely to overflow that int*int*sizeof(int). In second case int*int may overflow.(more likely)
There are some problems in your code:
With *(array_valid_char++), you move your pointer each time you pass on the loop. If you want to use this method, you need to keep a track on the beginning of your array with an other variable. You can also use an iterator array_valid_char[i] that starts at 0 and increments it at each loop turn.
In your main, you malloc your array int *array_valid_char = malloc(10 * sizeof(int*)); but you override it just after with array_valid_char = f(argv[1]);. If you malloc your array in a function and send it back with a return, the memory is still allocated.
In printf, %d is for display a number and %c is for display a character. In your case, you need to use %c. In the other case, you will see the ASCII value of your character.
By the way, you also use an int array to receive char array. It is not a problem now but for some optimisation, you can use char array to take less memory.
Also, don't forget to free the memory you have used when you don't use it anymore, it could be useful in bigger programs.
I am unsure if I am using the realloc function correctly.
In my program, I first ask the user for the size of the array and allocate memory for it using malloc, then initialise it with some values.
Then I want to make the same array twice it's size, using realloc. Here is my code. Am I using realloc to resize int *A correctly?
#include <stdio.h>
#include <stdlib.h>
int main(){
int n;
printf("Enter size of array\n");
scanf("%d", &n);
int *A = (int*)malloc(n*sizeof(int)); //dynamically allocated array
for (int i = 0; i < n; i++) //assign values to allocated memory
{
A[i] = i + 1;
}
A = (int*)realloc(A, 2*sizeof(int)); //make the array twice the size
free(A);
}
When using malloc() , don't cast the return value as said here
You are not using the right size. In hereint *A = (int*)malloc(n*sizeof(int)); the size given to malloc is n*sizeof(int). If you want twice that size, you should call realloc() with n*sizeof(int)*2 instead of 2*sizeof(int)
Handle realloc() failure. In case realloc(A, new_size) fails, A == NULL and you will have memory leak. So, use a different pointer B, check if (B != NULL) and then assign A = B (old_size = new_size). If B == NULL deal with the allocation fail
In this case it's easier to double the n before malloc so you don't have the use realloc, because you know, that you gonna double the arraysize. Using realloc can slow the working of the program, because if you make it longer, and the adresses after the currently allocated memories aren't free, then the whole array will be moved. Also you have the change the last line as it was suggested before me.
So i have this code:
/* Dynamic Array Reader */
/* Parameters:
* n: Number of values to be read
*
* Returns: pointer to the dynamically allocated array
*/
int *dyn_reader(unsigned int n) {
int* array = malloc(n * sizeof (int));
if (!array)
return NULL;
else {
unsigned int num_read = 0;
printf("Enter %u integers so they can be put into this array\n", n);
while (num_read < n) {
num_read += scanf("%d", array + num_read);
}
}
return array;
}
/* Add to array */
/* Parameters:
* arr: Existing array of integers
* num: number of integers in the array before the call
* newval: new value to be added
*
* Returns: pointer to the allocated array
*/
int *add_to_array(int *arr, unsigned int num, int newval) {
int* newarray = realloc(arr, (num+1) * sizeof (int)); //allocate one more space
if (newarray == NULL) //Return original array if failed to allocate
return arr;
//free(arr); //free old array -- this throws an error when i try and free up the old array
newarray[num] = newval;
return newarray;
}
int main()
{
/* testing exercise. Feel free to modify */
int *array = dyn_reader(5);
array = add_to_array(array, 5, 10);
array = add_to_array(array, 6, 100);
array = add_to_array(array, 6, 1000);
return 0;
}
As you can see, the main function calls dyn_reader which allocates enough memory to allow there to be n elements in the array. It reads in integers from the user and returns the array.
Then the main function calls add_to_array which realocates enough memory to add one addition element in the array. If it cant, it returns the original array. If the memory reallocation works, i add newval to the end of the array. In this case, i am using a new pointer to store where the newly reallocated array. How come when i try to free the old array (free(arr);), i get an error. Doesn't that pointer still point to memory on the heap and shouldnt i free it?
No, if the realloc moved to a new area of memory, then it does the "free()" for you (so make sure you don't have any other pointers pointing into that array ! ). The C standard says (at http://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html ) :
The realloc() function shall deallocate the old object pointed to by ptr
The linux man page (at https://linux.die.net/man/3/realloc) makes it more explicit :
If the area pointed to was moved, a free(ptr) is done.
If reallocation was successful, realloc() has handled freeing memory related to the earlier pointer. Note that the pointer may not have even changed.
Another problem with add_to_array() is that the calling function lacks any indication of success/failure.
Using what I have learned here: How to use realloc in a function in C, I wrote this program.
int data_length; // Keeps track of length of the dynamic array.
int n; // Keeps track of the number of elements in dynamic array.
void add(int x, int data[], int** test)
{
n++;
if (n > data_length)
{
data_length++;
*test = realloc(*test, data_length * sizeof (int));
}
data[n-1] = x;
}
int main(void)
{
int *data = malloc(2 * sizeof *data);
data_length = 2; // Set the initial values.
n = 0;
add(0,data,&data);
add(1,data,&data);
add(2,data,&data);
return 0;
}
The goal of the program is to have a dynamic array data that I can keep adding values to. When I try to add a value to data, if it is full, the length of the array is increased by using realloc.
Question
This program compiles and does not crash when run. However, printing out data[0],data[1],data[2] gives 0,1,0. The number 2 was not added to the array.
Is this due to my wrong use of realloc?
Additional Info
This program will be used later on with a varying number of "add" and possibly a "remove" function. Also, I know realloc should be checked to see if it failed (is NULL) but that has been left out here for simplicity.
I am still learning and experimenting with C. Thanks for your patience.
Your problem is in your utilisation of data, because it points on the old array's address. Then, when your call realloc, this area is freed. So you are trying to access to an invalid address on the next instruction: this leads to an undefined behavior.
Also you don't need to use this data pointer. test is sufficient.
(*test)[n-1] = x;
You don't need to pass data twice to add.
You could code
void add(int x, int** ptr)
{
n++;
int *data = *ptr;
if (n > data_length) {
data_length++;
*ptr = data = realloc(oldata, data_length * sizeof (int));
if (!data)
perror("realloc failed), exit(EXIT_FAILURE);
}
data [n-1] = x;
}
but that is very inefficient, you should call realloc only once in a while. You could for instance have
data_length = 3*data_length/2 + 5;
*ptr = data = realloc(oldata, data_length * sizeof (int));
Let's take a look at the POSIX realloc specification.
The description says:
If the new size of the memory object would require movement of the object, the space for the previous instantiation of the object is freed.
The return value (emphasis added) mentions:
Upon successful completion with a size not equal to 0, realloc() returns a pointer to the (possibly moved) allocated space.
You can check to see if the pointer changes.
int *old;
old = *test;
*test = realloc(*test, data_length * sizeof(int));
if (*test != old)
printf("Pointer changed from %p to %p\n", old, *test);
This possible change can interact badly because your code refers to the "same" memory by two different names, data and *test. If *test changes, data still points to the old chunk of memory.
For an assignment, part of what I have to do involves the use of malloc and realloc. I first create a 2D array of chars, the dimensions being the number of lines and the number of characters. I then use malloc to allocate enough memory to store input from some file. Using fgets I read one line in at a time, and store it in the array. This part works fine (or so I think). The problem comes in when I try to reallocate memory for more lines if need be. The program flow is supposed to be like this:
Create a character array of 50 lines, with 80 characters per line (working)
Use fgets to read one line at a time and save it to the array (working)
When 50 lines have been read, reallocate the array to allow for 100 lines (not working)
Keep reallocating as need be (not working)
This is what I have so far (the core of it at least, I omitted irrelevant code):
#define NUMBER_OF_LINES 50
#define CHARACTERS_PER_LINE 80
FILE *inputFile = fopen("some.text", "r");
char **lines;
lines = malloc(NUMBER_OF_LINES * sizeof(*lines));
int i;
for (i = 0; i < NUMBER_OF_LINES; i++)
*(lines+i) = malloc(CHARACTERS_PER_LINE * sizeof(char));
int linesRemaining = NUMBER_OF_LINES;
int reallocCount = 1;
i = 0;
while (!feof(inputFile)) {
if (!linesRemaining) {
reallocCount++;
lines = realloc(lines, (NUM_OF_LINES * reallocCount) * sizeof(*lines));
linesRemaining = NUM_OF_LINES;
}
fgets(*(lines+i), CHARS_PER_LINE, inputFile);
i++;
linesRemaining--;
}
My gut tells me the problem is with the realloc, so I'll explain what I think it's doing.
realloc(lines, (NUM_OF_LINES * reallocCount) * sizeof(*lines));
The first argument, lines, is the pointer I would like to reallocate a certain amount of memory. NUM_OF_LINES is the amount I would like to increase the size by. I multiply this by reallocLinesCount, which is a counter that keeps track of how many sets of 50 lines I ought to have. The sizeof(*lines) part is the size of a pointer to a char.
Thank you for reading and any help is greatly appreciated :)
EDIT: thank you all for the responses; I do not have time right now to read all of the answers right now, but all of your answers will be more thoroughly read and understood once this imminent deadline has passed :D
My motto is: "say what you mean". In your case, you MEAN to enlarge your array when it's not big enough to hold your data.
FILE *in; // you fill this in
int nlines=50; // initial value
char **buffer=malloc(nlines * sizeof *buffer);
int i=0;
for(int i=0; !feof(in); ++i)
{
if(i>=nlines)
buffer=realloc(buffer, (nlines+=50)*sizeof *buffer);
buffer[i]=malloc(80);
fgets(buffer[i], 80, in);
}
realloc() will often find out that there is not enough available room to expand the existing array in-place; in that case, it will create an entirely new array of the specified size, copy the contents of the old array to the new one, deallocate the old array, and return a pointer to the new one. So you should write
char **oldLines = lines;
lines = realloc(...);
(the purpose of oldLines is to keep the original pointer in case realloc() runs out of memory and returns NULL, as per #Brian L's tip).
This is how you should realloc:
char **new_lines = realloc(lines, (NUM_OF_LINES * ++reallocLinesCount) * sizeof(*lines));
if (new_lines)
{
lines = new_lines;
}
else
{
// Memory allocation fails. Do some error handling.
}
Read realloc reference for details.
EDIT
You need more allocation for each new lines.
You are allocating more pointers to lines but not the lines themselves. It is in your code at the beginning:
for (i = 0; i < NUMBER_OF_LINES; i++)
*(lines+i) = malloc(CHARACTERS_PER_LINE * sizeof(char));
So after you allocated your number of lines for each line you allocate the space for the line itself. You forgot to do this for the new lines when you reallocate.
Let's first see how realloc() works. It returns a pointer to new
memory on success, and NULL on failure. On failure, it doesn't
touch the old memory, and on success, it free()'s it, after copying
your data to the new place.
So, the way to use realloc() safely is:
/* allocate memory using malloc() */
ptr = malloc(N * sizeof *ptr);
/* make sure malloc succeeded */
...
/* call realloc() */
new_ptr = realloc(ptr, M * sizeof *new_ptr);
/* see if it succeeded */
if (new_ptr) {
/* okay, we can set ptr */
ptr = new_ptr;
} else {
/* realloc failed, old pointer still valid */
}
So, the first thing is that you are using realloc() incorrectly.
You should never say x = realloc(x, ...);, because if realloc()
fails, you assign x to NULL, and the old memory is lost. This is
a memory leak.
Now, on to your problem. Let's say you have successfully read
NUMBER_OF_LINES lines. Now you want to make room for an additional
NUMBER_OF_LINES lines. You would do:
char **new_lines = realloc(lines, NUMBER_OF_LINES*reallocCount*sizeof *new_lines);
if (new_lines) {
lines = new_lines;
} else {
fprintf(stderr, "realloc failed!\n");
return;
}
/* Now, lines[NUMBER_OF_LINES] to lines[2*NUMBER_OF_LINES-1] are
* available to point someplace useful. They don't point anywhere
* useful yet. We have to allocate memory for them, just like earlier */
start = NUMBER_OF_LINES*reallocCount;
for (i=0; i < NUMBER_OF_LINES; ++i) {
/* You weren't allocating memory here, and were writing to
* lines[0] through lines[NUMBER_OF_LINES-1], which is not what
* you want. */
lines[start+i] = malloc(CHARS_PER_LINE * sizeof *lines[start+i]);
/* check the result of malloc here */
}
fgets(lines[start+i], CHARS_PER_LINE, inputFile);
One final note: it's almost always wrong to use while (!feof(fp))
to read lines from a file.