memccpy return lower memory address than the src starting address

有些话、适合烂在心里 提交于 2019-12-02 20:29:41

问题


I've got a school project where I have to recode the memccpy() function.

I use 2 programs to check if my code works properly. The first is a small program with only a main. The second program is developped by another student (can be found here, if you want to see it)

With my program, there is no problem, both my memccpy and the original function return the right pointer on the right character from the dest pointer. But with the second program, the original function return a lower pointer adress than the dest pointer starting adress, for example:

  • Starting address of the src value: 0x7fff712edc40
  • Memccpy return pointer address: 0x712edc44
  • My memccpy function return pointer: 0x7fff712edc44

So, as someone compile on his computer and my code return the right adress, it must come from the second program.

Does somebody know what can cause this kind of behavior?

  • I tried to search on Google but the answer was not very helpful.
  • I read the man multiple times ^^' (not more helpful).
  • I read that memccpy have an undefined behavior when memory overlap, but they don't overlap.

Here is my ft_memccpy() function:

void    *ft_memccpy(void *str_dest, const void *str_src, int c, size_t n)
{
    unsigned int    i;
    char            *dest;
    char            *src;
    char            *ptr;

    dest = (char *)str_dest;
    src = (char *)str_src;
    i = 0;
    ptr = 0;
    while (i < n && ptr == 0)
    {
        dest[i] = src[i];
        if (src[i] == ((char)c))
            ptr = dest + i + 1;
        i++;
    }
    return (ptr);
}

Edit: I edited because i got downvote and many user didn't understood my question. So i thought that it was not clear enough, i hope this edit will ^^'.


回答1:


That's probably the problem of the 64bit pointer and 32bit pointer.

Your ft_memccpy would return a 64 bit pointer, so it outputs

0x7fff712edc44

While the system memccpy returned a 32bit's

0x712edc44


来源:https://stackoverflow.com/questions/31094133/memccpy-return-lower-memory-address-than-the-src-starting-address

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!