Home > Archive > Unix Programming > March 2006 > large struct cause segmentation fault
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
large struct cause segmentation fault
|
|
|
| Hi, everybody:
I am puzzled by the segmentation fault caused by a large struct
malloced in my program, see below the actual code, the program will
cause segmentation fault when I try to acess bb->xyz[20[1000], I am
running on solaris 2.8 using gcc 2.8.1, and other than the stack size
is limited to 8M, all other resources such as virtural memory are
unlimited, my physical memory is 256M, my virtual memory is more than
1G.
So, what is wrong?
Regards
/* code begins */
#include <stdio.h>
typedef struct abc {
char efg[128][1024*1024];
char ijk[128][1024*1024];
char xyz[128][1024*1024];
} ABC;
main()
{
ABC *bb = 0;
bb = (ABC *)malloc(sizeof(ABC));
printf("access head of ABC\n");
bb->ijk[0][0] = 0; /* this is ok */
printf("acces head ok.\n");
printf("access tail of ABC\n");
bb->xyz[20][1000] = 0; /*crash here */
printf("acces tail ok.\n");
}
/* code ends */
| |
| Aloha 2006-03-30, 10:00 pm |
| And I tested the same code on linux and Darwin using gcc, they both
work.
Also I tested on solaris with 1G physical memory and the program failed
as well. The solaris machine I tested are ultra10 and blade1500, I
don't have a x86 solaris, anybody can do me a favor to test the program
on x86 solaris? Thanks a lot
| |
| Ian Collins 2006-03-31, 4:01 am |
| Aloha wrote:
> Hi, everybody:
>
> I am puzzled by the segmentation fault caused by a large struct
> malloced in my program, see below the actual code, the program will
> cause segmentation fault when I try to acess bb->xyz[20[1000], I am
> running on solaris 2.8 using gcc 2.8.1, and other than the stack size
> is limited to 8M, all other resources such as virtural memory are
> unlimited, my physical memory is 256M, my virtual memory is more than
> 1G.
> So, what is wrong?
>
Works fine on my Solaris laptop with Sun Studio.
> Regards
>
> /* code begins */
>
> #include <stdio.h>
>
missing <stdlib.h>
> typedef struct abc {
> char efg[128][1024*1024];
> char ijk[128][1024*1024];
> char xyz[128][1024*1024];
> } ABC;
>
> main()
int main()
> {
> ABC *bb = 0;
>
> bb = (ABC *)malloc(sizeof(ABC));
>
> printf("access head of ABC\n");
>
> bb->ijk[0][0] = 0; /* this is ok */
>
> printf("acces head ok.\n");
>
> printf("access tail of ABC\n");
>
> bb->xyz[20][1000] = 0; /*crash here */
>
> printf("acces tail ok.\n");
>
> }
> /* code ends */
Isn't that rather obvious?
--
Ian Collins.
| |
| Rick Ingham 2006-03-31, 4:01 am |
| Aloha wrote:
> Hi, everybody:
>
> I am puzzled by the segmentation fault caused by a large struct
> malloced in my program, see below the actual code, the program will
> cause segmentation fault when I try to acess bb->xyz[20[1000], I am
> running on solaris 2.8 using gcc 2.8.1, and other than the stack size
> is limited to 8M, all other resources such as virtural memory are
> unlimited, my physical memory is 256M, my virtual memory is more than
> 1G.
> So, what is wrong?
>
> Regards
>
> /* code begins */
>
> #include <stdio.h>
>
> typedef struct abc {
> char efg[128][1024*1024];
> char ijk[128][1024*1024];
> char xyz[128][1024*1024];
> } ABC;
>
> main()
> {
> ABC *bb = 0;
>
> bb = (ABC *)malloc(sizeof(ABC));
>
> printf("access head of ABC\n");
>
> bb->ijk[0][0] = 0; /* this is ok */
>
> printf("acces head ok.\n");
>
> printf("access tail of ABC\n");
>
> bb->xyz[20][1000] = 0; /*crash here */
>
> printf("acces tail ok.\n");
>
> }
> /* code ends */
>
I would start with two things:
1) #include <malloc.h>
2) Check for an error from malloc() before trying to access the memory.
Could this be a ulimit() problem?
| |
|
| Ok, I forget to include stdlib.h and my limits are OK.
| |
|
| Thank you Ian, so what is the compiler of Sun Studio on your Solaris
laptop, if it is gcc, could you please let me know the version.
| |
| Ian Collins 2006-03-31, 7:01 pm |
| Aloha wrote:
> Thank you Ian, so what is the compiler of Sun Studio on your Solaris
> laptop, if it is gcc, could you please let me know the version.
>
Sun Studio is the compiler (Sun cc).
--
Ian Collins.
|
|
|
|
|