M
maclab2
Hello,
I have a question about 'memcpy' function,
This is a text code:
-[begin]-------------------------------------------
#include <string.h>
#include <stdio.h>
int test(void * ptr, size_t size)
{
static char const txt [] = "abcdefghijklmn";
int result = (!ptr && size) || size > sizeof(txt);
if (!result)
{
memcpy(ptr, &txt, size);
}
return result;
}
int main(void)
{
char arr1 [3];
char arr2 [7];
printf("a=%d\n", test(arr1, sizeof(arr1)));
printf("b=%d\n", test(arr2, sizeof(arr2)));
printf("c=%d\n", test(NULL, 0));
return 0;
}
-[end]--------------------------------------------
The code works correctly, but in one case ['c'],
the memcpy gets a arguments: (NULL, PTR, 0).
Is this undefined behaviour ?
Regards
I have a question about 'memcpy' function,
This is a text code:
-[begin]-------------------------------------------
#include <string.h>
#include <stdio.h>
int test(void * ptr, size_t size)
{
static char const txt [] = "abcdefghijklmn";
int result = (!ptr && size) || size > sizeof(txt);
if (!result)
{
memcpy(ptr, &txt, size);
}
return result;
}
int main(void)
{
char arr1 [3];
char arr2 [7];
printf("a=%d\n", test(arr1, sizeof(arr1)));
printf("b=%d\n", test(arr2, sizeof(arr2)));
printf("c=%d\n", test(NULL, 0));
return 0;
}
-[end]--------------------------------------------
The code works correctly, but in one case ['c'],
the memcpy gets a arguments: (NULL, PTR, 0).
Is this undefined behaviour ?
Regards