【问题标题】:application crashes at first strcat_s应用程序在第一次 strcat_s 崩溃
【发布时间】:2012-09-25 16:21:15
【问题描述】:

我尝试了 strcat 和 strcat_s,但它们都崩溃了。有谁知道为什么会这样?我找不到问题。

Crash: "Unhandled exception at 0x58636D2A (msvcr110d.dll)"
_Dst        0x00ea6b30  "C:\\Users\\Ruben\\Documents\\School\\" char *
_SizeInBytes    260                         unsigned int
_Src        0x0032ef64  "CKV"                   const char *
available       228                         unsigned int
p           0x00ea6b50  ""                  char *

代码:

#include <Windows.h>
#include <strsafe.h>

extern "C"
{
    char* GetFilesInFolders(LPCWSTR filedir, char* path)
    {
        char* files = "";

        char DefChar = ' ';
        char* Streepje = "-";
        bool LastPoint = false;

        WIN32_FIND_DATA ffd;
        TCHAR szDir[MAX_PATH];
        HANDLE hFind = INVALID_HANDLE_VALUE;
        DWORD dwError = 0;

        StringCchCopy(szDir, MAX_PATH, filedir);
        hFind = FindFirstFile(szDir, &ffd);

        if (INVALID_HANDLE_VALUE == hFind) 
            return "";

        do
        {
            DWORD attributes = ffd.dwFileAttributes;
            LPCWSTR nm = ffd.cFileName;
            char name[260];
            WideCharToMultiByte(CP_ACP,0,ffd.cFileName,-1, name,260,&DefChar, NULL);

            for (int i = 0; i <= 260; i++)
            {
                if (name[i] == '.')
                    LastPoint = true;
                else if (name[i] == ' ')
                    break;
            }

            if (LastPoint == true)
            {
                LastPoint = false;
                continue;
            }

            if (attributes & FILE_ATTRIBUTE_HIDDEN)
            {
                continue;
            }
            else if (attributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                char* newfiledir = "";
                char* newpath = path;
                char* add = "\\";
                char* extra = "*";
                strcat_s(newpath, sizeof(name), name);
                strcat_s(newpath, sizeof(add), add);
                puts(newpath);
                strcpy_s(newfiledir, sizeof(newpath) + 1, newpath);
                strcat_s(newfiledir, sizeof(extra) + 1, extra);
                puts(newfiledir);

                size_t origsize = strlen(newfiledir) + 1;
                const size_t newsize = 100;
                size_t convertedChars = 0;
                wchar_t wcstring[newsize];
                mbstowcs_s(&convertedChars, wcstring, origsize, newfiledir, _TRUNCATE);
                LPCWSTR dir = wcstring;
                GetFilesInFolders(dir, newpath);
            }
            else
            {
                char* file = path;
                strcat_s(file, sizeof(name), name);
                puts(file);
                strcat_s(files, sizeof(file), file);
                strcat_s(files, sizeof(Streepje), Streepje);
                puts(files);
            }
        }
        while (FindNextFile(hFind, &ffd) != 0);

        FindClose(hFind);
        return files;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    char* path = "C:\\Users\\Ruben\\Documents\\School\\";
    char* filedir = "C:\\Users\\Ruben\\Documents\\School\\*";
    size_t origsize = strlen(filedir) + 1;
    const size_t newsize = 100;
    size_t convertedChars = 0;
    wchar_t wcstring[newsize];
    mbstowcs_s(&convertedChars, wcstring, origsize, filedir, _TRUNCATE);
    LPCWSTR dir = wcstring;
    char* files = GetFilesInFolders(dir, path);
    return 0;
}

额外信息:我不想使用 boost 或字符串,我想将其保存为 unicode(默认)。

【问题讨论】:

  • 名称为空???你试过 valgrind 了吗?
  • 由于addchar *sizeof(add)sizeof(char *) 相同,这绝对不是您想要的。坦率地说,您似乎根本不了解如何使用 C 风格的字符串。

标签: c++ file search crash strcat-s


【解决方案1】:

您将const char* 分配给files,然后尝试附加到它。

char* files = "";
// ... 
strcat_s(files, sizeof(file), file);

您不能修改常量字符串文字。

我建议您打开编译器警告并确保查看它们。这会警告您将 const char* 分配给 char*。要修复它,您可能已将 files 更改为 const,这将导致您的 strcpy_s 不再编译。

【讨论】:

  • 很好地发现目标位于只读内存中。目标缓冲区的大小不足也是一个严重的问题。
  • 我在 AFAIK 上打开了编译器警告,但我没有收到任何错误消息。 “文件”和“文件”也是 char*。
【解决方案2】:

您似乎不了解变量如何存储在内存中或指针如何工作。在您的_tmain() 中,您有char * path 指向一个常量字符串文字,您将其传递给GetFilesInFolders(),在那里它会被修改。编译器倾向于允许char *s 指向常量字符串,以便向后兼容旧的 C 程序。您不能修改这些。您不能附加到它们。编译器(通常)将它们放在只读段中。这就是您遇到异常的原因之一。

你的整个GetFilesInFolders() 是错误的。正如 DarkFalcon 指出的那样,你没有为files 分配任何空间,你让它指向一个常量字符串文字。

获取“C++ 编程语言”并阅读第 5 章。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多