Create Your Own memcpy() Memory Copy Function | C Programming Example

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Thank you Kevin for making character casting easy to understand and why we need it to use it.

fifaham
Автор

I have just discovered your channel and your videos are super helpful, super easy to follow and super amazing.
please keep making videos about computer programming and C language.
✨✨✨

mohammadahmedragab
Автор

shouldn't you check if dest is also not null ?

soniablanche
Автор

Congrats on 2k!!
Sir, can u please do a video about boolean functions use - cases in C.
I see in ur videos that u use a lot of boolean functions

mongraal
Автор

Why do you hvae to cast the dest and src? Isn't it enough just to assign the pointer to the first element to the new char pointer?

Seeker-gwvv
Автор

Hi sir in this video I have noticed something wired .
My question is it correct to check void *dest with Null in the if condition without type casting to another one ...

ramakrishna
Автор

I just subscribed, your videos are so helpful, great explanation straight to the point, I'm in college and we use C . anyway ur channel is great keep up sir

mohamedelamine
Автор

After copying, is it safe to free the source pointer?

Amitkumar-dvkk
Автор

Thank you so much for this video. I have a question about casting. I've seen many examples where unsigned char* was used instead of char*. Why is it that a lot of people say that it's a lot better to use unsigned char*?

yasmineznatni
Автор

why we couldn't casting src and dst pointer from void* to char* and had to create a 2 local pointers : char_dest and char_src ?

cenarvd
Автор

You discuss so well that I could understand it even watching only once. I was hesitant at first because I haven't used memcpy alongside with structs. What IDE are you using btw?

marbles
Автор

hi professor could you explain why we cast to char pointer, but it works on all types like integer arrays wich in this case point to integer not a char

ast
Автор

Why use memcopy with structs when one can do student2 = student1; ???

AlessioSangalli
Автор

thank you man, can you make another vedio about the defrence between memmove and memcpy

ouaammoumouad
Автор

it's really awesome explanation and because of you i managed to make my own memcpy
void *ft_memcpy(void *dst, const void *src, size_t n)
{
size_t i;
char *char_dst;
char *char_src;

char_dst = (char *)dst;
char_src = (char *)src;
if (dst == NULL && src == NULL)
return (NULL);
i = 0;
while (i < n)
{
char_dst[i] = char_src[i];
i++;
}
return (dst);
}

SilentxMod
Автор

Really interesting example, but it could be confusing for a beginner because in your function you do operations on char_dest and you finally return dest
I'm not sure everyone understand why it works like that :)

ChockAir
Автор

I'm not sure about c but with c++ i think it's better to make to second argument a const in case we wanna pass a string literal :

void *stdmemcpy(void *dest, const void*buffer, size_t count){
unsigned char* casteddest = (unsigned char*)dest ;
unsigned char* castedbuffer = (unsigned char*)buffer ;
while(dest !=nullptr && buffer !=nullptr && count){
*casteddest = *castedbuffer ;
++casteddest, ++castedbuffer, --count ;
}
return dest ;
}

also i'm sorry i'm asking too much questions lately but i'm curious why u did not check if the second argument is a NULL and also i'm really confuse about the meaning of buffer in c/c++
or in programming in general i mean i know that the buffer is a memory area that stores data while it's being moved right ? but why we call an array that is allocated in the stack or in the heap a buffer ?

thank u for great videos 😉

justcurious
Автор

I did implement the memcpy() function, And i did try to run some test but my out put are not the same. I was trying to modify the strings which is my destination and the source.
check out the code;

#include "libft.h"
void *ft_memcpy(void *dest, const char *src, size_t n)
{
unsigned int i = 0;
unsigned char *dst = (unsigned char *)dest;
unsigned char *s = (unsigned char *)src;
while (s[i] != '\0' && i < n)
{

dst[i] = s[i];
i++;
}
return dest;
}
int main()
{
char dest[] = "Amone Patrick";
ft_memcpy(&dest[7], &dest[6], 5);
printf("This is the output for modified: %s\n", dest);
memcpy(dest + 7, &dest[6], 5);
printf("This is the output for Built-in: %s\n", dest);
}
output: This is the output for modified: Amone
This is the output for built-in: Amone PPatrik

jamieamone