簡體   English   中英

在c中重新分配char **的內存

[英]reallocating memory of char **'s in c

我正在嘗試用c語言編寫一個編譯器,但是已經有一段時間了,並且在為char **類型的對象分配內存時遇到了麻煩。 編碼:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define FATAL 1
#define NONFATAL 0

#define GENERIC 0
#define INVALID_ARG_COUNT 1
#define BAD_FILE_HANDLE 2
#define INVALID_FILE_TYPE 3
#define ACCESS_DENIED 4
#define SUCH_LOOPS_WOW 5
#define NAME_TOO_LONG 6
#define OOM 7

typedef struct {
    char *msg;
    int fatal;
} error_t;

static error_t table[] = {
/*000*/    {"something went wrong that does not have error handling", FATAL},
/*001*/    {"invalid number of arguments", FATAL},
/*002*/    {"file could not be opened", FATAL},
/*003*/    {"file is not a lightning (.lgt) source file", FATAL},
/*004*/    {"access to source file is denied", FATAL},
/*005*/    {"too many symbolic links between target and source", FATAL},
/*006*/    {"file to be passed has too long of a name", FATAL},
/*007*/    {"not enough memory to compile source file", FATAL}
};

void lgt_error(int code) {
    fprintf(stderr, "error: %s\n", table[code].msg);

    if(table[code].fatal) {
        exit(EXIT_FAILURE);
    }
}

char *processedFiles[] = {0};
int size = 0;

void add(char filename[]) {
    processedFiles = realloc(processedFiles, ++size);
    *processedFiles++ = malloc(strlen(filename));
    *processedFiles = filename;
}

int alreadyProcessed(char *filename) {
    char **iterator = &processedFiles[0];

    for(int counter = 0; counter <= size; iterator++, counter++) {
        if(filename == *iterator) {
            return 1;
        }
    }

    return 0;
}

int match(FILE *file, char *directive) {
    char *sequence = malloc(strlen(directive) + 1);

    for(int counter = 0; counter < strlen(directive); counter++) {
        *sequence++ = fgetc(file);
    }

    return strcmp(sequence, directive);
}

char *grabFile(FILE *file) {
    char current = 0;
    char *filename = malloc(1);

    while((current = fgetc(file)) != EOF && (!isspace(current) || current != ';')) {
        sprintf(filename, "%s%c", filename, current);
    }
}

void lgt_process(char *filename, char *translationUnit) {
    add(filename);

    struct stat buf; /* *sigh* have fun porting this to windows dumbass */

    if(stat(filename, &buf) != 0) {
        switch(errno) {
            case EACCES: {
                lgt_error(ACCESS_DENIED);
            }

            case EBADF:
            case ENOENT:
            case ENOTDIR:
            case EOVERFLOW: {
                lgt_error(BAD_FILE_HANDLE);
            }

            case EFAULT: {
                lgt_error(GENERIC);
            }

            case ELOOP: {
                lgt_error(SUCH_LOOPS_WOW);
            }

            case ENAMETOOLONG: {
                lgt_error(NAME_TOO_LONG);
            }

            case ENOMEM: {
                lgt_error(OOM);
            }
        }
    }

    translationUnit = (char*) realloc(translationUnit, strlen(translationUnit) + (size_t) buf.st_size);

    FILE *file = fopen(filename, "r");

    if(!file) {
        lgt_error(BAD_FILE_HANDLE);
    }

    char next = 0;

    while((next = fgetc(file)) != EOF) {
        ungetc(next, file);

        if(next == 'i') {
            if(match(file, "import") == 0) {
                char *nextFile = grabFile(file);

                if(alreadyProcessed(nextFile) == 0) {
                    lgt_process(nextFile, translationUnit);
                }
            }
        }
    }

    fclose(file);
}

int main(int argc, char **argv, char **env) {
    if(argc == 1) {
        lgt_error(INVALID_ARG_COUNT);
    }

    ++argv;

    if(strcmp(".lgt", strrchr(*argv, '.')) != 0) {
        lgt_error(INVALID_FILE_TYPE);
    }

    char *source = malloc(1);

    lgt_process(*argv, source);

    free(source);
}

該問題主要源於預處理器lgt_process。 以下是其生成的錯誤:

