SOLVED Silly Question

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay, this silly but I have a buffer called, " *identifier->data" I'm filling the buffer 

and displaying the results saved in it.  However, at the end when I go to display some (300) of the data it displays all zeros.  Why????

 

Thanks guys for looking!

 

for(int i = 0; i <  identifier->nr - 1; i++){
  
  Bool flags = fragment->flags; int offset = fragment->offset;
  
  if(flags && (offset == 0)){
   
   for(int j = 0; j < fragment->length - (20 + 8); j++){  // WORKS
    * identifier->data = fragment->data[j]; cs_print_string(" "); cs_print_string(itoa(* identifier->data)); identifier->data++;
   }
  }
  else if(flags && offset != 0){
   for(int j = 0; j < fragment->length - 20; j++){  // WORKS
    * identifier->data = fragment->data[j]; cs_print_string(" "); cs_print_string(itoa(* identifier->data)); identifier->data++;
   }
  }
  else if(!flags && offset){
   for(int j = 0; j < fragment->length - 20; j++){  // WORKS
    * identifier->data = fragment->data[j]; cs_print_string(" "); cs_print_string(itoa(* identifier->data)); identifier->data++;
   }
  }
  fragment = fragment->next;
 }
 
 identifier->data = &identifier->data[0];
 for(int k = 0; k < 300; k++){ // DOES NOT WORK!!!
  cs_print_string(" "); cs_print_string(itoa(* identifier->data)); identifier->data++;
 }
 

 

 

Last Edited: Tue. Oct 3, 2017 - 01:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh, and the identifier struct is as follows

 

struct IP_IDENT{
 unsigned char
 
 size,   /* Telegram Size */
 sequence,
 ident[2],  /* Telegram Ident */ 
 
 source_mac[6],
 source_ip[4],
 
 * data,
 type;
 
 int computed_offset;
 
 struct IP_FRAGMENT * start;
 int nr;
 
 struct IP_IDENT * next;
 struct IP_IDENT * prev;
};

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
identifier->data = &identifier->data[0];

This looks a bit silly.

 

The debugger/simulator should be able to enlighten you as to where your pointer is pointing. My guess it isn't quite where you expect.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman you where right.  Pointer was way of.  I saved the start location when I made the malloc and then restored the pointer when I needed/thought I to be at the start.  It's amazing how suddenly you can get lost with pointers even years into programming.

 

Thanks guys...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
    * identifier->data = fragment->data[j]; cs_print_string(" "); cs_print_string(itoa(* identifier->data)); identifier->data++;

Don't do this. MISRA would have a field day. Vertical space costs nothing and makes your code easier to read and maintain:

    * identifier->data = fragment->data[j]; 
    cs_print_string(" "); 
    cs_print_string(itoa(* identifier->data)); 
    identifier->data++;

The first things that jump out of that for me are:

    cs_print_string(itoa(* identifier->data)); 

What compiler are you using that has a ONE parameter version of itoa() ? Is this suggesting that the output (ASCII) buffer is allocated within that function? If so how is it ever freed??

 

But also:

    * identifier->data = fragment->data[j]; 

What is the * doing there? Is this really double indirection ?? Show us the types of "identifier" and "data". Presumably what you have is something like:

typedef struct {
    int nr;
    type * data;
} id_type;

type actual_data[N];

...
  id_type id;
  id.data = actual_data;
  id.nr = N;
  some_func(&id);

?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Vertical space costs nothing and makes your code easier to read and maintain

Absolutely! and the structure definition was pretty bizarrely incomprehensible, too!

 

EDIT

 

Didn't we have this argument before?

 

Is it the OP who likes to cram as much as possible onto each line?

 

EDIT 2

 

There once was a man from Japan

Who wrote limericks that never would scan

 

When told it was so,

He replied, "yes - I know,"

 

"But I always try to cram as many words into the last line as ever I possibly can"

 

laugh

Last Edited: Tue. Oct 3, 2017 - 10:18 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The man might be from Limerick!