Thread: Seg fault trying to dereference a void pointer in a structure?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    Seg fault trying to dereference a void pointer in a structure?

    Hi, I must be doing something wrong.

    p_stat_t structure has members such as

    uint32_t sys_counter
    uint8_t cpu_usage


    How can I print sys_counter in the function below?


    Code:
    void(*)handleCurrentStatus( p_handle dh, p_time_t tm, const p_stat_t * statusInfo, void * arg )
    {
        printf( "Got status at %u\n", tm.sec );    
        printf( "System config is %u\n", *(int*)statusInfo->sys_counter );    
    }
    It compiles but seg faults. Argg..


    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you just want:
    Code:
    printf("System config is %u\n", statusInfo->sys_counter);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > How can I print sys_counter in the function below?
    Well you could

    > printf( "System config is %u\n", *(int*)statusInfo->sys_counter );
    a) put a breakpoint on this line, then examine sys_counter to see if it looks like a valid pointer
    b) just look at the code and wonder whether all that casting of random integers to a pointer, then dereferencing it makes any sense to you at all.
    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.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    For reference, you can never get a segfault by dereferencing a void pointer, because you cannot actually compile a program that attempts to do so, hence there is nothing to run, and hence there is nothing to segfault.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. null pointer dereference
    By qwertylurker in forum C Programming
    Replies: 3
    Last Post: 03-14-2011, 12:06 AM
  2. Big structure + file pointer = segmentation fault
    By ERJuanca in forum C Programming
    Replies: 6
    Last Post: 03-02-2010, 05:46 PM
  3. Dereferencing a Void Pointer in a Structure
    By simpsonseric in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 04:58 PM
  4. Pointer dereference
    By taurus in forum C Programming
    Replies: 1
    Last Post: 11-09-2008, 07:41 AM
  5. Dereference pointer to void pointer to member
    By phil in forum C Programming
    Replies: 5
    Last Post: 04-20-2005, 11:54 AM