Home > Back-end >  Copy structure to buffer
Copy structure to buffer

Time:07-30

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <math.h>
#include <time.h>
#define RAND(lower,upper) (rand()%(upper-lower 1)) lower
int power(int base, int exp)
{
    int result=1;
    while (exp != 0) 
    {
        result *= base;
        --exp;
        }
        return result;
}
int isKthBitSet(int n, int k)//from right, 1<=k<=n
{
     int new_num = n >> (k - 1);
    // if it results to '1' then bit is set,
    // else it results to '0' bit is unset
    return (new_num & 1);
}
struct Astructure{
    uint16_t bitmap;        
    uint32_t a;          
    uint32_t b;         
    char str[10];           
    uint16_t d;     
}__attribute__((packed));
int main()
{
    struct Astructure abitmap;
    abitmap.bitmap = 0;
    char buffer[(sizeof(struct Astructure))];   
    abitmap.a = 52;
    abitmap.b = 16;
    char c[10]={"ramya"};
    strcpy(abitmap.str, c);
    abitmap.d = 59;
    char ch;
    srand(time(0));
    for(uint8_t position =1;position<5;position  )
    {
        int random10 = RAND(0,1);
        if(random10==1)
        {
            int value = power(2,position-1);
            abitmap.bitmap = abitmap.bitmap | value;
        }
    }
    memcpy(buffer, (char *)&abitmap.bitmap, sizeof(abitmap.bitmap) 1);
    
    
    uint8_t temp4 = isKthBitSet(abitmap.bitmap,4);
    uint8_t temp3 = isKthBitSet(abitmap.bitmap,3);
    uint8_t temp2 = isKthBitSet(abitmap.bitmap,2);
    uint8_t temp1 = isKthBitSet(abitmap.bitmap,1);
    uint8_t previousLength = sizeof(abitmap.bitmap);
    if(temp4){
        //add a to buffer
        memcpy(buffer   previousLength, (void *)&abitmap.a, sizeof(abitmap.a) 1);
        previousLength  = sizeof(abitmap.a);
    }
    if(temp3){
        //add b to buffer
        memcpy(buffer   previousLength, (void *)&abitmap.b, sizeof(abitmap.b) 1);
        previousLength  = sizeof(abitmap.b);
    }
    if(temp2){
        //add c to buffer
        memcpy(buffer   previousLength, (void *)&abitmap.str, sizeof(abitmap.str) 1);
        previousLength  = sizeof(abitmap.str);
    }
    if(temp1){
        //add d to buffer
        memcpy(buffer   previousLength, (char *)&abitmap.d, sizeof(abitmap.d) 1);
        previousLength  = sizeof(abitmap.d);
    }
    
    
    //SHOW BUFFER
    previousLength = sizeof(abitmap.bitmap);
    uint32_t a;
    uint32_t b;
    char str[10];
    uint16_t d;
    if(temp4){
        memcpy(&a , (void *) buffer previousLength , sizeof(abitmap.a) 1);//
        printf("a = %d\t",a);
        previousLength  = sizeof(a);
        
    }
    if(temp3){
        memcpy(&b , (void *) buffer previousLength , sizeof(abitmap.b) 1);
        printf("b = %d\t",b);
        previousLength  = sizeof(b);
    }
    if(temp2){
        memcpy(str , (void *) buffer previousLength , sizeof(abitmap.str) 1);//memccpy
        printf("string = %s\t",str);
        previousLength  = sizeof(str);
        
    }
    if(temp1){
        memcpy(&d , (void *) buffer previousLength , sizeof(abitmap.d) 1);
        printf("d = %d\t",d);
        previousLength  = sizeof(d);
        
    }
    printf("\n");
}

I trying to copy some variables to my buffer. The variables I want to add in the buffer is picked at random. Here I gave the size of my buffer before hand to the size of my structure. But as per my program I may not be adding all my variables in the buffer. So its a waste.

So How am I supposed to declare my buffer here.

if my replace this

char buffer[(sizeof(struct Astructure))];

to

char *buffer;

this

I get below error.

a = 52  string = ramya  
*** stack smashing detected ***: terminated
Aborted (core dumped)

I dont want to waste the size of my buffer in here, giving the whole size of structure. so what do i do.

CodePudding user response:

If you want to allocate only the required memory:

typedef struct
{
    size_t length;
    unsigned char data[];
}buff_t;


buff_t *addtobuffer(buff_t *buff, const void *data, size_t size)
{
    size_t pos = buff ? buff -> length : 0;

    buff = realloc(buff, pos   size   sizeof(*buff));
    if(buff) 
    {
        memcpy(buff -> data   pos, data, size);
        buff -> length = pos   size;
    }
    return buff;
}

and example usage:

    buff_t *buffer = NULL;  

/* ... */


    if(temp4)
    {
        buff_t *tmp = addtobuffer(buffer, &abitmap.a, sizeof(abitmap.a) 1);
        if(!tmp) { /* error handling */}
        buffer = tmp;
        /*...*/
    }
  • Related