-1

I when running the following code,I get Realloc() Error : invalid next size error

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define ENLRAGE_SIZE(size) size = size + 10

int checkEndOfFile();
int numIsInArray(int *arr, int num);

/*------
get_set()
Gets the input of the int numbers arranges the set
1. Gets the input (Assumption is that input is valid, as stated in the course booklet)
2. Checks each int value (seperated by whitespace) if has been entered before
3. If hasn't been entered, inputs the value to an array of ints
4. Once reaches EOF terminates the function and returns the array adress
-------*/
int *get_set(){
    
    int inputNum;
    int *setArray = malloc(11);
    int valuesInArray;
    int currentArraySize = 11;
    *setArray = 0; /*setArray[0] - is the counter of the number of values entered */
    
    printf("Hello! please enter a list of full numbers, with whitespace (space or enter) seperating"\
        " each number from the other\n");
    
    while(!checkEndOfFile()){
        
            scanf("%d",&inputNum);
            if(!numIsInArray(setArray,inputNum)){
                            
                valuesInArray = *setArray;
                if(valuesInArray == currentArraySize - 1){
                    ENLRAGE_SIZE(currentArraySize); 
                    setArray = (int *)realloc(setArray, currentArraySize);
                }/*if(valuesInArray == currentArraySize)*/
                
                *(setArray + valuesInArray + 1 ) = inputNum;    
                *setArray += 1;/* increasing the numbers counter*/
            }/*if(!numIsInArray(setArry,num))*/
        
    }/* while(!checkEndOfFile())*/
...

Why I do get this error and how can I fix it?

Tried to realloc space that was already malloc'ed.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 2
    This allocates 11 bytes, not 11 elements: `int *setArray = malloc(11);` Probably want: `malloc(11 * sizeof(int))` – josh poley Apr 20 '23 at 13:25
  • And you typically want to use a temp variable when calling realloc, so that you can test for NULL return. If you just assign directly over your main pointer, you cant free the old memory. – josh poley Apr 20 '23 at 13:28
  • in addition to what others said: how will the caller know how big the array is? I think it would be better to return the size of the array as well. – AndersK Apr 20 '23 at 13:43

1 Answers1

0

The problem with the code is due to the initial allocation and reallocation of memory for setArray, which causes memory corruption when accessing the array elements. Insufficiently determining the size of allocated memory results in issues. To fix this, it is necessary to modify the malloc and realloc calls to consider the integer size during allocation.

int *get_set() {
    
    int inputNum;
    int *setArray = malloc(11 * sizeof(int));
    int valuesInArray;
    int currentArraySize = 11;
    *setArray = 0; // setArray[0] - is the counter of the number of values entered
    
    printf("Hello! please enter a list of full numbers, with whitespace (space or enter) separating"\
        " each number from the other\n");
    
    while(!checkEndOfFile()) {
        
        scanf("%d", &inputNum);
        if(!numIsInArray(setArray, inputNum)) {
                
            valuesInArray = *setArray;
            if(valuesInArray == currentArraySize - 1) {
                ENLRAGE_SIZE(currentArraySize); 
                setArray = (int *)realloc(setArray, currentArraySize * sizeof(int));
            } // if(valuesInArray == currentArraySize)
                
            *(setArray + valuesInArray + 1) = inputNum;
        }
    }
    // ...
}
Kozydot
  • 515
  • 1
  • 6
  • Thanks, it worked. So what is the original meaning of the value in the malloc and alloc functions? when I wrote 11, what did it stand for? – Eitan Ratner Apr 20 '23 at 14:08
  • When you wrote 11 in the malloc and realloc functions, it stood for the number of bytes to allocate. However, this value was incorrect since you intended to allocate memory for 11 integers, not 11 bytes. In C, the size of an integer can vary depending on the platform and compiler, but it is commonly 4 bytes. Therefore, when you wrote malloc(11), you were only allocating memory for 11 bytes, which is insufficient to store 11 integers. – Kozydot Apr 20 '23 at 15:59