CyberPsychotic wrote:

> ~ > gcc foo.c -o foo
> ~ >  and 
> ~ > gcc -S foo.c
> ~ > gcc foo.s -o foo
> ~ >  
> ~ > should produce similar (well almost) code. However, when in latter case I
> ~ > run foo, I get Bus error(core dumped) message. Ideas what goes wrong here?
> ~ > I think that's something to do with alignment,  but not sure.
> ~ 
> ~ Odd. Try adding `-g' to both commands and running the resulting
> ~ program under gdb.
> 
>   
> bash-2.01$ gcc -S -ggdb foo.c
> foo.c: In function `main':
> foo.c:5: warning: initialization makes integer from pointer without a cast
> foo.c:6: warning: initialization makes integer from pointer without a cast

This is trying to tell you something.

> bash-2.01$ gcc foo.s -o foo.gdb
> bash-2.01$ ./foo.gdb
> Bus error (core dumped)
> bash-2.01$ gdb foo.gdb foo.gdb.core

> Core was generated by `foo.gdb'.
> Program terminated with signal 10, Bus error.
> Cannot access memory at address 0x20015080.
> #0  0x20068c8e in ?? ()
> (gdb) where
> #0  0x20068c8e in ?? ()
> #1  0x2007d060 in ?? ()
> #2  0x2005c456 in ?? ()
> #3  0x1646 in main () at foo.c:8
> (gdb)  

This indicates that your stack has been trashed. Code which is part of
the executable should have an address of around 0x08000000. Code which
is part of a shared library should be around 0x40000000. Anything else
usually indicates a corrupted stack.

> here's the code itself:
> 
> #include <stdio.h>
> 
> void main()
> {
> char foo="bababa";
> char bar="hello world";

These should be `const char []' or `const char *'. As it stands, it
will convert the address of the string constant to an int, then store
the LSB in the variable.

> printf(" foo %s\n",foo);
> printf("%s",bar);

`foo' has type char, which will be promoted to an int (which will be
in one of the ranges 0x00 - 0x7F or 0xFFFFFF80 - 0xFFFFFFFF), which
will then be treated as a pointer.

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to