我试图在c中实现一个字符串队列。
(使用数组排队)
但是我的代码中有一只未知的苍蝇。
1)我尝试为队列分配一个字符串。我的逻辑错了吗?
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)来自我的代码其他部分的回调填充队列。
我曾想过在堆栈上使用内存,它会起作用还是 malloc 是必须的?