After implementing the above principles, I have the following program:
realloc_appendElements_func4.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "realloc_appendElem.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
int decide; //If you declare as 'char', then the entailing while loop will not be able to pick up 'n/N' as entered by user, thus will continue forever.
//User decides to append array
printf("Add elements(Y) or quit(N)> ");
while((decide = getchar()) != EOF)
{
//Display user's decision
printf("You just entered %c\n", decide);
//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;
}
realloc_appendElem.h
Code:
int * expandArrThenVerif_int(int *, int);
void scanIntArr(int *, int, int);
void verif_HeapMemRemains(int *);
void printInt(int * , int);
expandArrThenVerif_int.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "realloc_appendElem.h"
//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);
}
scanIntArr.c
Code:
#include <stdio.h>
//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]);
}
}
verif_HeapMemRemains.c
Code:
#include <stdio.h>
#include <stdlib.h>
//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);
}
}
printInt.c
Code:
#include <stdio.h>
void printInt(int* arr, int l)
{
int i;
for(i=0; i < l; i++)
{
printf("\t\"%d\".\n", arr[i]);
}
}
Even though now the above program compiles with
Code:
$ clang -g -std=c99 -Wall -Wvla -Werror -fsanitize=address,undefined realloc_appendElements_func4.c expandArrThenVerif_int.c scanIntArr.c verif_HeapMemRemains.c printInt.c
so I have made significant progress thanks to Salem, christop and john.c vital corrections, thanks a lot!
Entailing Question
However the user input part for decision is still screwed up, with the following runtime error:
Code:
a@a-TP-X200:/media/a/LG/AC/Learn/Programming/C/arrays/reallocProject$ ./a.outEnter integer array's 0 's element> 0
Enter integer array's 1 's element> 1
Enter integer array's 2 's element> 2
"0".
"1".
"2".
Add elements(Y) or quit(N)> You just entered
<<Not until here does the program wait until I can enter code!!!>>>
My target behavior for the program is to ask "Add elements(Y) or quit(N)>", and then at that point GIVE ME A CHANCE TO ANSWER. But when I want it to give me a chance to answer it jumps ahead and prints the data confirmation line:
Code:
//Display user's decision
printf("You just entered %c\n", decide);".
I had previously said
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.
so I can repost this question if someone tells me to or doesn't answer, but the entailing question is:
How to get the program not to jump??