[MSX-C] Q&A official thread

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

By Sylvester

Hero (552)

Sylvester's picture

15-09-2015, 20:25

A question about pool, system table and hash. I wrote a c program (around 500 lines of code), but when I compile (CF) it i get the following error:

MSX-C ver 1.20p   (parser)
Copyright (C) 1989 by ASCII Corporation
line 433 column 8: heap over flow
pool                  7310/9100
system table     2817/4194
hash                 2060/2804

What things do I need to keep in mind during coding so these values wont grow to fast? I know I can tweak the compiler options so that the system table will become smaller and i get more space for the pool. But would like to know what causes these large values I get.

By AxelStone

Prophet (3094)

AxelStone's picture

15-09-2015, 20:38

Perhaps it's a good moment to explain / remember what is stored in every space (heap, pool, system table, hash, stack), what is it's default space and how they can be modified through compiler options Wink

By Manuel

Ascended (18747)

Manuel's picture

15-09-2015, 20:55

AxelStone wrote:
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.

See Grauw's reaction, but to be honest, this is actually just wrong. It only works for 8-bit pointers. You probably want something like this then:

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

Anyway, bollocks, just use the index operator, it does all the work for you.

By PingPong

Prophet (3885)

PingPong's picture

15-09-2015, 21:40

Sylvester wrote:

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)?

Emh no one pointed that probably char word[len] is incorrect? should be char word[1+len]. remember C string are null terminated array of chars.

By PingPong

Prophet (3885)

PingPong's picture

15-09-2015, 21:43

Manuel wrote:
AxelStone wrote:
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.

See Grauw's reaction, but to be honest, this is actually just wrong. It only works for 8-bit pointers. You probably want something like this then:

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

Anyway, bollocks, just use the index operator, it does all the work for you.

Umh pay attention at the two forms, modern compilers are able to generate high efficent code when using
*(p+x) or p[x], but some old compilers generate faster & compact code (and manually more controllable code )with the first syntax, the pointer syntax. I suspect MSX is enough old to not have this kind of optimizations.

By AxelStone

Prophet (3094)

AxelStone's picture

15-09-2015, 21:53

Manuel wrote:

See Grauw's reaction, but to be honest, this is actually just wrong. It only works for 8-bit pointers. You probably want something like this then:

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

Anyway, bollocks, just use the index operator, it does all the work for you.

You are right, I'm still a little confusing with pointers and where * should be placed. Really to get next position should be like:

   *(array + i)

However a static initialization it's better if you can do it.

By Grauw

Ascended (10565)

Grauw's picture

15-09-2015, 22:42

Problem with the dereferencing operator aside, if the type of array is a 16-bit type, it needs to add 2 * i, or any other multiple if it’s a struct. Using the index operator, it knows the size automatically, so it’s better (more typesafe).

By Sylvester

Hero (552)

Sylvester's picture

15-09-2015, 22:50

Thanks for all the array/pointer info, with it I managed to implement these functions:

char *strdup(s);
char *strstr(haystack, needle);
char *str_replace(original, pattern, replacement);
VOID wrap(out, str, columns);

Smile

so back to the next questions Smile

Quote:

What things do I need to keep in mind during coding so these values wont grow to fast? I know I can tweak the compiler options so that the system table will become smaller and i get more space for the pool. But would like to know what causes these large values I get.

and

Quote:

Perhaps it's a good moment to explain / remember what is stored in every space (heap, pool, system table, hash, stack), what is it's default space and how they can be modified through compiler options

@Axelstone: about the compiler options, see: http://www.lavandeira.net/2015/02/relearning-msx-13-msx-c-co...

By anonymous

incognito ergo sum (116)

anonymous's picture

16-09-2015, 01:51

Sylvester wrote:

A question about pool, system table and hash. I wrote a c program (around 500 lines of code), but when I compile (CF) it i get the following error:

MSX-C ver 1.20p   (parser)
Copyright (C) 1989 by ASCII Corporation
line 433 column 8: heap over flow
pool                  7310/9100
system table     2817/4194
hash                 2060/2804

What things do I need to keep in mind during coding so these values wont grow to fast? I know I can tweak the compiler options so that the system table will become smaller and i get more space for the pool. But would like to know what causes these large values I get.

Those are actually two different questions. None of the pool, system or hash tables ran out of space, so you're still fine to that regard. In any case, I already wrote how to tweak these tables in Relearning MSX #13.

The error you're getting is 'heap overflow'. According to the manual (page 269) possible causes are:

- Too many includes
- You declared too many automatic variables
- The program is too complex

Basically, the frontend ran out of memory. This would be a good time to split your source code in several files and compile per parts using a BAT script. I think we discussed a way to do just that a few days ago.

By anonymous

incognito ergo sum (116)

anonymous's picture

16-09-2015, 13:10

i don't know how it works in MSX-C but in ANSI C the pointer arithmetic is based on the size of the data type referenced by the pointer.
i mean, "*(ptr + i)" is the equivalent to "ptr[i]" with pointers of any type.
>https://en.wikipedia.org/wiki/Pointer_(computer_programming)#C_arrays

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