Thread: function loading problem

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    62

    function loading problem

    I load a function from main.cpp. It includes a file where the function is.
    The functions header is:

    Code:
    void my_function(int buff[], int length)
    I make this call from main.cpp:

    Code:
    my_function(buff[2], length);
    buff[2] is an array which contains 0 and 1. length's value is 2.

    When I debug the values are correct, but immediately after loading the function, they turn into different values right after function call. It seems that it doesn't have anything wrong with code, the function doesn't even change these values, it only uses them in calculations.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I believe you could change:
    Code:
    void my_function(int buff[], int length)
    my_function(buff[2], length);
    to
    Code:
    void my_function(int *buff, int length)
    my_function(buff, length);
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Sorry, I gave you some wrong information.
    The call I use is:

    Code:
    int length=2;
    int buff[2]={0, 1};
    my_function(buff, length);
    I mean I throw the whole buffer into function. Is it allowed? It worked under a bit different conditions and I fear it might give unstable results.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it works. And...
    Code:
    void my_function(int buff[], int length);
    ...and...
    Code:
    void my_function(int* buff, int length);
    Are exactly the same thing.

    But this is C++, so it's better if you were to use std::vector instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    But why do I get such results when debugging? There's no way in code that would change those variables.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I can't say. It must depend on HOW you're debugging it, because the code is correct.
    Although you may want to note that the array is an array no longer when passed to the function. In that function, it's a pointer, regardless of syntax.
    And that means you can only see the first value, unless you manually tell the debugger to print the value of buff[0] and buff[1]. Perhaps that's the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Cause (where the bug really is) and effect (where you happen to notice something is wrong) are seldom in the same place when it comes to more complicated programs.

    You can post all the snippets of where the effect is, but that won't tell us anything. All we'll be able to tell you is there's nothing wrong with that bit of code. If you copied the same code into an empty project and just called it, you'd see that too.

    The ideal scenario (for you) is being able to produce a cut down copy of the program (to a few hundred lines) which demonstrates the problem, which you can then post as a complete program. We could probably figure something out from that.

    If it's thousands of lines long, then you're pretty much on your own. Going through all the code run up to that point in detail, considering any potential for corrupted memory as you go.

    It's one of those "rites of passage" things on the way to becoming an experienced programmer. Avoiding such problems in the first place, and knowing how to go about solving them if they do show up.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM