Lỗi redefination int right trong lập trình c năm 2024
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. Show
Putting source code in header files is a bad idea. If you really want code re-use then do this. Code: // verif_HeapMemRemains.h //VERIFY NEW MEMORY SUCCESSFULLY ALLOCATED > void verif_HeapMemRemains(int buffer);Code: // verif_HeapMemRemains.c > include "verif_HeapMemRemains.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); > } > } If you then want to create a set of functions you want to re-use, then put them all into a library. Code: gcc -c something.c # for all your re-usable source > ar c libmytools.a something.o # list all the other .o files -
Also, getchar() returns an int, not char, so decide should be an int, not char. (On some platforms, char is unsigned, so it can't ever be equal to EOF. Plus, it's easier to do the correct thing--you can just type 3 letters (int) instead of 4 (char).) -Also, getchar() returns an int, not char, so decide should be an int, not char. (On some platforms, char is unsigned, so it can't ever be equal to EOF. That's not the actual reason getchar returns an int. The reason is so that EOF can be chosen to be a bit pattern that can never occur in a char (an "out-of-band" value). Obviously that means it must have more bits and have at least one of those extra bits set. As EOF is defined to be negative, it's highest bit will be set for sure. That is enough to distinguish it from any 8-bit value. It is usually set to -1 (all bits set for 2's complement). And that's the world in a nutshell, an appropriate receptacle. -
Another problem is that, on platforms where char is signed (so that decide can be equal to EOF), it's still broken. It means the program can't tell the difference between actual data which has the value 0xFF and end-of-file. Whether char is signed or unsigned, it's wrong to store the return value of getchar() in a char variable. -
new conceptPutting source code in header files is a bad idea. If you really want code re-use then do this. Code: // verif_HeapMemRemains.h //VERIFY NEW MEMORY SUCCESSFULLY ALLOCATED void verif_HeapMemRemains(int buffer);Code: // verif_HeapMemRemains.c include "verif_HeapMemRemains.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); } } Ohhh. The vital lessons I learned from this above advice is:
I checked back in K & R, and it does mention a program with multiple source files and only one header file, but Chapter 4.5 Header Files is really quite brief. Luckily with your advice I just found flavio's header File tutorial where he explained the basic concept. Actually 're-using' the code would be nice! Thanks a lot! Last edited by andrew.comly; 06-12-2020 at 08:29 PM.Reason: spacing, readability -
1) Corrections of header file misconception; 2) Entailing getchar() input ErrorAfter implementing the above principles, I have the following program: realloc_appendElements_func4.cCode: include
include
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; } 0realloc_appendElem.h Code: include
include
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; } 1expandArrThenVerif_int.c Code: include
include
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; } 2scanIntArr.c Code: include
include
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; } 3verif_HeapMemRemains.c Code: include
include
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; } 4printInt.c Code: include
include
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; } 5Even though now the above program compiles with Code: include
include
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; } 6so 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: include
include
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; } 7My 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: include
include
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; } 8I 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?? Last edited by andrew.comly; 06-14-2020 at 08:25 PM.Reason: clarity 06-14-2020 \> //Get rid of ensuing line feed You also need this after your %d scanf as well. Otherwise the getchar() on the next loop iteration just comes right back with a return character. Mixing getchar() with scanf is always problematic. Not to mention later on, separating out I/O errors from conversion errors when using just simple scanf calls. In short, always use fgets() to read a line into a buffer, then use whatever you like (sscanf, str..., etc) to extract info from the buffer. - 06-14-2020non-concise solutionI found the error. First off, I already knew whenever one uses getchar() to read in a int/char, that you have to use another entailing getchar() to shoo off the wretchedly ensuing new line (ASCII 10). So I uncommented the originally wrongly commented out getchar() after the perror codeblock. I tried again but the problem then eventually resurfaced at a later segment. Then it suddenly dawned on me, even after using scanf the impish newline will befoul stdin, so I then added Code: include
include
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; } 9subsequent to any functions that used scanf. I ran it again and no more runtime/semantic errors. But I then examined the scanIntArr function and found: 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); } } 01. scanf"%d" --> scanf"%d\n" ?? I modified it with a "%d\n" along with getting rid of the getchar() subsequent to the scanIntArr function calls, and that produced the following runtime error: 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); } } 1Anotherwords, with the "%d\n" rather than "%d", the for loop doesn't iterate forward properly. fail2. scanf"%d" > scanf"%d " ??I then tried: 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); } } 2which produced: 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); } } 3"%d "'s two errors:
So with "%d\n" and "%d " both failed, I guess one must amass oodles of: Code: include
include
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; } 9In case anyone knows a way to more be concise[circumvent the need for so many getchar()], please inform me. Last edited by andrew.comly; 06-14-2020 at 11:48 PM. -
In case anyone knows a way to more be concise[circumvent the need for so many getchar()], please inform me. Salem already shared the "secret" with you in the previous post: In short, always use fgets() to read a line into a buffer, then use whatever you like (sscanf, str..., etc) to extract info from the buffer. (Of course, where appropriate you can replace fgets with another function that reads a line, e.g., POSIX getline) Last edited by laserlight; 06-15-2020 at 12:39 AM. |