Page 1 of 1

SNMP question

Posted: Wed Dec 13, 2017 1:49 pm
by SeeCwriter
I copied this function from an example in one of the NB manuals, and it works, but I have 2 questions. First, shouldn't pInfo be static since it's on the stack? Second, if I put a printf statement inside the if statement, before the return, why does the program crash almost every time it boots.

Code: Select all

const SysInfo * GetSysInfo()
{
	SysInfo *pInfo = ( SysInfo * ) GetUserParameters();
	if ( pInfo->valid == 0x12345678 )
	{
		return pInfo;
	}
       // initialize structure.
      ...
}

Re: SNMP question

Posted: Wed Dec 13, 2017 6:22 pm
by pbreed
Its part of the SNMP static initalization.
So it happens before the system is setup, hence a printf here will cause the system to try and print to i/o that is not setup.

Its getting the record directly out of flash, and its returning a pointer to that memory block.
Its not static because the pointer is just used to hold the return value, of a pointer, not hold the content of the structure....

If something else changes the record in flash then next time this info is gotten it will get the fresh flash contents...

Re: SNMP question

Posted: Thu Dec 14, 2017 7:57 am
by SeeCwriter
Ah, that explains it. Thank you.

As an aside, in another thread I mentioned that when I turn on stack checking I get a warning in my MIB file telling me to reduce local variables. It turns out that the warning was caused by calling function SetSysInfo.

Code: Select all

void SetSysInfo( SysInfo si )
{
  si.valid = 0x12345678;  // You may want something more meaningful or robust.
  SaveUserParameters( &si, sizeof( SysInfo ) );
}
It's because the SysInfo structure is being passed on the stack. I overloaded the function with one that accepts a pointer to the structure and the warning went away.