Thread: struct pointer -> Bus Error

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    72

    struct pointer -> Bus Error

    Hi All

    I've written a test program to experiment with structs a little bit:

    Code:
    #include <stdio.h>
    
    struct FOO {
      int id ;
    } foo, *bar;
    
    struct FOO test(struct FOO x) ; // prototype
    
    int main(int argc, char *argv[])
     {
            foo.id = 10 ;
            bar->id = 20 ;
            printf("%d %d\n", foo.id, bar->id) ;
            test(foo) ;
            return 0 ;
     }
    
    struct FOO test(struct FOO s) {
      return s ;
    }
    When I run this program I get a Bus error. As far as I know it has todo with the second bar->id but I can't figure out why.
    Any suggestions ?

    cheers
    Last edited by jeanluca; 04-18-2010 at 02:00 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    bar is a pointer to struct FOO, but it was never initialised beyond the zero initialisation of global variables (i.e., you tried to dereference a null pointer with bar->id).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    72
    ok, I tried the following to fix it:

    Code:
    bar = (struct *FOO)malloc(sizeof(FOO)) ;
    Doesn't compile

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It should be:
    Code:
    bar = malloc(sizeof(*bar));
    If you want to use the struct name, then you should write:
    Code:
    bar = malloc(sizeof(struct FOO));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    72
    malloc returns a void pointer, so I thought you should cast it, like
    Code:
    char *str ;
            str = (char*) malloc( 100 * sizeof(char) ) ;
    I assume this is optional ? Just curieus, can this be done with structs too ?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    void * is convertible to any pointer, and will convert to any pointer provided you're not compiling in C++. If you are in C, you shouldn't be casting the result of malloc.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Doesn't compile
    Doesn't tell us anything either!

    Did you put the statement inside main(), like you should,

    or did you try to initialise it like this(bad)?
    Code:
    struct FOO {
      int id ;
    } foo, *bar = malloc(sizeof(FOO)) ;
    Did you include stdlib.h ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    72
    Code:
    bar = (struct *FOO)malloc(sizeof(FOO)) ;
    doesn't compile, but I found out it should be:
    Code:
    bar = (struct FOO*)malloc(sizeof(FOO)) ;

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jeanluca View Post
    Code:
    bar = (struct *FOO)malloc(sizeof(FOO)) ;
    doesn't compile,
    That is correct, as the "(struct *FOO)" is invalid

    Quote Originally Posted by jeanluca View Post
    but I found out it should be:
    Code:
    bar = (struct FOO*)malloc(sizeof(FOO)) ;
    In C, that conversion is not necessary if you have #include'd <stdlib.h>. However, if your compiler is actually a C++ compiler (and a lot of C compilers are, in practice) then that conversion is required.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    72
    so, its a 'best practice' to always do the conversion youself!?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by grumpy
    That is correct, as the "(struct *FOO)" is invalid
    With respect to the original code snippet posted, the statement is still invalid because sizeof(FOO) is wrong: it should be sizeof(struct FOO).

    Quote Originally Posted by jeanluca
    so, its a 'best practice' to always do the conversion youself!?
    It depends on whether you want your program to be compilable as C++. It is true that many C compilers are bundled with or part of a C++ compiler, but nonetheless you will be able to compile your code as C, otherwise the compiler cannot possibly be regarded as a C compiler.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    72
    thnx

    Well my code is pure C, but I guess it might be useful when it compiles with a c++ compiler too!

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jeanluca View Post
    thnx

    Well my code is pure C, but I guess it might be useful when it compiles with a c++ compiler too!
    You wouldn't want to call malloc() in a C++ program. If for some reason this code ever gets compiled as C++, then you might consider adding a cast, but at this point it's not a good idea to dirty the code because of a hypothetical.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Well my code is pure C, but I guess it might be useful when it compiles with a c++ compiler too!
    Why not make it compile with Fortran while you're at it?

    The apparent similarity of the languages is no excuse for writing some mashed C/C++ code.

    Incompatibilities Between ISO C and ISO C++
    Read this to discover a whole load of issues with respect to turning a valid C program into a valid C++ program. Casting malloc is the least of your worries if you ever seriously compile it as C++.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    72
    fortran 77 or 90 ?

    good point, I'll skip the casts!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM