Home > Software design >  C: string limit? No compiler err/warn- why?
C: string limit? No compiler err/warn- why?

Time:05-11

I am coding some mqtt stuff. The maximum message size for a valid mqtt message is 268435455 bytes approx 260MB.

Now I encountered several segfaults in my code and it came down I can not use a *char of this size in C (Rasbian/ Debian 9: Linux zentrale 5.10.103-v7 #1529 SMP Tue Mar 8 12:21:37 GMT 2022 armv7l GNU/Linux).

For testing purposes I wrote the following example program:

#include <stdio.h> 
#include <string.h>
#define MAX_STRING 268435456 

int main()
{
int i;

void function()
{
        char    message[MAX_STRING];

        printf("0\n");
        memset(message,0,sizeof(message));
        printf("1\n");
        return;
}

i=1;
printf("i: %d\n",i);
i  ;
printf("i: %d\n",i);
function();
printf("i: %d\n",i);
}

I can compile it without any warnings or errors. When running I am getting the following output:

root@rasbpi:/src# ./Beispiel 
i: 1
i: 2
Segmentation fault

(original in German: Speicherzugriffsfehler)

I do not understand why I am getting the segfault. If the size is too large shouldn't the compiler complain? If there is no limit and the system is short on memory (which it isn't!) I should get OOM error, but not a segfault.

Anyone able to explain it to me?

Thanks

/KNEBB

CodePudding user response:

You are allocating the space for the character array on the stack.

Compilers commonly don't know how much memory is provided for the stack. It would make no sense, since they don't know how much stack the other modules of the final program might use. And if recursion comes into play, the used stack space can be indeterminable.

The linker cannot do it either.

The target's ABI defines the size of the stack.

Common targets have stacks starting at a few dozen bytes (microcontroller) up to some megabytes (PCs).

So you are not hitting a limit of the language, but of your target.

Solve your issue by allocating the needed space with malloc().

Off-topic note: You cannot define a function inside another function.

  • Related