[英]How to copy char* to char* array in C
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. 我正在尝试生成应存储在缓冲区数组中的uint_32随机数,但是无论如何,我的代码每次都只存储最后一个值。
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]);
}
}
You're misusing realloc()
. 您正在滥用
realloc()
。 Your ta.buffer
is always pointing to the same ta.buffer
because it's already equal to sizeof (uint32_t)
. 您的
ta.buffer
始终指向相同的ta.buffer
因为它已经等于sizeof (uint32_t)
。 So when you do the assignment ta.queue[ele] = ta.buffer
, every ta.queue
pointer is the same value. 因此,当您执行
ta.queue[ele] = ta.buffer
分配时,每个ta.queue
指针都是相同的值。
What you need is malloc()
. 您需要的是
malloc()
。
EDIT 编辑
You should be using malloc()
for each call to put_buffer()
, like this: 对于每个对
put_buffer()
调用, put_buffer()
使用malloc()
put_buffer()
,如下所示:
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;
}
Not only are you using realloc
with the same size every time, the amount of memory you are allocating is not right. 不仅您每次都使用相同大小的
realloc
,而且分配的内存量也不正确。
ta.buffer = realloc(ta.buffer, sizeof(uint32_t));
sprintf(ta.buffer, "%zu", ta.r);
sizeof(uint32_t)
will be 4
. sizeof(uint32_t)
将为4
。 That is not enough memory to store an object of type uint32_t
in string form. 这不足以以字符串形式存储
uint32_t
类型的对象的内存。
Your program is subject to undefined behavior. 您的程序受到未定义的行为的约束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.