[MSX-C] Q&A official thread

Page 20/57
13 | 14 | 15 | 16 | 17 | 18 | 19 | | 21 | 22 | 23 | 24 | 25

By Grauw

Ascended (10581)

Grauw's picture

14-09-2015, 22:50

And the heap memory can fragment… And allocation costs more CPU cycles. Worth noting.

By Sylvester

Hero (555)

Sylvester's picture

14-09-2015, 22:51

Ok, but when I try:

len = strlen(str);
char word[len];

it gives an error. So how to initialize it with the correct size (so not a hardcoded value)?

By Grauw

Ascended (10581)

Grauw's picture

14-09-2015, 23:02

Ah, good point Smile. I think stack allocations must be constant size, because the function’s stack frame size must be known at compile time…

By the way, what is the stack and heap size in MSX-C?

By Manuel

Ascended (18788)

Manuel's picture

14-09-2015, 23:23

Indeed, variable length arrays are only present in modern C dialects/extensions.

By AxelStone

Prophet (3108)

AxelStone's picture

14-09-2015, 23:29

Manuel wrote:

Indeed, variable length arrays are only present in modern C dialects/extensions.

I can confirm, in MSX-C you must create static size arrays or use pointers. However, as far as I know the correct way to access a *char is not as array (array[x]) but as addresses (*array+x). So if you are using array:

for(i=0;i<=len;i++) {
   array[i]
}

For array.

for(i=0;i<=len;i++) {
   *array+i;
}

For pointer.

By Grauw

Ascended (10581)

Grauw's picture

14-09-2015, 23:41

Afaik [] is just syntactic sugar for *array+i, so it’s fine (better even, imo) to use array[i].

By DarkSchneider

Paladin (944)

DarkSchneider's picture

15-09-2015, 08:42

Also remember that the stack is small and as said the size of array must be static.

By Grauw

Ascended (10581)

Grauw's picture

15-09-2015, 09:55

How small is the stack? 256 bytes? 1K? 4K?

And does anyone know where is the stack located? After the program code and static data, or at the end of the TPA (as is typical in assembly)? And the heap? Between the program code and the stack or after the stack? Does it just consume all remaining space or does it have a fixed size?

By anonymous

incognito ergo sum (116)

anonymous's picture

15-09-2015, 10:31

Grauw wrote:

How small is the stack? 256 bytes? 1K? 4K?

1000 bytes by default. You can use the rsvstk() function to increase or decrease the stack size:

    rsvstk(2500);     /* Sets the stack size to 2500 bytes */
    /* Do something that requires a big stack */
    rsvstk(1000);     /* Put it back as it was */
Grauw wrote:

And does anyone know where is the stack located? After the program code and static data, or at the end of the TPA (as is typical in assembly)? And the heap? Between the program code and the stack or after the stack? Does it just consume all remaining space or does it have a fixed size?

The stack is located at the end of the TPA. The heap is located immediately after the program code and consumes all the space between the end of the program and the stack. The sbrk() function reserves space in the heap.

Both rsvstk() and sbrk() are low-level memory management functions. When you need to allocate and free memory use the high-level memory management functions alloc() to request it, and free() to release it after usage.

This is explained in the MSX-C User's Manual:

- Page 88: quick description of the memory management functions, with a nice diagram of the memory map, with the location of the program code, heap, stack, and MSX-DOS work area.
- Page 124: Detailed description of alloc()
- Page 166: Detailed description of free()
- Pages 218 and 219: Detailed descriptions of rsvstk() and sbrk()

By Grauw

Ascended (10581)

Grauw's picture

15-09-2015, 13:06

Thanks, my understanding of the environment has improved Smile.

Page 20/57
13 | 14 | 15 | 16 | 17 | 18 | 19 | | 21 | 22 | 23 | 24 | 25