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.


  • 06-11-2020
    Lỗi redefination int right trong lập trình c năm 2024
    and the hat of int overfl
    Lỗi redefination int right trong lập trình c năm 2024
    ---
    \> I prefer header files because: Except - it doesn't work!

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

-
  • 06-12-2020

    Lỗi redefination int right trong lập trình c năm 2024
    Registered User

    -

    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).)

    -
  • 06-12-2020

    Lỗi redefination int right trong lập trình c năm 2024
    Registered User

    -
    Lỗi redefination int right trong lập trình c năm 2024
    Originally Posted by christop

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.

-
  • 06-12-2020

    Lỗi redefination int right trong lập trình c năm 2024
    Registered User

    - Yes, I know. I just wanted to point out one problem that's caused by storing the return value of getchar() in a char instead of int.

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.

-
  • 06-12-2020

    Lỗi redefination int right trong lập trình c năm 2024
    Registered User

    -

    new concept

    Lỗi redefination int right trong lập trình c năm 2024
    Originally Posted by Salem

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); } }

Ohhh.

Lỗi redefination int right trong lập trình c năm 2024
I never did anything like this before, guess I really was clueless. Time to sail on into uncharted waters.

The vital lessons I learned from this above advice is:

  1. Making programs with multiple *.c files exists. ** Originally I thought that one program could only have one *.c file and multiple *.h files, this stems from me maybe misunderstanding one of the header file tutorials that I encountered before online. **
  1. When encountering programs that are too long, you MAY organize by moving functions into separate files ending in *.c, and for the function declarations those are the things that should be placed in the *.h files.
  1. *.c files' theme is code source files, not just the main program.

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

-
  • 06-14-2020

    Lỗi redefination int right trong lập trình c năm 2024
    Registered User

    -

    1) Corrections of header file misconception; 2) Entailing getchar() input Error

    After implementing the above principles, I have the following program: realloc_appendElements_func4.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; }

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

Lỗi redefination int right trong lập trình c năm 2024
and the hat of int overfl
Lỗi redefination int right trong lập trình c năm 2024

-

\> //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-2020

Lỗi redefination int right trong lập trình c năm 2024
Registered User


non-concise solution

I 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:

  1. First iteration delayed
  2. After program run still needed a getchar() to dispel the doggedly impish 'newline'. fail

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.

-
  • 06-15-2020

    Lỗi redefination int right trong lập trình c năm 2024
    C++ Witch
    Lỗi redefination int right trong lập trình c năm 2024


    Lỗi redefination int right trong lập trình c năm 2024
    Originally Posted by andrew.comly

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:

Lỗi redefination int right trong lập trình c năm 2024
Originally Posted by Salem

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.
Lỗi redefination int right trong lập trình c năm 2024
Originally Posted by Bjarne Stroustrup (2000-10-14) I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.