我想釋放一個變量列表的指針與freeS宏下面:freeS(s1,s2,...);免費沒有免費的指針作爲va_arg列表發送
儘管使用freeSF函數的第一個指針地址進行打印,但我的代碼並未釋放第一個指針。 主要,免費(s1)的作品,但它應該。免費(s2)在主要崩潰如預期。
如何釋放freeSF函數中的s1指針?
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#define freeS(...) freeSF("", __VA_ARGS__, NULL)
void freeSF(char *paramType, ...) {
va_list pl;
va_start(pl, paramType);
paramType = va_arg(pl, char *);
while (paramType) {
printf("arg %p\n", paramType);
free(paramType);
paramType = va_arg(pl, char *);
}
va_end(pl);
}
int main(int ARGC, char** ARGV) {
char *s1 = strdup("1");
char *s2 = strdup("2");
printf("s1 %p, s2 %p\n", s1, s2);
freeS(s1, s2);
free(s1);
}
你在做:'freeS(s1,s2)'然後做'free(s1)'。你是雙重釋放。 http://ideone.com/shZ3Sc工作得很好。你怎麼知道它不是免費的? – Brandon
當指針加倍時,出現錯誤:'./a.out'中的錯誤:雙倍空閒或損壞(fasttop):0x00000000024c5030。它只發生在免費(s2) – Remy
它不會導致錯誤:http://ideone.com/2sQPXl你仍然調用'free'兩次,這是未定義的行爲..不要調用'free(s1)當你已經在執行freeS(s1,s2)''時,''和'free(s2)'。 – Brandon