[英]Run-Time Check Failure #2 - Stack around the variable 'obj' was corrupted
I am getting Run-Time Check Failure #2 - Stack around the variable 'obj' was corrupted error when I run the following code. 我收到运行时检查失败#2-运行以下代码时,变量'obj'周围的堆栈损坏错误。 I know this is failing because of overwriting the bounds of 'obj' causing corrupting the stack. 我知道这是由于覆盖'obj'的边界而导致堆栈损坏而失败的。 So how to prevent buffer overrun here. 那么如何在这里防止缓冲区溢出。
typedef struct _INFO {
int Count;
} Info, *InfoPtr;
#define MAX_COUNT 10
//void fn(Info(*obj)[MAX_COUNT])
void fn(Info (*obj)[MAX_COUNT])
{
for (int i = 0; i < 2; i++)
{
obj[i]->Count = i;
}
}
int main()
{
Info obj[MAX_COUNT];
fn(&obj);
return 1;
}
With Info (*obj)[MAX_COUNT]
you say that obj
is a pointer to an array of MAX_COUNT
objects of type Info
. 使用Info (*obj)[MAX_COUNT]
您说obj
是指向Info
类型的MAX_COUNT
对象的数组的指针。
But then you use it like obj[i]->Count = i
which treats obj
as an array of pointers to Info
objects. 但是,然后像obj[i]->Count = i
一样使用它,它将obj
视为指向 Info
对象的指针数组。 Ie Info *obj][]
. 即Info *obj][]
。 Not the same thing. 不一样的东西。 And that leads to undefined behavior . 这导致不确定的行为 。
The solution is quite simple, don't pass a pointer to the array as argument, and treat it as an array of objects and not pointers to objects. 解决方案非常简单,不要将指向数组的指针作为参数传递,而是将其视为对象数组而不是对象的指针。
Ie 即
typedef struct Info {
int Count;
} Info;
#define MAX_COUNT 10
void fn(Info *obj, const size_t elems)
{
for (size_t i = 0; i < elems; i++)
{
obj[i].Count = i;
}
}
int main()
{
Info obj[MAX_COUNT];
fn(obj, MAX_COUNT);
}
The changes are most notably the fn
function declaration, which takes a pointer to Info
. 更改最明显的是fn
函数声明,该声明需要一个指向 Info
的指针 。 That is because arrays naturally decays to pointers to their first element. 那是因为数组自然会衰减到指向第一个元素的指针。 I also added an argument to hold the number of elements in the array, so the function knows it. 我还添加了一个参数来保存数组中元素的数量,因此该函数知道这一点。 That makes the function more general and you can pass it different arrays of different sizes. 这使函数更加通用,您可以将其传递给不同大小的不同数组。
I also changed to main
function to not return anything at all. 我还更改为main
函数,以完全不返回任何内容。 Since the C99 standard a main
function without an explicit return
will implicitly get a return 0
by the compiler. 由于C99标准,没有显式return
的main
函数将由编译器隐式return 0
。 And returning 0
from the main
function is usually seen as "okay" or "no failure". 从main
函数返回0
通常被视为“好”或“无故障”。 Returning a non-zero value is considered as a failure or error. 返回非零值被视为失败或错误。
I also changed the name of your structure. 我还更改了您的结构名称。 Names (C or preprocessor) with a leading underscore followed by an upper-case letter is reserved in all scopes by the compiler and the standard C library. 在所有作用域中,编译器和标准C库都保留名称(C或预处理器),下划线前加大写字母的名称。 Also, structure tag names live in a separate namespace, so you can have the same name of the structure as a type-name (type-alias, as defined by typedef
). 同样,结构标记名位于单独的命名空间中,因此您可以将结构的名称与类型名(由typedef
定义的类型别名)相同。 I also removed the InfoPtr
type-name, using such pointers as type-names obfuscates the code and makes it less readable and maintainable. 我还删除了InfoPtr
类型名称,使用了诸如类型名称之类的指针来混淆代码,并使代码的可读性和可维护性降低。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.