Doubts regarding the use of memset and memcpy functions

Asked

Viewed 2,008 times

3

I’ve been sifting through some codes on the internet and at a certain point I came across the functions memset and memcpy. I was able to understand superficially the functioning of the functions mentioned, because all the sources that provide some information or example code in relation to these two functions were somewhat similar and had very basic information. However, I ended up formulating the following doubts:

  • It’s wrong to use memcpy and memset with dice don’t char?
  • What is the void pointer utility returned by the function memset?
  • What is the void pointer utility returned by the function memcpy?
  • memcpy copies only the data or also copies the address of the copied block?

Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 10
#define NEW_SIZE 20

int main(void){

    int *numbers=(int*)calloc(SIZE, sizeof(int));

    for(unsigned int i=0; i<SIZE; i++){

        numbers[i]=i+1;
    }

    for(unsigned int i=0; i<SIZE; i++){

        printf("numbers[%d]=%d\n", i, numbers[i]);
    }

    printf("\n==================\n\n");

    int *aux=(int*)realloc(numbers, NEW_SIZE*sizeof(int));

    if(aux!=NULL){

        memcpy(numbers, aux, NEW_SIZE*sizeof(int)); //isso é a mesma coisa que 'numbers=aux;' ???

        for(unsigned int i=SIZE; i<NEW_SIZE; i++){

            numbers[i]=i+1;
        }

        for(unsigned int i=0; i<NEW_SIZE; i++){

            printf("numbers[%d]=%d\n", i, numbers[i]);
        }
    }

    free(numbers);

    return 0;
}

1 answer

3


It’s wrong to use memcpy and memset with data not char?

  • memcpy can be applied in any type
  • memset is actually wrong something other than the size of char. In this case it will not give the desired result, since the value is assigned byte to byte and interpreted as unsigned char.

    Of documentation:

    (...) specified value (Interpreted as an unsigned char).

    But it is relatively easy to demonstrate that it goes wrong when used with other types. Consider the following example, which tries to assign the value 5 to two houses of an array of integers with memset:

    int arr[2];
    memset(arr, 5, sizeof(int) * 2);
    printf("%d %d", arr[0], arr[1]);
    

    Here you might be thinking that the result would be 5 5 when it’s actually:

    84215045 84215045
    

    Check it out at Ideone

    But how was that result generated ?

    It turns out that the memset assigned 5 each byte of the two integers. Then one of these integers in its representation in memory was like this:

    -----------------
    | 5 | 5 | 5 | 5 |
    -----------------
    

    That expanded to binary would be like this:

    ---------------------------------------------
    | 00000101 | 00000101 | 00000101 | 00000101 |
    ---------------------------------------------
    

    What form the value 00000101000001010000010100000101 in torque, corresponding to 84215045.

    When it was meant to be

    -----------------
    |       5       |
    -----------------
    

    That expanding also to binary would already look like this (assuming the format little-endian):

    ---------------------------------------------
    | 00000000 | 00000000 | 00000000 | 00000101 |
    ---------------------------------------------
    

    This is the problem of using different types in memset. Interestingly, if I wanted to assign 0 would work properly because 0 is always 0 regardless of the amount of bits we are considering.

What is the void pointer utility returned by the function memset?

Very little utility has this pointer, but can for example chain function calls:

strcpy(memset(ptr, 0, 200), "texto");

Something that wouldn’t be possible to do if the kind of feedback was void. Still it’s a very particular and certainly unusual case.

What is the void pointer utility returned by the function memcpy?

Same answer as the previous question, chain calls.

memcpy copies only the data or also copies the address of the copied block?

memcpy copies only the data that is in the address passed as the second parameter, called source, to the destination pointer called destination.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.