c - Windows 上的 malloc

标签 c windows malloc

我是这个论坛的新人,我希望做对。 我的问题如下: 我正在编写一个具有此功能的程序:

void* s_malloc(int _size, int dim){
    printf("%d %d\n", _size, dim);
    char* pointer;
    pointer = (char*)malloc(_size * dim);
    printf("Malloc eseguita \n");
    if(pointer == NULL){
        ExitFailure(strerror(errno));
    }else{
        for(int i = 0; i < dim * _size; i++){
            pointer[i] = '\0';
        }
        printf("Allocata\n");
        return (void*)pointer;
    }
    return (void*) NULL;
}

我多次测试了这个函数(事实上,我在一个月前写了这个函数,从那时起我就经常使用它)。我很确定该功能可以正常工作。 然而在这个其他函数中我使用了's_malloc'并且它崩溃了

#define LDTOCAT "C:\\Users\\admin\\logdrstrace\\"

void startProcess(){
    char* logfile, *logfilefullpath;
    int run = TRUE, lenth = 0;

    /*
    for(int i = 0; i < 16; i ++){
        s_malloc(60, 1);
    }*/ commented

    while(run){
        logfile = checkLogFileDirectory();
        Sleep(1000);
        #ifdef DEBUG_MODE
        printf("\nValue returned: %s\n", logfile);
        fflush(stdout);
        #endif // DEBUG_MODE
        if(strcmp(logfile, NOFILEFOUND) != 0){
            run = FALSE;
        }
    }
    lenth = (strlen(LDTOCAT)+strlen(logfile)+1);
     #ifdef DEBUG_MODE
        printf("Let's go forward allocating memory: size %d\n", lenth);
        printf("LDTOCAT size: %d, logfile size: %d + 1\n", strlen(LDTOCAT), strlen(logfile));
        fflush(stdout);
        #endif // DEBUG_MODE

    logfilefullpath =(char*) s_malloc(lenth, sizeof(char));
    #ifdef DEBUG_MODE
    printf("Memory created!\n");
    fflush(stdout);
    #endif // DEBUG_MODE
    memcpy(logfilefullpath, LDTOCAT, sizeof(LDTOCAT));
    #ifdef DEBUG_MODE
    printf("Created logfilefullpath with directory: %s\n", logfilefullpath);
    fflush(stdout);
    printf("File found: %s\n", strcat(logfilefullpath, logfile));
    #endif // DEBUG_MODE

    int fd = open(strcat(LDTOCAT, logfile), O_RDONLY);
    if(fd <= OK){
        ExitFailure(strerror(errno));
    }
}

如您所见,在开始注释处有一个 for 循环。现在我们有三种可能: 1) 如果我编译并运行程序,当它执行命令 'loginfulpath = (char*)s_malloc(lenth, sizeof(char))' 时,它在 s_malloc 函数中失败,它进入 s_malloc 并在调用原始 malloc 时崩溃。有时 errno 变量设置为“内存不足”,但有时会崩溃并停止。 2)如果我在开始时取消循环并且停止条件是'i <15'它会像第一点一样崩溃 3) 如果我设置停止条件 'i<16',神奇地,它工作正常。

还有第三个函数,打开目录并查找文件, 事实上,正如您在代码中看到的那样,它避免了找到的前 2 个文件,因为 他们是 。和..

#define NOFILEFOUND "Nothing"

 char* checkLogFileDirectory(){
 HANDLE hfile = INVALID_HANDLE_VALUE;
 WIN32_FIND_DATA fdata, nullValue;
 char* file = NULL;
 int counter=0;
 while(hfile == INVALID_HANDLE_VALUE){
 hfile = FindFirstFileA(LOGDIR, &nullValue);
 }
 #ifdef DEBUG_MODE
 printf("-FileFound: %s\n", nullValue.cFileName);
#endif // DEBUG_MODE
 while(FindNextFile(hfile, &fdata) && counter != 2){
        #ifdef DEBUG_MODE
        printf("-File found: %s\n", fdata.cFileName);
        #endif // DEBUG_MODE
    if(counter == 1){
         #ifdef DEBUG_MODE
        printf("Third file it's my file\n");
        #endif // DEBUG_MODE
        file = s_malloc(strlen(fdata.cFileName), sizeof(char));
        memcpy(file, fdata.cFileName, strlen(fdata.cFileName)+1);
        if(file == NULL){
            ExitFailure("Error reading file");
        }
        file[strlen(fdata.cFileName)] = '\0';

    }
    counter ++;
 }
 #ifdef DEBUG_MODE
 printf("\n\=>File selected: %s", file==NULL | file == "" ? NOFILEFOUND:file);
 #endif // DEBUG_MODE
 return counter == 2? file : NOFILEFOUND;
}

注意:我打印了所有变量的值,它们都是正确的, 我在带有 vmware 工作站的虚拟机上。 对不起我的英语我希望你能理解

这是在情况 1 或 2 中崩溃时的输出

-FileFound: .
-File found: ..
-File found: drstrace.calc.exe.00532.0000.log
Third file it's my file
32 1
Malloc eseguita
Allocata

=>File selected: drstrace.calc.exe.00532.0000.log
Value returned: drstrace.calc.exe.00532.0000.log
Let's go forward allocating memory: size 60
LDTOCAT size: 27, logfile size: 32 + 1
60 1
Malloc eseguita
Error: Not enough space!!

谢谢!!

最佳答案

你的分配函数没问题(它是带有错误检查的穷人版本的 calloc,也许使用 calloc 会节省一些手动代码并且会更有效率)

但是在checkLogFileDirectory中这段代码是错误的:

file = s_malloc(strlen(fdata.cFileName), sizeof(char));
memcpy(file, fdata.cFileName, strlen(fdata.cFileName)+1);

file 空间不足(nul-terminating char)。您丢失了 1 个字节,这可能会通过未定义的行为破坏应用程序的其余部分(分配 15 或 16 个无用的 block 有时会使其“工作”,因为它会更改内存布局)。

为什么不这样做:

file = strdup(fdata.cFileName);

此外,正如评论中所指出的,如果您的程序到达了更远的地方,您将在这里遇到一个大问题:

int fd = open(strcat(LDTOCAT, logfile), O_RDONLY);

LDTOCAT 是一个字符串文字,您不能在其上应用strcat。即使可以,您也没有足够的空间。

关于c - Windows 上的 malloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46940817/

相关文章:

c - 记录开头出现奇怪的字符 - C

c++ - 如何通过具有命令行参数的 procdump 启动 Windows 应用程序?

Windows 主机名解析

windows - 使用 Bonobo 在 IIS 上托管的 GIT 问题

c - 释放分配的二叉树 - C 编程

c - Malloc 没有分配我告诉它的那么多内存(我相信)

c - C 中除了转义字符之外的第一次出现

计算文件中的行字母但排除特殊字符

C语言 :Scanning input from Eclipse console

c - 创建链表头的问题