Thread: Nesting functions which take same pointer as arguments

  1. #1
    Registered User
    Join Date
    Dec 2011

    Nesting functions which take same pointer as arguments

    Please open the file code.c in a suitable text editor (like notepad++, to help keep track of the line numbers)

    Line 738: Let us consider the variable R (5th argument in the function MP_modKalMontInv() ). R is passed by reference so the function MP_modKalMontInv() should get an address to R

    Line 565: Inside the function MP_modKalMontInv(), R is further passed into the function MP_monmul_SandK_ver() which is also declared to take address of R (function definition at Line 293)

    Line 310: The address of R is further passed into show_mp_int_fullinfo() but the program now crashes at Line 14 (line 14 is not executed). Why should that happen and what can I do to correct it?

    This program uses the LibTomMath library. R is of type mp_int, which is a structure declared in the LibTomMath library as
    typedef struct {
       int used, alloc, sign;
       mp digit *dp;
    } mp int;
    At the menu screen enter 9
    Then enter the following values sequentially when asked
    then press any keys except q at the getch()'s and when the program flow reaches the line described above the program crashes
    I have recreated a similar scenario in the file code.c, however that one doesn't crash.

    Could anyone please give me some idea about this?
    Thank you very much.

    PS: I am using Codeblocks 8.02 with gcc version 3.4.5 (mingw-vista special)
    Attached Files Attached Files
    Last edited by mahaju; 01-29-2012 at 07:39 AM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Another hint:
    by adding
    if(!a) {printf("Null Pointer"); exit(0);}
    at the beginning of the function show_mp_int_fullinfo() it looks like the program is crashing due to null pointer assignment when calling the function
    My question is why is this happening, when it does not happen in the other program code.c ?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    My suggestion is that now is a really good time to start learning about debuggers.

    You can use it to catch the moment a program crashes, and then look around the program state to see what went wrong. If a param is NULL, then you can step out a stack frame and see why the caller passed in NULL to begin with.

    You can set breakpoints near to where you suspect a problem, then single step the code until the problem reveals itself.

    You can even change variables on the fly to be "correct", just to see how the code would behave before you re-edit the code and test again.

    Dumping 500+ line programs for someone else to fix is not a long term strategy. What happens when you get to say 5K lines and you still don't know about debuggers.
    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: 5
    Last Post: 07-28-2011, 06:45 AM
  2. Functions as arguments
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 04-19-2008, 11:11 AM
  3. Passing arguments between functions
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-17-2006, 04:59 PM
  4. help with functions using variables as arguments
    By kristentx in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2006, 10:36 AM
  5. functions and pointer arguments
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2002, 05:04 PM