Segments in HCS12 controller.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Segments in HCS12 controller.

Jump to solution
2,148 Views
Vicky
Contributor I
HI,
Its a very basic question but it would be great if anybody can enlighten me, how it works..

In a function, we have declared couple of variables which are local to function. One of the local variable is initialized to some label. See below function for more clarity:
void MyFunc()
{
int test1;
int test2 = 2;
char testchar = "HELLO"

/* These variables used here..*/
}

My question is:
1) How HCS12 knows test char is equal to "HELLO", when ever it control comes to this function.
2) As far my understanding is concerned, linker doesnt allocate memory for local variables. Then how controller knows, these variables declared inside the function?

Pl. clarify my question.

Regards,
Vignesh
Labels (1)
Tags (1)
0 Kudos
1 Solution
526 Views
bigmac
Specialist III

Hello Vignesh,

Strictly speaking, this post is in the wrong forum - the CW forum might be more appropriate for this type of query.

There is a problem with your declaration of the third variable on two counts -

  1. A missing semicolon, and
  2. You are attempting to initialise a simple char variable with array data.  To prevent the compiler from complaining or a type mismatch, the following would be needed -
    char testchar[] = "HELLO";
    This will allocate six bytes on the stack (including a final null), and initialise to the given string data.  If the required string length is more than five characters, the following could be used -
    char testchar[20] = "HELLO";

When a function is entered, stack space is allocated for all the local variables that are defined (but not static variables).

Regards,
Mac

View solution in original post

0 Kudos
2 Replies
527 Views
bigmac
Specialist III

Hello Vignesh,

Strictly speaking, this post is in the wrong forum - the CW forum might be more appropriate for this type of query.

There is a problem with your declaration of the third variable on two counts -

  1. A missing semicolon, and
  2. You are attempting to initialise a simple char variable with array data.  To prevent the compiler from complaining or a type mismatch, the following would be needed -
    char testchar[] = "HELLO";
    This will allocate six bytes on the stack (including a final null), and initialise to the given string data.  If the required string length is more than five characters, the following could be used -
    char testchar[20] = "HELLO";

When a function is entered, stack space is allocated for all the local variables that are defined (but not static variables).

Regards,
Mac

0 Kudos
526 Views
CompilerGuru
NXP Employee
NXP Employee
If you don't need to modify the string content, you can alternatively allocate a pointer on the stack, and the content of the string literal in the flash:

const char* testchar = "HELLO";

Here the linker does allocate the 6 bytes for "HELLO", and the compiler just uses 2 bytes for the pointer testchar on the stack (if it is necessary at all). So this is more efficient than the

char testchar[] = "HELLO";

syntax, but the string content is not modifiable or owned by the function, so it depends on what you need. I did add an explicit const to make sure the string does not get changed accidentally.

For super long strings, by the way, don't initialize the string on the stack.
Write the value into it:
char testchar[100];
(void)strcpy(testchar, "HELLO");
This runs at lot faster as ANSI requires that otherwise all 100 bytes are initialized. It's a init all or nothing thing.

Daniel