I am trying to make a short mwe of realloc, more specifically how it can be used to expand integer arrays. Unfortunately due to the error:
Code:
a@a-TP-X200:/media/a/LG/AC/Learn/Programming/C/arrays$ clang -g -std=c99 -Wall -Wvla -Werror -fsanitize=address,undefined realloc_appendElements_func4.c
In file included from realloc_appendElements_func4.c:5:
./verif_HeapMemRemains.h:2:7: error: redefinition of 'verif_HeapMemRemains'
void verif_HeapMemRemains(int * buffer)
^
./verif_HeapMemRemains.h:2:7: note: previous definition is here
void verif_HeapMemRemains(int * buffer)
^
1 error generated.
I prefer header files because:
Organization:
For future programs that use same/similar functions, I can look up past programs WAY QUICKER than pondering for hour after hour on remembering that I put the function for scanning in an integer Array in a program designed to demonstrate how to use 'remalloc'" or a program designed to track how many segments in a millipede's body with age or whatever.
and don't care whether it is reusable or not.
My program:
realloc_appendElements_func4.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "printInt.h"
#include "expandArrThenVerif_int.h"
#include "verif_HeapMemRemains.h"
#include "scanIntArr.h"
//PURPOSE
// First allocate enough for just a few first numbers. Then to demonstrate "realloc"'s ability to resize an already existing array take in more numbers to add to the new space.
//Main Program
int main (int argc, char** argv)
{
int * buffer;
//1ST ARRAY
int currentSize = 0;
int increaseSize = 3;
int firstElemOfNewAnnex = currentSize;
currentSize += increaseSize;
//Integer Array 1: Allocate enough memory for 10 integers
buffer = (int *) malloc (currentSize*sizeof(int));
//VERIFY NEW MEMORY SUCCESSFULLY ALLOCATED
verif_HeapMemRemains(buffer);
//Scan in new values for the buffer
scanIntArr(buffer, firstElemOfNewAnnex, currentSize);
//DEBUG
printInt(buffer, currentSize);
//Expand Array
char decide;
//User decides to append array
printf("Add elements(Y) or quit(N)> ");
while((decide = getchar()) != EOF)
{
//Get rid of ensuing line feed
getchar();
//Upon user input 'n' terminate loop
if(decide == 'n' || decide == 'N')
{
break;
}
//Number of elements to add to current array
printf("Enter quantity of elements to add to end of current array> ");
//Verify scan function succeeded scanning Data
if((scanf("%d", &increaseSize)) != 1)
{
perror("scanf didn't return \"1\"/n");
//printf("%d/n", stdin);
exit(EXIT_FAILURE);
}
////Get rid of ensuing line feed
//getchar();
//Record which element new space will begin on
firstElemOfNewAnnex = currentSize;
currentSize += increaseSize;
//REALLOC CURRENT BUFFER, VERIFY HEAP HAD ENOUGH REMAINING MEM
buffer = expandArrThenVerif_int(buffer, currentSize);
//Scan in second set of values for the buffer
scanIntArr(buffer, firstElemOfNewAnnex, currentSize);
//DEBUG
printInt(buffer, currentSize);
//User decides to append array
printf("Add elements(Y) or quit(N)> ");
}
free (buffer);
return 0;
}
verif_HeapMemRemains.h
Code:
//VERIFY NEW MEMORY SUCCESSFULLY ALLOCATED
void verif_HeapMemRemains(int * buffer)
{
//Check if heap had enough memory to allocate above space (out of memory)
if (buffer==NULL)
{
printf("Error allocating memory!");
//Free the initial memory block.
free (buffer);
exit (EXIT_FAILURE);
}
}
scanIntArr.h
Code:
//SCAN IN VALUES TO THE ARRAY
void scanIntArr(int * buffer, int s, int l)
{
int i;
//Initialize "buffer's" values
for(i = 0 + s; i < l; i++)
{
printf("Enter integer array's %d 's element> ", i);
scanf("%d", &buffer[i]);
}
}
expandArrThenVerif_int.h
Code:
#ifndef verif_HeapMemRemains
#include "verif_HeapMemRemains.h"
#endif
//REALLOC CURRENT BUFFER, VERIFY HEAP HAD ENOUGH REMAINING MEMORY
int * expandArrThenVerif_int(int * buffer, int currentSize)
{
/*get more memory with realloc*/
//Assign pointer to the newly allocated memory to current variable "buffer"
buffer = (int*) realloc(buffer, (currentSize)*sizeof(int));
//***
//VERIFY NEW MEMORY SUCCESSFULLY ALLOCATED
verif_HeapMemRemains(buffer);
return(buffer);
}
Notice how in expandArrThenVerif_int.h I've attempted to use a header file. Even though it is realitively easy to find a lot of tutorials on how to understand the "theory" of header files, I couldn't find any concrete real life examples. Any recommendations?
Optionally, before I moved the functions outside of the main program, the decide = getchar() clause fails (can see when running in GDB) and no way to terminate loop, but that is not the main question, and I can repost for website organization standards if venerable contributors think appropriate.