简体   繁体   English

在c中实现自己的realloc函数

[英]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. 在这里,我试图编写自己的realloc函数,并且坚持将旧数组复制到新数组。 It is no problem if one file contains only 16 or even less strings each line. 如果一个文件每行仅包含16个甚至更少的字符串,这没有问题。 The problem occurs when the lines over original setup 16. Which means the problem refers to my realloc function implementation I think. 该问题在原始设置16上的行出现时发生。这意味着问题与我认为的realloc函数实现有关。 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 第一步:当文件的行数小于或等于16时,将值赋给new_array

Second step: when the file's line equal to 17, should malloc one more space. 第二步:当文件行等于17时,应多分配一个空间。 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: 当我的文件正好有17行时,例如:

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 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.... 我只是迷路了...

I think your problem comes from the memcpy. 我认为您的问题来自于memcpy。 void * memcpy ( void * destination, const void * source, size_t num ); void * memcpy(void *目标,const void *源,size_t num); You need to memcpy each case of your tab instead of the char** in one call. 您需要一次调用一次标签中的每种情况,而不是char **。 try it 试试吧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM