相关疑难解决方法(0)

我是否施放了malloc的结果?

在这个问题,有人建议意见,我应该不会投的结果malloc,即

int *sieve = malloc(sizeof(int) * length);
Run Code Online (Sandbox Code Playgroud)

而不是:

int *sieve = (int *) malloc(sizeof(int) * length);
Run Code Online (Sandbox Code Playgroud)

为什么会这样呢?

c malloc casting

2318
推荐指数
27
解决办法
22万
查看次数

为什么strlcpy和strlcat被认为是不安全的?

据我所知,strlcpy和strlcat被设计为安全的替代品strncpy和strncat.然而,有些人仍然认为他们不安全,只是造成不同类型的问题.

有人可以举例说明如何使用strlcpy或strlcat(即一个总是 null终止其字符串的函数)会导致安全问题吗?

Ulrich Drepper和James Antill表示这是事实,但从未提供实例或澄清这一点.

c security strncpy strlcpy

77
推荐指数
6
解决办法
6万
查看次数

如何在C中汇总所有命令行参数?

我有一个任务.该程序将打印C中所有命令行参数的总和.我尝试编译它的代码,但在控制台中传递参数后抛出错误.下面是代码.

/* Printing sum of all command line arguments */
#include <stdio.h>

int main(int argc, char *argv[]) {
    int sum = 0, counter;

    for (counter = 1; counter <= argc; counter++) {
       sum = atoi(sum) + atoi(argv[counter]);
    }
    printf("Sum of %d command line arguments is: %d\n", argc, sum);
}
Run Code Online (Sandbox Code Playgroud)

编译后显示Segmentation fault(core dumped)错误.您的经验可以解决我的问题.

以下是我编辑的代码:

/* Printing sum of all command line arguments*/
#include <stdio.h>
#include <stdlib.h> // Added this library file

int main (int argc, char *argv[]) {
    int …
Run Code Online (Sandbox Code Playgroud)

c syntax-error atoi command-line-arguments

7
推荐指数
3
解决办法
1790
查看次数

C中的类型转换和按位操作的结果取决于顺序

我试图在int, char, short, long不使用头文件的情况下打印最小值<limit.h>.所以按位操作将是一个不错的选择.但有些奇怪的事发生了

该声明

printf("The minimum of short: %d\n", ~(((unsigned short)~0) >> 1));
Run Code Online (Sandbox Code Playgroud)

给我

The minimum of short: -32768
Run Code Online (Sandbox Code Playgroud)

但声明

printf("The minimum of short: %d\n", ~((~(unsigned short)0) >> 1));
Run Code Online (Sandbox Code Playgroud)

给我

The minimum of short: 0
Run Code Online (Sandbox Code Playgroud)

这种现象也发生在char.但它不会发生在long, int.为什么会这样?

值得一提的是我使用VS Code作为我的编辑器.当我unsigned char在语句中移动光标时

printf("The minimum of char: %d\n", (short)~((~(unsigned char)0) >> 1));
Run Code Online (Sandbox Code Playgroud)

它给了我一个提示,(int) 0而不是(unsigned char)0我所期望的.为什么会这样?

c bit-shift bitwise-operators implicit-conversion visual-studio-code

6
推荐指数
1
解决办法
402
查看次数

C4996(函数unsafe)警告strcpy但不是memcpy

我在VS2010中编写代码,我碰巧在编译后看到编译器为strcpy和sprintf调用提供了C4996警告("此函数或变量可能不安全").

但是,我无法获得memcpy的类似警告(可能在代码中有更多类似的'不安全'函数调用)

int _tmain(int argc, _TCHAR* argv[])
{
    char buf1[100], buf2[100];
    strcpy (buf1, buf2); // Warning C4996 displayed here asking to use strcpy_s instead
    memcpy (buf1, buf2, 100); // No warning here asking to use memcpy_s
    memcpy_s(buf1, 100, buf2, 100);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么会这样?如何在代码中为所有可能的不安全呼叫打开C4996警告?

c visual-studio-2010

4
推荐指数
2
解决办法
4767
查看次数

?错误“在此函数中未初始化使用了s1,s2”

这是我在编程基础课程的第一堂课后遇到的问题之一

gets()可以正常工作(我认为),但是“ cin”拒绝工作

int main(void)
{
    char *s1, *s2;
    puts("Enter your name and surname for gets()");
    gets(s1);
    puts("Enter your name and surname for cin()");
    cin >> s2;
    cout << s1 << "! Hello from gets" << endl;
    cout << s2 << "! Hello from cin" << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望cin输出您在控制台中输入的内容,但是在输入programm之后等待一秒钟,然后所有内容将关闭,而没有任何输出。

屏幕截图是我们老师给我们的,它没有用

屏幕截图

c++ string c-strings fgets getline

1
推荐指数
1
解决办法
84
查看次数