Like Tree2Likes

Initialize struct in function

This is a discussion on Initialize struct in function within the C Programming forums, part of the General Programming Boards category; I have a segmentation fault in this program Code: struct obj { wchar_t *name; struct obj *father; }; struct pbl ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    I have a segmentation fault in this program

    Code:
    struct obj {
        wchar_t *name;
        struct obj *father;
    };
    struct pbl {
        wchar_t *name;
        struct obj *obj;
        struct pbl *next;
    };
    struct pbt {
        wchar_t *name, *path;
        struct pbl *first;
    };
    
    int newTarget(struct pbt** t ){
    
        int
            ret = -1;
    
        *t = malloc( (size_t) sizeof(struct pbt)); // <<<< HERE
        if (!(*t)) 
            goto endl;
    
        ret = 0;
    
        endl:
        return ret;
    }
    
    int main(int argc, char** arg){
    
        int
            ret = -1;  
    
        struct pborca_pbt
            **target = NULL;
    
        if ( newTarget(target) )
            goto endl;
    
        ret = 0;
    
        endl:
        return ret;
    }
    Probably it's because I'm assigning the memory from malloc to *t, and t is NULL.
    But how can I malloc a structure passed by reference then?
    I guess I'm missing the point.

    EDIT:
    Yeah, ofc, I've found the answer.
    target is getting passed by copy.
    What i need is

    Code:
    struct pborca_pbt *target;
    
    if ( newTarget( &target) ) goto endl;
    Last edited by lordkrandel; 04-01-2011 at 02:25 AM. Reason: bad formatting

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Is goto really necessary here?
    mike65535 likes this.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by Bayint Naung View Post
    Is goto really necessary here?
    Cprogramming.com Tutorial - Articles - When to use Goto

    The example program is trivial, but I tend to "goto" like that in C.
    Last edited by lordkrandel; 04-15-2011 at 10:00 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Goto is NOT an answer... it's a resignation. Something you use as an absolute last resort before giving up on the project.

    Your NewTarget function would probably work a lot better like this...

    Code:
    void * newTarget(int size)
       { return malloc(size); }
    But then you might as well just use...
    Code:
        struct pborca_pbt *target = malloc(sizeof(struct pbt));
        if ( !target )
         { puts("Malloc failed");
            exit(0); }
    Right in your mainline code.

    Oh and a little tip for readability... don't split your variable definitions across lines, especially if you want to be able to read your own code 5 years from now.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Anyone codes the way he wants.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Quote Originally Posted by lordkrandel View Post
    Anyone codes the way he wants.
    You never worked for a living, huh?

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Sure, we're just giving advice/suggestion.
    Unnecessary use of goto makes code difficult to follow/understand and causes spaghetti code.
    But if you are working with team and such, you'll have to follow the rules and such....

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    And this is such a lame and unnecessary place to use goto...

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mike65535 View Post
    And this is such a lame and unnecessary place to use goto...
    Yep... especially when instead of...
    Code:
        if (!(*t)) 
            goto endl;
    
        ret = 0;
    
        endl:
        return ret;
    He could have simply used this...
    Code:
        if (!(*t)) 
         return ret;
        return 0;

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lordkrandel View Post
    Anyone codes the way he wants.
    Yep, nothing is stopping you from writing terrible code that nobody can follow.

    But don't count on earning a paycheck with it...

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by CommonTater View Post
    Yep, nothing is stopping you from writing terrible code that nobody can follow.

    But don't count on earning a paycheck with it...
    Are you really judging my professionality from two absolutely trimmed down functions from a personal project which just served as example to ask a question about a completely different issue after giving me unrequested advices on my coding style? Good.

    Can't you follow my example code because the type and the name of the variable are on two separate lines and your visual pattern recognition skills are limited enough to not let your brain understand what is going on?

    Sure, if the function was all there, there would be no goto.
    Probably there would be no function.
    And no program, as it actually does nothing useful.
    And I wouldn't get a paycheck for my work as a professional coder.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by Bayint Naung View Post
    Unnecessary use of goto makes code difficult to follow/understand and causes spaghetti code.
    I cannot really understand why using goto in a specific and determined and well-detailed way can lead to "spaghetti code". You have to follow rules when you program in team. Well, ok, can't my rule be: use goto JUST LIKE THAT and don't use it for any other reason?

    No no, no. It will SURE LEAD to SPAGHETTI CODE.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    If you cannot see the inherent advantages to a construct like Tater's example then I wouldn't worry too much about running a team.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lordkrandel View Post
    Are you really judging my professionality from two absolutely trimmed down functions from a personal project which just served as example to ask a question about a completely different issue after giving me unrequested advices on my coding style? Good.

    Can't you follow my example code because the type and the name of the variable are on two separate lines and your visual pattern recognition skills are limited enough to not let your brain understand what is going on?
    It was a suggestion from one person with an unusual coding style to another... I write functions like this...
    Code:
    int function (int param)
      { if (param < 0)
           { param = abs(param) }
         return param *11; }
    I know how people are about following code... when things are not where they expect them to be (as in my bracketing style) they really can't follow the code as easily or as comfortably as they would with a style they're used to ... and yes I've been taken to task for it more than once. Why? Because it makes my code harder (for them) to read... even though for me it's as comfortable as typing my name.

    So yes... I actually am telling you --from personal experience-- that anything that makes your code harder to follow is going to seriously degrade the level of support you will get in these forums... and in the real world where a paycheck depends on stuff just that trivial.

    Sure, if the function was all there, there would be no goto.
    Probably there would be no function.
    And no program, as it actually does nothing useful.
    And I wouldn't get a paycheck for my work as a professional coder.
    So you decided to take up our time with it .... because???

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Because I had a problem (that I solved myself in 3 minutes) and I didn't ask you to teach me the truth about gotos or inline functions, or teamworking, or code-style-marketing. That was just something you wanted to do.

    If your job vitally depends on variable definition being on one single line instead of two, I'd really change my job if I were you.
    My job does depend on fulfilling requests in a functional way and I'm not gonna be fired tomorrow, so I guess all this arguing is kinda futile.

    I know perfectly how to write an if and a normal error handling "hey guys something is wrong let's quit it" but if I _don't_ want to use it I guess it's none of your business.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21