On Sun, 27 Jun 1999, Berkeley Hynes wrote:
> If anyone could help me out with the following, it would be much
appreciated:
>
> I have compiled a relatively large C program with gcc (on a
linux
> 486 machine) using: gcc prog.c -lgdbm
>
> Part of the code defining some of the variables of one of the functions
is
> as follows:
>
> datum key;
> datum key1;
> datum key2;
> datum key3;
> datum content;
> datum content1;
> datum content2;
> datum content3;
>
> where datum is defined in the gdbm.h include file as :
> struct{
> char *dptr;
> int dsize;
> } datum
>
> However, the program constantly causes segmentation faults. Examining
the
> program using gdb (after compiling with "gcc prog.c -lgdbm -ggdb")
reveals
> that key, key1, key2, key3, content, and content2 are all fine. ie.
using
> "display content" in gdb returns:
> content={dptr=0xbffffa7c "0", dsize = 1}
> However, using "display content1" in gdb reveals:
> content1={dptr= 0x0, dsize =-1073743240}
> and similarly for content3. And the program causes the segementation
fault
> at the line which calls content1 for the first time.
If gcc had been unable to allocate memory for content1, gdb would be
unable to display it. Since it can in fact display it, but the content
is garbage, it looks to me like you have neglected to assign it any
initial value. char *dptr doesn't allocate any char variable, only a
variable that can hold a pointer to one. If you don't bother to put
one there, it might very well be zero, which will certainly segment
fault when you refer to it.
>
> >From experience programming on other operating systems which shall
remain
> anonymous, I figure I need to swtich to a larger memory model, or
something
> else to allow gcc to allocate memory to content1 and content3 but
nothing in
> the info, man, or other linux programming books tells me how to do
this. Of
> course, this preliminary diagnosis could be entirely wrong...
>
AFAIK there is only one memory model for real c. Automatic variables
come out of the stack, which is finite (try ulimit -a to see what
limit), as do any recursions... if you have a great many variables, you
might make them static or define them outside of any function to put
them in the data segment instead, or malloc them, but I don't think that
is the problem.
> I'm sorry for taking up so much bandwidth with this question, but the
fact
> that out of 8 identical variable declarations 6 were fine and 2 were
not is
> exceedingly puzzling -- normally things either always work or never
work...
>
> Thanks in advance for any help,
>
> Berkeley Hynes
> [EMAIL PROTECTED]
>
Lawson
>< Microsoft free environment
This mail client runs on Wine. Your mileage may vary.
___________________________________________________________________
Get the Internet just the way you want it.
Free software, free e-mail, and free Internet access for a month!
Try Juno Web: http://dl.www.juno.com/dynoget/tagj.