memccpy return lower memory address than the src starting address

716 views Asked by At

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

There are 1 answers

1
Eric Tsui On BEST ANSWER

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