在c中重新分配char **的记忆

时间:2014-09-09 04:35:09

标签: c linux memory char

我正在尝试用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));
                    ^

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

也许这可能会有所帮助,一个简单的字符串向量......

#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。

答案 2 :(得分:0)

既然你说你喜欢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