[RESOLVED] Problem when passing reference to struct to function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: [RESOLVED] Problem when passing reference to struct to function

  1. #1
    Join Date
    Jun 2017
    Posts
    4

    [RESOLVED] Problem when passing reference to struct to function

    Hi there I have a problem regarding passing references as an argument to a function.
    That I would really like to have an expert opinion on.

    So I have this double** that I pass to function called func, which runs multiple times (several thousand times in some cases).

    This double (d) is part of a struct:

    I then go and do boostrapping on d, meaning sampling rows from d randomly (with replacement), via this function:

    The problem is that sometimes when I run this program I get a segmentation fault after X bootstrapping runs.

    However I have discovered that this only happens when I call the function func with a reference to my struct:
    (In which it also runs faster, which I guess is consistent with theory)

    And not when I do like this:

    The error I get is:

    So does anyone have an idea of why this is happening? I mean one should be able to pass a struct as a reference in C++. Does it have anything to do with the implementation of the boostrap function?
    Last edited by lolller; July 20th, 2017 at 01:09 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,633

    Re: Problem when passing reference to struct to function

    Can you post the code body for the function func() as you have only posted the code for bootstrap().
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  3. #3
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    693

    Re: Problem when passing reference to struct to function

    The problem is most probably concerned with improper allocation of the memory for the pointers.
    It'll be interesting to see how the freeing of the memory is organized.
    Last edited by AvDav; June 5th, 2017 at 02:30 PM.
    Popular opinion is the greatest lie in the world.

  4. #4
    Join Date
    Jun 2017
    Posts
    4

    Re: Problem when passing reference to struct to function

    Here is the func:

    And here is the calc inside it:

    Where:

    And I deallocate it like this:
    Last edited by lolller; July 20th, 2017 at 01:09 PM.

  5. #5
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    693

    Re: Problem when passing reference to struct to function

    Why not to use std::vector instead, then it would be possible to clean some mess code in your project.
    Popular opinion is the greatest lie in the world.

  6. #6
    Join Date
    Jun 2017
    Posts
    4

    Re: Problem when passing reference to struct to function

    I have tried that, and I found that using pointers is considerably faster, when doing many iterations.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,633

    Re: Problem when passing reference to struct to function

    Code:
    double sumA[y];
      double sumB[y];
      
      
      double sumA2[y];
      double sumB2[y];
    What compiler are you using as that's not standard c++ as the array size is determined at run-time and not compile time.

    However, you are passing dgd (which contains dynamic memory pointers) by value (eg returning new from allocDgd() - though a variable called new isn't recommended) without having a proper copy-constructor for dgd - which is messing with memory. I would suggest that you implement a constructor, destructor, copy constructor, move constructor, copy assignment and move assignment for dgd. Also with c++ you don't need the typedef as you do with c.
    Last edited by 2kaud; June 5th, 2017 at 02:55 PM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  8. #8
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    693

    Re: Problem when passing reference to struct to function

    Premature optimization is evil, (C) D. Knuth.
    Popular opinion is the greatest lie in the world.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,633

    Re: Problem when passing reference to struct to function

    Quote Originally Posted by lolller View Post
    I have tried that, and I found that using pointers is considerably faster, when doing many iterations.
    Were you passing the vectors by value or by reference? If by value, then yes using vectors would be much slower then passing pointers.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  10. #10
    Join Date
    Feb 2017
    Posts
    128

    Re: Problem when passing reference to struct to function

    Quote Originally Posted by lolller View Post
    I have tried that, and I found that using pointers is considerably faster, when doing many iterations.
    I doubt that.

    If it's not legacy code this kind of error-prone programming is inexcusable today. It's unprofessional. You are deliberately shooting yourself in the foot and then come asking why it hurts.

    I suggest you rewrite in modern C++ concentrating on high quality working code. If it turns out slower than expected ask about that instead.

  11. #11
    Join Date
    Jun 2017
    Posts
    4

    Re: Problem when passing reference to struct to function

    Ok thanks a lot for the input I restructured it to make it work with vectors, passing them by reference.

    I used G++, which allows for array to be declared with variables and thereby for the size to be determined at run-time instead of compile-time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)