integer in struct equals zero later.

This is a discussion on integer in struct equals zero later. within the C Programming forums, part of the General Programming Boards category; Code: /* * Instruction_t - stores a specific instruction */ typedef struct { /* * action - CPU or IO ...

  1. #1
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50

    integer in struct equals zero later.

    Code:
          
    
    /*
     * Instruction_t - stores a specific instruction
     */
    typedef struct
    {
            /*
             * action - CPU or IO
             */
            Action_t action;
    
            /*
             * burstTime - the time taken in the instruction.
             */
            int burstTime;
    
    } Instruction_t;
    
    
    
      static void loadInstruction( Action_t actionType, int burstTime, PA_t *pa)
            {
                    Instruction_t *instruction;
    
                    instruction = (Instruction_t *)malloc( sizeof( Instruction_t));
    
                    instruction->action = actionType;
                    instruction->burstTime = burstTime;
    
                    insertLastElement( pa->instructions, (void *)instruction);
            }
    
    /* Here pa->instructions is a Linked List, and insertLastElement is defined elsewhere. */
    I have a problem. When I run my program (which contains this), through gdb, I can see that burstTime does have a value ( such as 1, 3, 15, etc).

    But when I dequeue from my list, instruction->burstTime suddenly equals zero!

    Is there something I'm not understanding? Or isn't it anything to do with this part of the code?
    Last edited by The Doctor; 05-15-2013 at 12:34 AM.
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    My guess is that the problem lies elsewhere.

    By the way, there is no need to cast a pointer type to void* since the conversion will happen automatically (even if you intend this to be compilable as C++).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    This is part of a Uni assignment, and the coding style I was taught was to cast it back to the specific type anyway, so that's the style I will follow.

    Boy is this bug annoying... You really can shoot yourself in the foot with C, lol.
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Quote Originally Posted by The Doctor
    This is part of a Uni assignment, and the coding style I was taught was to cast it back to the specific type anyway, so that's the style I will follow.
    I would consider that to be bad style: unnecessary explicit casting can hide potential problems.

    Quote Originally Posted by The Doctor
    Boy is this bug annoying...
    You are tracing what happens to that variable using a debugger, right?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Well, everyone makes a mistakes with list implementations... It's funny how something so simple tends to go wrong at first attempt - no matter how many lists have you written before =)

    Anyways, few things which might help you find the issue.

    1. Using gdb, ensure that the list item location (memory address) the data resides is same when you store and fetch the item.
    if it is, then something in your program accidentally writes to that exact location, or frees the memory before you read it.
    2. Try using valgrind to detect accidental writes to and reads from unallocated space

    Oh, also gdb's hardware watchpoints are nice.
    Debugging with GDB

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,283
    Quote Originally Posted by The Doctor View Post
    Qhen I dequeue from my list, instruction->burstTime suddenly equals zero!
    Assuming you have a source level debugger, write down the addresses for the first few instances of instruction and the burstTime values at the time of insertLastElement() and when you deque.

    If your debugger supports it, you could add the first few instances of instruction to the "watch" window and check to see if they get changed.

    Better still, if your debugger supports it, set a data breakpoint on a specific instance of instruction->burstTime. This will cause a break to happen if burstTime is written to when it shouldn't be written to (don't set the data breakpoint until after it's not supposed to change).

    If you're running under windows, visual c++ express debugger includes these features.

  7. #7
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Hey guys, is there an easy way in gdb, to keep track on the value of one particular instance of a struct?

    Even if I could keep track of each instance of the struct at all points it is accessed and/or modified, that would help heaps!

    I really only know how to tell gdb to break at a particular function and display the values of it

    (it displays enums as some value like 678598, which is not entirely helpful... :-\ And yes,)
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,362
    Hey there

    I noticed something about your declarations - You are accidently making your program non-standard by adding "_t" at the end of your variables

    7.26 Future library directions
    #1
    The following names are grouped under individual headers for convenience. All external names described below are reserved no matter what headers are included by the program.

    ...

    7.26.8 Integer types <stdint.h>
    #1
    Typedef names beginning with int or uint and ending with _t may be added to the types defined in the <stdint.h> header. Macro names beginning with INT or UINT and ending with _MAX _MIN, or _C may be added to the macros defined in the <stdint.h> header.
    However, I see that it's probably too late to go through and change all your code.

    Also, your malloc would be better like this, in case you want to change instruction's datatype.
    Code:
    instruction = malloc (sizeof ( *instruction));
    As for the 0 -> I'd recommend that you use the assert.h library
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    They aren't variables, they're custom Data types. ( they're structs, and I've used typedef so I don't have to type struct Instruction everywhere).

    I first learned to program in Java, so I like using typedef to make it look more familiar to me.

    I ended those things with _t to make it easier to differentiate between the Data Types, and the "objects".
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Quote Originally Posted by Click_here
    I noticed something about your declarations - You are accidently making your program non-standard by adding "_t" at the end of your variables
    You're misreading the standard. Action_t is not reserved, but interesting_action_t is reserved. However, I have heard that POSIX does reserve names ending with _t in general.

    Quote Originally Posted by The Doctor
    I first learned to program in Java, so I like using typedef to make it look more familiar to me.

    I ended those things with _t to make it easier to differentiate between the Data Types, and the "objects".
    Since you are already using camel case to style type names as how they would be styled in typical Java code, you might as well stick to that as a personal style in C.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,362
    Thanks for clarifying that for me, laserlight
    Fact - Beethoven wrote his first symphony in C

  12. #12
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Quote Originally Posted by The Doctor View Post
    Hey guys, is there an easy way in gdb, to keep track on the value of one particular instance of a struct?

    Even if I could keep track of each instance of the struct at all points it is accessed and/or modified, that would help heaps!

    I really only know how to tell gdb to break at a particular function and display the values of it
    Quote Originally Posted by Maz View Post
    Oh, also gdb's hardware watchpoints are nice.
    Debugging with GDB
    Did you check that out?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct integer member has wrong value
    By DoomerMrT in forum C Programming
    Replies: 2
    Last Post: 02-23-2013, 07:20 AM
  2. The sum that does not equals the sum. gdb involved.
    By nepper271 in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2012, 11:40 AM
  3. how to make a if a char equals something
    By Megamanenm in forum C++ Programming
    Replies: 18
    Last Post: 04-15-2009, 05:02 AM
  4. Equals()
    By siavoshkc in forum C# Programming
    Replies: 5
    Last Post: 09-27-2006, 07:05 AM
  5. Why strcmp equals zero
    By barim in forum C Programming
    Replies: 5
    Last Post: 07-21-2004, 08:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21