C++ program works as is, but does not when creating functions out of main() contents

Thread Starter

Adam Uraynar

Joined Dec 21, 2015
67

Attachments

Thread Starter

Adam Uraynar

Joined Dec 21, 2015
67
I think dev_spi is allocated on the stack in Startup and that space would be deallocated when that routine completes. Try moving it to a global variable.
DevSPI dev_spi(D11, D12, D13);

What about the parameters? Should I just put something like this:
DevSPI;

It's just hard to know what you mean when making a variable out of something with parenthesis.
 

BobaMosfet

Joined Jul 1, 2009
2,113
DevSPI dev_spi(D11, D12, D13);

What about the parameters? Should I just put something like this:
DevSPI;

It's just hard to know what you mean when making a variable out of something with parenthesis.
DevSPI is defined in the DevSPI.h header file to an external library. Do you not know how to read C/C++ code?

DevSPI is a type. Like int or unsigned char. It is likely a structure or a class. it is a return type of the function dev_spi().
dev_spi() is a function name.
argument variables are between the parends (ie. '(' and ')').

The reason you are confused is because you don't understand pointers, and function pointers.

Good luck ;)
 

402DF855

Joined Feb 9, 2013
271
C:
DevSPI dev_spi(D11, D12, D13);
Declaring an object (variable) with initialization parameters is a key feature of C++. So this line of code creates a variable called dev_spi of type DevSPI and invokes the constructor that performs the necessary initialization based on the parameters D11, D12, and D13. Presumably the three parameters are MISO, MOSI, and CLK pin definitions, although you should refer to the constructor definition (in DevSPI.h) and verify what exactly you need to specify in the declaration parameters.

In my previous post I tried to impress upon you that you need to be aware of when and where the memory is allocated and the constructor invoked thereafter. In main() the variable dev_spi is allocated space on the stack and then the constructor is called. The subsequent allocation (via new) and call to the XNucleoIHM02A1 constructor takes the address of dev_spi as one of its parameters; obviously that constructor will copy the DevSPI pointer to one of its member variables for use later.

I would expect there to be an infinite loop somewhere in main(). After the functions are called (forward(),...,altogether()) main() will exit and as part of that the memory allocated for dev_spi will be reclaimed as that variable exits scope. Any attempt to use that variable after that will almost certainly not work and will probably result in bizarre, unexpected, random behavior.

For instance, look in startup() where you also define dev_spi, and call the XNucleoIHM02A1 constructor. If startup() completes (it won't because you aren't calling it), the variable dev_spi will go away (but its destructor will be called automatically, if one is defined). Any subsequent use of that memory by x_nucleo_ihm02a1 will probably be disastrous, as other routines running after that will use stack for its purposes and overwrite that memory causing the DevSPI routines to fail.
 
Last edited:

BobaMosfet

Joined Jul 1, 2009
2,113
Nope. The reason I was confused is because defining such a thing as a variable doesn't make sense in the first place!
That is because you don't understand that C++ uses function pointers to declare a function in a variable scope. I've been doing this since before you were born. I helped the compiler manufacturers make their C/C++ compilers, and I've written O/S's and compilers myself.

Don't confuse your inability to correctly understand code, because you can't get your mind around syntax. Learn something about stack frames and how the compiler actually turns your syntactic grammar into object code.

402DF855 explained it to you in C++ terms. I'm giving it to you in terms of what's underneath what he said- how the compiler is actually viewing it.
 
Last edited:
Top