strcpy 和 strcat 有时会出现问题

发布于 2024-12-02 03:51:26 字数 435 浏览 5 评论 0原文

你好,我有一个像下面这样的代码

char *str ;

        strcpy(str, "\t<");
        strcat(str, time);
        strcat(str, ">[");
        strcat(str, user);
        strcat(str, "]");
        strcat(str, "(");
        strcat(str, baseName);
        strcat(str, ") $ ");

        printf("\String is now: %s\n", str);

这段代码似乎可以工作,但是当我使用 XCode 分析函数时,它说“函数调用参数是一个未初始化的值”,而且它有时会导致我的程序崩溃..当我删除它时,它就可以工作好吧……有什么问题吗?谢谢

hello I have a code like the one below

char *str ;

        strcpy(str, "\t<");
        strcat(str, time);
        strcat(str, ">[");
        strcat(str, user);
        strcat(str, "]");
        strcat(str, "(");
        strcat(str, baseName);
        strcat(str, ") $ ");

        printf("\String is now: %s\n", str);

This code seems working but when I use XCode analyse function, it says "Function call argument is an uninitialized value" and also it sometimes causes my program crash.. when I remove it, then it works fine... Whats wrong with that? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北凤男飞 2024-12-09 03:51:26

strcpystrcat 用于将字符串复制并连接到分配的 char 数组。

由于 str 未初始化,您正在内存中的某个位置写入,这很糟糕,因为您正在破坏其他数据。它可能在那一刻起作用,但迟早你的程序会崩溃。

声明 str 时应该分配内存:

char str[100];

另外,strcat 效率不高,因为它需要搜索字符串末尾才能知道在哪里连接字符。使用 sprintf 会更高效:

sprintf(str, "\t<%s>[%s](%s) $ ", time, user, baseName);

最后,如果不能保证生成的字符串适合数组,最好使用 snsprintf。

strcpy and strcat are used to copy and concatenate strings to an allocated char array.

Since str in not initilized you're writing somewhere in memory and this is bad because you're corrupting other data. It may work at that moment but sooner or later you'll program will crash.

You should allocate memory when declaring str:

char str[100];

Also, strcat is not efficient as it needs to search for the string end to know where concatenate chars. Using sprintf would be more efficient:

sprintf(str, "\t<%s>[%s](%s) $ ", time, user, baseName);

Finally, if you can't guarantee the generated string will fit the array, you'd better use snsprintf.

述情 2024-12-09 03:51:26

您不分配内存,并且 str 未初始化。所有后续写入都是通过指向“某处”的未初始化指针完成的 - 这是未定义的行为。

您必须分配(然后释放)足够大的内存来容纳生成的字符串:

char *str = malloc( computeResultSizeSomehow() );
if( str == 0 ) {
   // malloc failed - handle as fatal error
}

//proceed with your code, then

free( str );

You don't allocate memory and you leave str uninitialized. All later writes are done through an uninitialized pointer that points "somewhere" - that's undefined behavior.

You have to allocate (and later free) memory large enough to hold the resulting string:

char *str = malloc( computeResultSizeSomehow() );
if( str == 0 ) {
   // malloc failed - handle as fatal error
}

//proceed with your code, then

free( str );
小姐丶请自重 2024-12-09 03:51:26

更加简单并且不会出现缓冲区溢出错误:

#define BUFFERSIZE 512
char str[BUFFERSIZE];

snprintf(str, BUFFERSIZE, "\t<%s>[%s](%s) $ ", time, user, baseName);

This is much simpler and error-free from buffer overflows:

#define BUFFERSIZE 512
char str[BUFFERSIZE];

snprintf(str, BUFFERSIZE, "\t<%s>[%s](%s) $ ", time, user, baseName);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文