Garbage value even after initializing members of a dynamically allocated array of struct

I have a dynamically allocated array of structures, 'buff'. Each element is a structure that has several integer variables and a buffer_ptr pointer that points to another dynamically allocated array of structures. The size of both arrays is specified as input on the command line.

int buffer_size;
int user_num;
struct tuple
{
  char userID[5];
  char topic[16];
  int weight;
};
struct buff_ctrl    
{
  struct tuple* buffer_ptr;
  int in;
  int out;
  int numItems;
  int done;
};

Arrays are created and initialized in main () as follows:

int main(int argc, char* argv[])
{
  void *status;
  pthread_t mapThd;
  if(argc != 4)
  {
    printf("Input format: ./combiner <buffer_size> <number_of_users> <input_file>\n");
    return -1;
  }
  buffer_size = atoi(argv[1]);
  user_num = atoi(argv[2]);
  struct buff_ctrl *buff = (struct buff_ctrl*)malloc(user_num * sizeof(struct buff_ctrl)); 
  for(int i=0; i<user_num; i++)
  {
    struct buff_ctrl* curr_buff = (buff + (i*sizeof(struct buff_ctrl)));
    struct tuple *ptr = (struct tuple*)malloc(buffer_size * sizeof(struct tuple));
    curr_buff->buffer_ptr = ptr;//points to another array
    curr_buff->in = 8;
    curr_buff->out = 4;
    curr_buff->numItems = 7;
    curr_buff->done = 0;
    printf("%p\n",curr_buff);
  }

Then I need to pass the buff pointer as an argument when creating the thread using pthread_create:

  pthread_create(&mapThd, NULL, mapper, (void*)buff);
  pthread_join(mapThd, &status);
  free(buff);
  /*end of main*/

My function pointer looks like this:

void* mapper(void *buff)
{
  struct buff_ctrl* arr = (struct buff_ctrl *)buff;
  struct buff_ctrl* temp_ptr;
  printf("######################################################\n");
  for(int k=0; k<user_num; k++)
  {
    /*Printing just to check values */
    temp_ptr = arr + (k*sizeof(struct buff_ctrl));
    printf("buffer ptr =  %p\n", temp_ptr->buffer_ptr);
    printf("in =  %d\n", temp_ptr->in);
    printf("out =  %d\n", temp_ptr->out);
    printf("numItems =  %d\n", temp_ptr->numItems);
  }
  printf("######################################################\n");
  pthread_exit((void*)buff);
}

'buffer_ptr' ( ), ODD user_num 'buff', pthread_create! pthread, .

+4
3

struct buff_ctrl* curr_buff = (buff + (i*sizeof(struct buff_ctrl)));

struct buff_ctrl* curr_buff = buff + i;

buff + i , , buff. i*sizeof(struct buff_ctrl), , .

:

malloc. sizeof(<type>) sizeof *variable, , sizeof(<type>).

:

struct buff_ctrl *buff = malloc(user_num * sizeof *buff);

...

struct tuple *ptr = malloc(buffer_size * sizeof *ptr);

, :

for(int i=0; i<user_num; i++)
{
    buff[i].buffer_ptr = malloc(buffer_size * sizeof *buff[i].buffer_ptr);
    buff[i].in = 8;
    buff[i].out = 4;
    buff[i].numItems = 7;
    buff[i].done = 0;
}

malloc. NULL, .

+4

:

struct buff_ctrl* curr_buff = (buff + (i*sizeof(struct buff_ctrl)));

, , sizeof. .

buff , .

  for(int i=0; i<user_num; i++)
  {
    struct tuple *ptr = malloc(buffer_size * sizeof(struct tuple));
    buff[i].buffer_ptr = ptr;//points to another array
    buff[i].in = 8;
    buff[i].out = 4;
    buff[i].numItems = 7;
    buff[i].done = 0;
  }

. malloc?

+3

.

Pointer arithmetic works by adding an offset to the multiplicity of the pointer type, so adding the offset itself will not work, as you probably expect.

If it was a pointer char *, then you will need to add the offset manually, the increments will be multiplied by units. But in your case, increments by nare multiplied by the size of the base type of the pointer.

There are times when you draw pointer arithmetic with a note about adding, but in most cases it is much clearer to write the index notation.

+1
source

Source: https://habr.com/ru/post/1693387/


All Articles