简体   繁体   English

在c中实现字符串队列:何时必须使用malloc

[英]implementing strings queue in c: when do I have to use malloc

I have tried to implement a strings queue in c. 我试图在c中实现一个字符串队列。

(Queue using an array) (使用数组排队)

But I get an unknown fly in my code. 但是我的代码中出现了一个未知的问题。

1) I try to assign a string to the queue. 1)我尝试为队列分配一个字符串。 Is my logic wrong? 我的逻辑错了吗?

static void enqueueInSearchEngineQueue(const char* res_name) {

    if (searchEnginesNamesQueue_ItemsCount <= SEASRCH_ENGINES_QUEUE_MAX_SIZE) {

        *searchEnginesNamesQueue[searchEnginesNamesQueue_ItemsCount] = malloc(sizeof(*res_name));

        strcpy(searchEnginesNamesQueue[searchEnginesNamesQueue_ItemsCount] ,res_name);

        searchEnginesNamesQueue_ItemsCount++;
    }
    else
    {
//      freeSearchEngingeQueue();
    }
}

static int existInSearchEngingeQueue(const char* res_name) {
    int i = 0;
    int answer = 0;

    for (i; i < searchEnginesNamesQueue_ItemsCount; i++) {
        if (strcmp(searchEnginesNamesQueue[i], res_name) == 0) {
            answer = 1;
            break;
        }
    }
    return answer;
}

static void freeSearchEngingeQueue() {
    int i = 0;

    for (i; i < searchEnginesNamesQueue_ItemsCount; i++) {
        free(searchEnginesNamesQueue[i]);
    }

    searchEnginesNamesQueue_ItemsCount = 0;
}

static void searchEnginesIcons_download_callback(const char* res_name,
        int success, void *context, char *last_modified) {
    if (success) {

        if (!existInSearchEngingeQueue(res_name)) {
            enqueueInSearchEngineQueue(res_name);

            #ifdef ANDROID
                        DriveToNativeManager_refreshSearchEnginesIconsOnSearchActivity(res_name);
            #elif defined(IPHONE)
                        //TODO
                        refreshIconsOnSearchActivity();
            #endif
        }
    }
}

2) callbacks from other part of my code fill the queue. 2)我代码其他部分的回调填充了队列。

I have thought to use a memory on the stack, would it work or malloc is a must? 我曾经考虑过在堆栈上使用内存,它是否可以工作还是必须使用malloc?

Yes, your code is broken. 是的,您的代码已损坏。

You cannot check the length of a string passed to a function as a const char * using sizeof , you need to call strlen() , and add 1 for the terminator to figure out how memory to malloc() . 您无法使用sizeof来检查作为const char *传递给函数的字符串的长度,您需要调用strlen() ,并为终止符加1以弄清楚malloc()内存方式。

The value of sizeof *res_name is constant, and simply sizeof (char) , ie 1. So you are overwriting memory wildly, which causes undefined behavior. sizeof *res_name的值是常量,只是sizeof (char) ,即1。因此,您将疯狂地覆盖内存,这将导致未定义的行为。

This looks wrong: 这看起来是错误的:

*searchEnginesNamesQueue[searchEnginesNamesQueue_ItemsCount] = malloc(sizeof(*res_name));

You don't show the type definition, but the leading * is highly suspicious. 您没有显示类型定义,但是前导*高度可疑。 Did you really want a dereference there? 您真的要在那里取消引用吗? If that is deliberate, then it looks like it's missing on the following line, and elsewhere. 如果这故意的,那么在下一行以及其他地方似乎都找不到它。

Also, that's not the way to get a length of a string. 另外,这也不是获取字符串长度的方法。 Use strlen instead. 请改用strlen

Try this: 尝试这个:

searchEnginesNamesQueue[searchEnginesNamesQueue_ItemsCount] = malloc(strlen(res_name)+1);

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

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