The code:
int main(int argc, char *argv[])
{
typedef struct lb_data_US
{
char uname[255];
char Eid[4];
char myrole[4];
char Login_t[30];
char Logout_t[30];
char ClientIP[20];
char ZoneName[256];
};
int x = atoi(argv[1]);
lb_data_US lb_local[x];
printf("stands for %d value\n", x);
exit(0);
}
When I run this code using ./structure_testop 20995, it runs completely but when i run this code with a larger argument (like 20996 or more), it fails occasionally…
When i tried to debug it by gdb it says
Program received signal SIGSEGV, Segmentation fault.
0x003d6773 in _IO_vfprintf_internal (s=<value optimized out>, format=<value optimized out>, ap=<value optimized out>)
at vfprintf.c:233
233 int save_errno = errno;
Current language: auto; currently c"
Can anyone explain this?
Assuming you are actually using C++ which supports dynamic array sizing like that, consider what an argument of 20995 does: it dynamically allocates 20995 times
sizeof lb_data_US(which is about 600) for a total allocation of 12.5+ megabytes. Few environments support such a large stack size. Instead, use the heap viamalloc()supported in many environments for process limit sorts of sizes.