Re: [PIC] Learning C

smplx <[email protected]>
Newsgroups gmane.comp.hardware.microcontrollers.pic
Message-ID <[email protected]>

On Tue, 19 Nov 2024, Harold Hallikainen wrote:

>
>
> On Mon, November 18, 2024 11:54 pm, Forrest Christian (List Account) wrote:
>
>> In the context of detecting an overflow at runtime, one strategy that is
>> used is to initialize the stack memory to a known non-zero value and then
>> to verify that the top values never change.  If they do, you know that
>> something has overwritten them probably due to a stack overflow which can
>> be handled appropriately.
>>
>
> I've used this on a PIC24H. I have a function that fills the heap to the
> stack with 0xaa55. I ran the firmware with the Debugger (why does spell
> check want to capitalize that?) and then looked at RAM to make sure there
> was a large continuous block of 0xaa55.
>
> On the use of the address of a local variable to find the stack pointer, I
> THINK that in optimization, a compiler may decide to use a register
> instead of the stack, so anything like that needs to be tested.
>
> On learning C, I really like it, but could use more knowledge on pointers,
> void types, and use of CONST. I generally think CONST means it is
> constant, does not change, and could be stored in flash. But I've seen
> stuff where that does not seem to be the case.

If you need help just shout. I'm sure the answers you get from the members 
of this list will be above average and will be of greater help than the 
average results of a google search.

I guess the most confusing thing about "C" pointers is the way "C" treats 
array names / pointers and struct names / pointers.

e.g.
char buff[256];
char *str;

int x, j;

x = buff[j];

x = str[j];

// buff above is the name of an array
// str above is a pointer to an array

char func(char *xstr, int k)
{
 	return xstr[k];
}

x = func(buff, 23);  // convert buff to a pointer and pass it
x = func(str, 23);   // pass str as a pointer

Then you see something like:

struct	FRED
{
 	....
 	int	blah;
 	...
};

int func2(struct FRED arg)
{
 	return arg.blah;
}

int func3(struct FRED *arg)
{
 	return arg->blah;
}

x = func2(something); // pass struct by value

x = func3( & something); // pass struxt by reference

x = func(buff); // pass array by reference

// pass by value makes copy of 'something' on stack and passes address of 
copy to func2

// pass by reference passes address of 'something' directly to func2
// ALSO: pass by reference passes address of 'buff' directly to func

yes array / struct passing is inconsistant and confusing

just remember:

something->blah

is equivalent to:

(*something).blah


Best regards
Sergio Masci
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.