dtscode@dtscode-Latitude-E6410 ~/Desktop/lightning $ gcc -o lightning main.c -std=c99
main.c: In function ‘add’:
main.c:50:20: error: incompatible types when assigning to type ‘char *[1]’ from type ‘void *’
     processedFiles = realloc(processedFiles, ++size);
                    ^
main.c:51:20: error: lvalue required as increment operand
     *processedFiles++ = malloc(strlen(filename));
                    ^
char *processedFiles[] = {0};//You can not realloc for array.
int size = 0;

void add(char *filename) {
    processedFiles = realloc(processedFiles, ++size);//need size * object size
    *processedFiles++ = malloc(strlen(filename));//does not let you change the pointer as a base. and It is necessary to ensure the +1 extra for the NUL character.
    *processedFiles = filename;//use the function such as strcpy to copy the string rather than a pointer.
}

應該像下面

char **processedFiles = NULL;
int size = 0;

void add(char *filename) {
    processedFiles = realloc(processedFiles, (size+1)*sizeof(char*));
    processedFiles[size] = malloc(strlen(filename)+1);
    strcpy(processedFiles[size++], filename);
}

一個簡單的字符串向量可能會有所幫助。

#define STRINGVECTOR_CHUNK_ALLOCATION_SIZE 50

typedef struct{
    char** vector;
    unsigned int size;
    unsigned int allocated_size;
} stringvector;

typedef  int (*stringvector_comparer)(char* l, char* r);

void stringvector_create(stringvector* v)
{
    v->vector = malloc(sizeof(char*)*STRINGVECTOR_CHUNK_ALLOCATION_SIZE);
    v->allocated_size = STRINGVECTOR_CHUNK_ALLOCATION_SIZE;
    v->size=0;
}

char* stringvector_at(stringvector* v, int index)
{
    return (v->vector[index]);
}

void stringvector_add(stringvector* v, char* s)
{
    if(v->size+1 >= v->allocated_size)
    {
        v->allocated_size+=STRINGVECTOR_CHUNK_ALLOCATION_SIZE;
        v->vector = realloc(v->vector, sizeof(char*)*(v->allocated_size));
    }   
    v->vector[v->size] = strdup(s); 
    v->size++;
}

void stringvector_destroy(stringvector* v)
{
    int i;
    for(i=0; i<v->size; i++)
    {
        free(v->vector[i]);
    }
    free(v->vector);
}

int stringvector_contains(stringvector* v, char* s, stringvector_comparer comparer)
{
    int i;
    for(i=0; i<v->size; i++)
    {
        if(comparer(v->vector[i], s) == 0) return 1;
    }
    return 0;
}

那么你可以像這樣使用它

  int i;
    stringvector v;
    stringvector_create(&v);
    stringvector_add(&v, "test");
    stringvector_add(&v, "this");
    stringvector_add(&v, "code");

    for(i=0; i< v.size; i++)
    {
        printf("%s\r\n", stringvector_at(&v, i));
    }

    printf("contains 'this': %d\r\n", stringvector_contains(&v, "this", strcmp));
    printf("contains 'This': %d\r\n", stringvector_contains(&v, "This", strcmp));
    printf("contains 'This': %d\r\n", stringvector_contains(&v, "This", stricmp));
    stringvector_destroy(&v);

還是給你...

stringvector processedFiles;
stringvector_create(&processedFiles);

那么您不需要添加功能,只需使用stringvector_add。

既然您說您喜歡RAII,那么我將為該代碼建議一個更好的設計,該設計借鑒一些OO原則:

typedef struct 
{
    char const **data;  // or non-const if you intend to modify in-place
    size_t size;
} StringList;

void add(StringList *list, char const *filename)
{
    void *new = realloc(list->data, (list->size + 1) * sizeof *list->data);
    if ( !new )
        exit(EXIT_FAILURE);  // and/or free list->data, other error handling

    list->data = new;
    list->data[list->size++] = strdup(filename);
}

用法示例:

StringList processed_files = { 0 };

add(&processed_files, "foobar.baz");

如果您打算在任何嚴肅的代碼中使用它,我會考慮改進分配策略(即,將size_t capacity;添加到StringList ,並分配更大的塊;這樣您就不必為每個新條目調用realloc 。 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM