简体   繁体   English

运行时检查失败#2-变量'name'周围的堆栈已损坏

[英]Run-Time Check Failure #2 - Stack around the variable 'name' was corrupted

I've got an interface function in my application: 我的应用程序中有一个接口函数:

void addShopToList(Tshp **shpHead){
    char* name;
    Tshp *newshp = NULL;
    system("cls");
    printf("Name: ");
    scanf("%s[^\n]", &name);
    fflush(stdin);
    newshp = addShp(shpHead,name,NULL);
    if(prompt("Do you want to add some products?")){
        addProductMenu(&newshp);
    }
}

and I get: 我得到:

Run-Time Check Failure #2 - Stack around the variable 'name' was corrupted.

When I trigger those functions separately (I mean like addShp() -> it's just adding a new shop to the list), it works properly. 当我分别触发这些功能时(我的意思是像addShp()->只是向列表中添加新商店),它可以正常工作。 I have no idea how to fix it :/. 我不知道如何解决它:/。

name is uninitialized, so then when you try to take the address of the uninitialized pointer: name未初始化,因此当您尝试获取未初始化指针的地址时:

scanf("%s[^\n]", &name);

it pukes on you. 它p你。 So two points: 所以有两点:

1) char *name = malloc(100); // now it's initialized to something, don't forget to free it later 1) char *name = malloc(100); // now it's initialized to something, don't forget to free it later char *name = malloc(100); // now it's initialized to something, don't forget to free it later

2) scanf("%s[^\\n]", name); // shouldn't use the & for a string in scanf 2) scanf("%s[^\\n]", name); // shouldn't use the & for a string in scanf scanf("%s[^\\n]", name); // shouldn't use the & for a string in scanf

and a third bonus point: 第三点奖励:

3) fflush(stdin); 3) fflush(stdin); don't do that. 不要那样做 Flushing stdin is undefined behavior according to the C11 standard §7.21.5.2 part 2: 根据C11标准§7.21.5.2第2部分,冲洗stdin是未定义的行为:

Ifstream points to an output stream ... the fflush function causes any unwritten data for that stream ... to be written to the file; 如果流指向输出流,则fflush函数使该流的所有未写入数据都写入文件; otherwise, the behavior is undefined. 否则,行为是不确定的。

On some systems, Linux being one as you can see in the man page for fflush() , there's a defined behavior but it's system dependent so your code may not be portable. 在某些系统上,Linux是您在fflush()手册页中看到的一个,它具有定义的行为,但是它取决于系统,因此您的代码可能不可移植。

You have two problems: 您有两个问题:

The first is that you are passing the address of the pointer to the string to scanf - not the address of the string. 首先是您将指针的地址传递给scanf的字符串-而不是字符串的地址。 The scanf call doesn't know that, so it starts reading data in to the address you passed it, overwriting the memory location where name is stored, and the memory after it ( newshp ) and so on... by the time the scanf is done, who knows what's left? scanf调用不知道这一点,所以它开始在给你传递它的地址读取数据,覆盖了存储位置name进行存储,并且之后 (内存newshp )等等......由当时scanf完成了,谁知道还剩下什么? You can fix that easily by removing the & before name in the scanf call. 您可以通过删除scanf调用中的& name来轻松解决此问题。

But then you stumble onto the other, important, issue: currently name is a pointer that points to who-knows-where - it's uninitialized. 但是随后您偶然发现另一个重要的问题:当前name是一个指向谁知道哪里的指针-它尚未初始化。 Wherever it points, it's not your memory. 无论指向何处,都不是您的记忆。 The fixed call to scanf will overwrite that memory - which you don't own. scanf的固定调用将覆盖该内存-您不拥有该内存。 And then what happens? 然后会发生什么?

The solution to that is to initialize name to point to memory that you allocate. 解决方案是初始化name以指向您分配的内存。 You can do that using something like malloc to allocate a chunk of memory, or, alternatively, you can allocate the space on the stack thusly: 您可以使用malloc来分配一块内存,或者,您也可以这样在堆栈上分配空间:

// allocate space for 99 characters (plus 1 space for the null-terminator
// and initialize the memory to all null characters.
char name[100] = { 0 }; 

Sidenote : you should really try to understand the subtle difference between what happens when you pass &name instead of name to scanf . 旁注 :您应该真正尝试理解将&name而不是name传递给scanf时发生的细微差别。 It will be a very important pedagogical exercise and help you gain a much better understanding of pointers and how your code is translated into something the machine understands. 这将是非常重要的教学活动,可以帮助您更好地了解指针以及如何将代码转换为机器可以理解的内容。

Finally, you shouldn't call fflush(stdin) as that results in undefined behavior. 最后,您不应调用fflush(stdin)因为这会导致未定义的行为。 And that's a bad thing. 那是一件坏事。 What do you believe that function call would achieve anyways? 您认为函数调用将实现什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时检查失败#2-变量'check'周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'check' was corrupted 运行时检查失败#2-变量'input'周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'input' was corrupted 运行时检查失败#2-变量'indices'周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'indices' was corrupted 运行时检查失败#2-变量'tempID'周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'tempID' was corrupted 运行时检查失败-变量周围的堆栈已损坏 - Run-Time Check Failure - Stack around variable was corrupted 运行时检查失败#2-变量周围的堆栈-已损坏 - Run-Time Check Failure #2 - Stack around the variable — was corrupted 运行时检查失败 #2 - 变量“newRow”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'newRow' was corrupted 运行时检查失败 #2 - 变量“数组”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'array' was corrupted 运行时检查失败#2-变量'd'周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'd' was corrupted 运行时检查失败#2-变量“ z”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable ''z" was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM