1

I am trying to generate uint_32 random numbers which should be stored in the buffer array, but somehow, my codes only stores last value every time.

For instance, when I generate random numbers like

12365645
97897875
45458788

then the value of

buffer[0]=12365645
buffer[1]=97897875
buffer[2]=45458788

However currently, I am getting like

buffer[0]=45458788
buffer[1]=45458788
buffer[2]=45458788

Here is my corresponding code but ain't sure where I have made a mistake.

/*Required header files are added*/
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>         
#include <fcntl.h>
#include <stdint.h>

struct thread_arguments
{
    char *buffer;
    char *queue[10];
    uint32_t offset;
    uint32_t r;
    size_t bufferlen;


    size_t minlevel;


}ta;

void randomgenerate();
void constructor(int size, int filllevel);
void put_buffer(int ele);

void printbuf();
int main(void)
{
ta.offset=0;
ta.buffer=NULL;

    constructor(1,3);
    randomgenerate();
    printbuf();

    return EXIT_SUCCESS;

}
void constructor( int filllevel,int size)
{   
    //ta.buffer[size];
     ta.bufferlen=size;  
     ta.minlevel=filllevel;
}

void randomgenerate()
{
int i;
    for(i=0;i<ta.bufferlen;i++)
    {
                int myFile = open("/dev/random", O_RDONLY);             
                read(myFile, &ta.r, sizeof(ta.r)) ;

                put_buffer(i);
                close(myFile);

    }
}
void put_buffer(int ele)
{


ta.buffer = realloc(ta.buffer, sizeof(uint32_t));
sprintf(ta.buffer, "%zu", ta.r);
ta.offset += sizeof(uint32_t);
ta.queue[ele]=ta.buffer;
printf("%d\t%s\n",ele,ta.queue[ele]);

}
void printbuf()
{
int k;

    for(k=0;k<ta.bufferlen;k++)
    {
    printf("%s\n",ta.queue[k]);
    }
}
9
  • Without reading your code, I'm going to bet that all elements of buffer point to the same storage location. Commented May 11, 2015 at 2:55
  • Did the program compile? I get an error: 64:33: error: invalid conversion from 'void*' to 'char*' Commented May 11, 2015 at 3:01
  • 1
    You may wish to review your use of realloc() and consider whether a different function might be better suited for what you want. Commented May 11, 2015 at 3:07
  • 1
    You need to allocate space for each ta.queue[ele] using simple malloc(). Commented May 11, 2015 at 3:10
  • 1
    Why not malloc it during your "constructor" and then just memset to zero? You're currently not "reallocating" anything, you're just resizing the buffer to the size it is currently. Commented May 11, 2015 at 3:10

2 Answers 2

1

You're misusing realloc(). Your ta.bufferis always pointing to the same ta.buffer because it's already equal to sizeof (uint32_t). So when you do the assignment ta.queue[ele] = ta.buffer, every ta.queue pointer is the same value.

What you need is malloc().

EDIT

You should be using malloc() for each call to put_buffer(), like this:

void put_buffer (int ele)
{
    /* There are 10 decimal digits (characters) in a 32-bit unsigned integer, + 1 for the null terminator */
    ta.buffer = malloc ((sizeof *ta.buffer) * 11); 
    /* Print the string representation to the newly-allocated buffer */
    sprintf(ta.buffer, "%u", ta.r);
    /* I'm not sure what this is for so I'll leave it alone */
    ta.offset += sizeof (uint32_t);
    ta.queue[ele] = ta.buffer;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I've expanded my answer to include example usage. Your size was not correct. You're making space for the string representation of the 32-bit unsigned integer in ta.buffer so you need to take into account how many characters you need. A size of 3 is not enough characters to fit the string "4294967295".
1

Not only are you using realloc with the same size every time, the amount of memory you are allocating is not right.

 ta.buffer = realloc(ta.buffer, sizeof(uint32_t));
 sprintf(ta.buffer, "%zu", ta.r);

sizeof(uint32_t) will be 4. That is not enough memory to store an object of type uint32_t in string form.

Your program is subject to undefined behavior.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.