Search code examples
crealloc

implementation of my own realloc function in c


Here I am trying to write my own realloc function and I stuck on copy the old array to a new array. It is no problem if one file contains only 16 or even less strings each line. The problem occurs when the lines over original setup 16. Which means the problem refers to my realloc function implementation I think. In my program, I set three steps to control the changes of array.

My thought is:

First step: when the file's line less than or equal to 16, give the value to new_array

Second step: when the file's line equal to 17, should malloc one more space. copy the old array to new one and give the current line to the last space

Third step: similar to the second step but the old array is previous extended array.

For now, I am struggling to figure out why it does not work. And when my file has exactly 17 lines, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

The result is

here start
1

2

(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
17

The file 'foo' had 17 lines.

utitlityFunction.c

#include "utilityFunction.h"
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>

char **extendArray(char **oldArray, int oldLen, int newLen){ 
    char **newptr = malloc(newLen * sizeof (char*)); //updated here. No changed for result
    // if(newptr == NULL){
    //  perror("Failed to allocate");
    //  exit(1);
    // }
    memcpy(newptr, oldArray, oldLen);
    free(oldArray);
    return newptr;
}

My main function code as the following.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utilityFunction.h"

int main(int argc, char**argv){
    FILE *filename;
    size_t len = 0;
    char *line = NULL;
    int index;
    int countLine = 0, i = 0;
    char** new_array = malloc(16 * sizeof(char*));
    char** extended_array;
    char* current_line;

    for(index = 1; index < argc; index++){ //loop for all files name you input in the command line
        filename = fopen(argv[index], "r");
        if(filename == NULL){
            printf("The file '%s' did not exist.\n", argv[index]);
        }else{
            while(getline(&line, &len, filename)!=EOF){
                current_line = strdup(line);
                if(new_array == NULL){
                    perror("Failed to allocate");
                    exit(1);
                }
                if(i<16){
                    new_array[i] = current_line;
                }
                else{
                    if(i==16){ //actually index 17
                        extended_array = extendArray(new_array, i, countLine);
                        extended_array[i] = current_line;
                    }else{
                        printf("aaa?\n");
                        extended_array = extendArray(extended_array, i, countLine);
                        extended_array[i] = current_line;
                    }
                }
                i++;
                countLine++;
            }
            //mprintf("%s\n", extended_array[0]);
            //mprintf("-->m%d\n", countLine);
            printf("here start\n");
            int j;
            for(j = 0; j < countLine; j++){
                printf("%s\n", extended_array[j]);
            }
            //print line result after end of file
            printf("The file '%s' had %d lines.\n", argv[index], countLine);
        }
    }
}

I am just lost....


Solution

  • I think your problem comes from the memcpy. void * memcpy ( void * destination, const void * source, size_t num ); You need to memcpy each case of your tab instead of the char** in one call. try it