Thread: Pointers - Passing to function

  1. #1
    Registered User
    Join Date
    May 2010

    Pointers - Passing to function


    I need help with the following:

    I am using a C API available in a network simulator. Using the op_ima_obj_svar_get function it takes an object id and the name of the state variable that the user wishes to retrieve and returns a pointer to the variable. Because the state variable can be any type (void*) it must be cast to the appropriate type. DhtT_Key_Bytes is defined in a header file as typedef unsigned char* DhtT_Key_Bytes;

    DhtT_Key_Bytes*		nodesNodeID;
    DhtT_Key_Bytes		predec;
    Int				intpredec;
    nodesNodeID = (DhtT_Key_Bytes *) op_ima_obj_svar_get(rem_proc_objid, "myNodeID");
    predec = dht_sup_get_node_predecessor_idealised (*nodesNodeID);
    intpredec = sha_get_int_id(predec);
    The above works perfectly. Now here’s the problem: I am passing the same to a different function but get an invalid memory access error:

    DhtT_Key_Bytes*		predecessorPtr;
    Int				intPredecessorPtr;
    predecessorPtr = (DhtT_Key_Bytes *) op_ima_obj_svar_get(rem_proc_objid, "predecessor");
    intPredecessorPtr = sha_get_int_id(*predecessorPtr);
    The state variables “myNodeID” and “predecessor” are both of type DhtT_Key_Bytes.

    The only difference that I can make out is that the dht_sup_get_node_predecessor_idealised expects a parameter of type DhtT_Key_Bytes whereas the sha_get_int_id expects a parameter of unsigned char digest[ShaC_Bytes].

    I have used sha_get_int_id function before and it’s always worked just fine when passing in a variable of type DhtT_Key_Bytes.

    I think I’m confused with my pointers. Can someone please help me with this?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Because the state variable can be any type (void*) it must be cast to the appropriate type.
    You do not need to cast void* when assigning to any object pointer type. C does the conversion for you automatically.

    As for the stated problem: I'm confused about sha_get_int_id(). You say it takes unsigned char[] as an argument, but you're passing DhtT_Key_Bytes. Does your compiler complain when you do this? If so, don't do that! If not, then it would appear that DhtT_Key_Bytes is a typedef for a pointer to unsigned char, which means they're interchangeable.

    At any rate, when you get a memory access error, your first step should be to use a debugger to find out what's going on. If it's available for your platform, Valgrind is a fantastic debugger. Otherwise, you probably have some other sort of debugger available, such as gdb or, if you're using an IDE, some built-in debugging tool.

  3. #3
    Registered User
    Join Date
    Oct 2008
    It's the case of the "wild pointer", the double indirect pointer has been initialized to point to an object but not the single indirect one i.e.
    char **dp, *sp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Passing a string array to a function using pointers
    By asofaihp in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 11:31 AM
  3. Passing function pointers
    By keira in forum C Programming
    Replies: 6
    Last Post: 09-29-2007, 03:55 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM