Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C: string limit? No compiler err/warn- why?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading