简体   繁体   English

C代码将结构传递给函数堆栈溢出

[英]C code passing struct into function Stack overflow

Okay so I'm having an issue with a current assignment (trust me this is a minuscule part of it) as we are required to write in C code and not C++, and we are not allowed to change certain parts of code. 好的,我目前的工作分配存在问题(相信我,这是其中的一个小部分),因为我们需要用C代码而不是C ++编写,并且我们不允许更改某些代码部分。 So I have a struct defined: 所以我定义了一个结构:

    typedef struct someStruct {
    int what;
    int something[MAX];
    int another[MAX];
} someType;

in main() I initialize all the values in a defined struct: 在main()中,我在定义的结构中初始化所有值:

someType whatever, *whatptr;

EDIT:of course set the pointer to the struct, trying to simplify the code for the example It is present in my code already 编辑:当然设置指向结构的指针,试图简化示例代码它已经存在于我的代码中

whatptr = &whatever;
whatever.what = 0;
// initialize both arrays to hold 0 at all indexes
// Then I must call a function progRun()
progRun();  //I need to pass struct 'whatever' in some way

Now progRun() looks like this: 现在,progRun()如下所示:

void progRun(){
printWhat(&whatever);

    if (whatever.what == 0) {
    //do stuff 
    }
}

I can't change anything inside this code except what parameters to pass inside the progRun() function and I can add stuff before printWhat(). 除了在progRun()函数中传递哪些参数外,我无法更改此代码内的任何内容,并且可以在printWhat()之前添加内容。 I've tried changing progRun to 我尝试将progRun更改为

void progRun(someType *stptr)

then calling it in main as 然后在主叫它

progRun(whatptr);

but this causes a stack overflow issue for some reason, I've tried stepping through with a debugger and it only occurs when the function is called. 但这由于某种原因导致了堆栈溢出问题,我尝试使用调试器逐步解决,并且仅在调用函数时发生。 Is there a better way to pass the 'whatever' struct to the function so it can be passed into progRun() and printWhat() and can access 'whatever.what'? 有没有更好的方法可以将“ whatever”结构传递给函数,以便可以将其传递给progRun()和printWhat()并可以访问“ whatever.what”?

Any help would be greatly appreciated! 任何帮助将不胜感激! in the meantime I'll try to figure it myself if I can. 同时,如果可以的话,我会尝试自己解决。

EDIT: Something else must be wrong in the code even though everything else has compiled and ran perfectly fine until this code was added. 编辑:即使在添加了此代码之前,其他所有内容都已编译并运行得很好,但代码中的其他内容肯定是错误的。 If I can break down the code and find out what's wrong I'll update the question. 如果我可以分解代码并找出问题所在,则将更新该问题。 And no I cannot post the whole code as it is an assignment (this isn't the goal of the assignment trust me it focuses on data forwarding and more, just need to get this basic thing working) Thank you for help everyone. 而且不行,我无法发布整个代码,因为这是一个任务(这不是任务的目标,相信我它专注于数据转发等等,只需要使此基本功能正常工作即可)谢谢大家的帮助。

EDIT: the MAX number used in the struct for something[MAX] and another[MAX] was extremely large ( I left my desktop that I started this project with back home, I'm currently using an old laptop that can't handle large arrays). 编辑:结构中用于某[MAX]和另一个[MAX]的MAX数非常大(我离开桌面时回到家中开始了该项目,我目前使用的是旧笔记本电脑,无法处理较大的数据数组)。 All the answers below, and some of the stuff I used before now works fine. 下面的所有答案以及我之前使用过的一些东西现在都可以正常工作。

void progRun(someStruct *ptr) {
    someStruct whatever2 = *ptr;
    printWhat(whatever2);
    if (whatever2.what == 0) { ...
}

whatptr = &whatever;
progRun(whatptr);

Your problem was that: 您的问题是:

  • you need to pass a pointer to whatever , yet you were passing a variable ( whatptr ) that had absolutely nothing to do with whatever . 你需要一个指针传递给whatever ,但你传递一个变量( whatptr是有绝对无关) whatever

    You need to first assign the pointer to whatever into your pointer variable. 您需要先将指针分配给指针变量中的任何内容。

  • You are not dereferencing the pointer in the function 您没有在函数中取消引用指针

Alternately, get rid of pointer variables: 或者,摆脱指针变量:

void progRun(someType *stptr) {
    printWhat(*stptr);
    if (stptr->what == 0) { ...
}

progRun(&whatever);

Instruction 指令

someType whatever, *whatptr;

is the problem: 是问题所在:

*whatptr will not point to the struct whatever unless you do the assignment as follows: *whatptr不会指向struct whatever ,除非你做的任务如下:

whatptr = &whatever;

Alternatively you could dynamically allocate memory on the heap for a pointer to your struct whatever by using the malloc() function and pass the pointer returned by malloc to the function progrun : 另外,您可以动态地分配在堆上的一个指针,你的记忆struct whatever使用malloc()函数并传递malloc返回给函数指针progrun

whatptr = (someType*) malloc ( sizeof(someType) );
if (whatptr == NULL) exit (1);

//whatever you need to do with your code

progrun(whatptr); // call function like this

In this case of course you will need to dereference your pointer to access member elements of the struct by using the arrow -> operator: 当然,在这种情况下,您将需要使用箭头->运算符来取消引用指针以访问struct的成员元素:

whatpr->what = 0; // for example

Also, check these tutorials to understand both approaches: 另外,请查看这些教程以了解这两种方法:


If you can't change print and if statements then you should pass your function a copy of your struct : 如果您不能更改printif语句,则应向函数传递struct的副本:

void progRun( someType whatever ){ // <---Passing by copy
printWhat(&whatever);

    if (whatever.what == 0) {
    //do stuff 
    }
}

and in your main() you should just call the function like this: main()您应该像这样调用函数:

someType whatever;
//assign values to members of the struct
progRun(whatever);

and do not need at all to define and assign a pointer to the struct . 并且根本不需要定义和分配指向struct的指针。

Though passing variables to functions by copy (especially when they are objects composed by many variables such as a struct is) is not a good behaviour: 尽管通过复制将变量传递给函数(尤其是当它们是由许多变量(例如struct )组成的对象)不是一个好习惯:

  • it will require an overhead to copy all member elements 复制所有成员元素将需要开销
  • your copy will have a limited scope, which means that any change you do to the variable inside of the function will be lost when your function ends and will not be reflected on variable at main scope 您的副本将具有有限的作用域,这意味着您对函数内部变量所做的任何更改都将在函数结束时丢失,并且不会反映在主作用域的变量上

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM