lundi 29 juillet 2019

segmentation fault in random data stream generator

I'm attempting to initialize two random 512-byte blocks of data, each byte in the block is represented with a defined data type 'byte' which is an unsigned char.

Included here, I have listed my definitions and functions. I keep getting segmentation fault 11. Any ideas about directions I could go to figure out why this isnt working?

I have been trying to go line by line with this but have been having no luck...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef unsigned char byte;
//define the primary object, each block is a linked-list that represents 512 bytes of unsigned chars
struct block
{

  byte res;

  int i;
  int j;
  int k;

  struct block* next;

  };

//function to generate a pointer to randomized 512 byte stream.

struct block* fivetwelvegen(){
  struct block* r;
  int w=32;
  int l=16;
  int ran;
  char hex[] = "0123456789ABCDEF";
  byte g = 1;
  r=0;
for(r->i=1;r->i<=w;r->i++){
  for(r->j=1;r->j<=l;r->j++){
    for(r->k=1;r->k<=(l*w);r->k++){
      ran = rand()%l;
      g = hex[ran];
      r->next->k = g;
      r=r->next;
    }
      if(r->i==(w+1))
        r->i=1;
      }
    }
return r;
}
int main(void){

  srand((unsigned)time(NULL));

  struct block* one;
  struct block* two;
  int w=32;
  int l=16;

  printf("Creating two random 512-byte data streams...\n");

  one = fivetwelvegen();
  two = fivetwelvegen();

  printf("The intializing value of block A is: %hhu\n", one->res);
  printf("The intializing value of block B is: %hhu\n", two->res);
}

Expected to print the initializing (first) value of the 512-byte stream, essestially the value of the first byte of data. Currently I am receiving a segmentation fault 11




Aucun commentaire:

Enregistrer un commentaire