memcpy not doing its job

This is a discussion on memcpy not doing its job within the C Programming forums, part of the General Programming Boards category; Hi guys! I have a problem with memcpy. I have a memory location with 3 strings, all 0 terminated and ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    68

    memcpy not doing its job

    Hi guys!

    I have a problem with memcpy. I have a memory location with 3 strings, all 0 terminated and want to shorten it to 2 strings.

    Example:
    Code:
    Tree\0House\0Banana\0
    becomes
    House\0Banana\0
    And my code:

    Code:
    	void msgbox(char text[]){MessageBox(NULL,text,"Debug",MB_OK);}
    	char msgbuf[200];
    	memcpy(tmp, startnewstring, newlength);
    	if((worktodo = realloc(worktodo, newlength!=0?newlength:1)) == NULL) break;
    	memcpy(worktodo, &tmp, newlength);
    		sprintf(msgbuf, "newlength: %d, startnewstring: %d, tmp: %s, worktodo: %s", newlength, startnewstring, tmp, worktodo);
    		msgbox(msgbuf);
    tmp = a char array where House\0Banana\0 should be saved temporarily
    startnewstring = the memory location of House\0 (= memory location of Tree\0 + 5)
    newlength = the length in bytes of House\0Banana\0
    worktodo = the array that holds Tree\0House\0Banana\0 and should become House\0Banana\0


    It seems the memory copying into tmp is successful but the copying back into the new worktodo is not.

    That's what I get:
    Code:
    ---------------------------
    Debug
    ---------------------------
    newlength: 13, startnewstring: 13319336, tmp: House, worktodo: =
    ---------------------------
    OK   
    ---------------------------
    Why is there gibberish in worktodo?

    TIA, Hawk
    Last edited by Hawkin; 11-11-2010 at 08:14 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    > memcpy(worktodo, &tmp, newlength);
    Is tmp really a char array, like you assert?

    Because if tmp is a char pointer, then tmp and &tmp are DIFFERENT things.

    You also need to be careful about using realloc. Do not use the same pointer for the result as the one being realloc'ed. If realloc returns NULL, the old memory is still allocated, but your pointer to it is trashed. This is a leak.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    68
    Code:
    char *tmp = malloc(newlength!=0?newlength:1);
    About realloc I know, but thanks. I am too lazy to implement any security in raw code. That will be the task when my snippet works and I will put it into my project.


    Okay, I see now that it works with
    Code:
    char *tmp = malloc(1);
    memcpy(memorylocation, tmp, 1);
    but
    Code:
    char msgbuf[1];
    memcpy(memorylocation, &msgbuf, 1);
    needs the '&'. Where can I read up why?
    Last edited by Hawkin; 11-11-2010 at 09:23 AM.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by Hawkin View Post
    Code:
    char *tmp = malloc(newlength!=0?newlength:1);
    About realloc I know, but thanks. I am too lazy to implement any security in raw code. That will be the task when my snippet works and I will put it into my project.


    Okay, I see now that it works with
    Code:
    char *tmp = malloc(1);
    memcpy(memorylocation, tmp, 1);
    but
    Code:
    char msgbuf[1];
    memcpy(memorylocation, &msgbuf, 1);
    needs the '&'. Where can I read up why?
    You don't need it in the second case either. array names are treated as pointers to their first entry, so it ends up being the same as the first case.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Actually it's memcpy(destination, source, bytes);

    I can't tell from your discussion if you have that backwards or not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. crazy pipe/fork
    By fortune2k in forum C Programming
    Replies: 8
    Last Post: 03-13-2009, 11:28 AM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 10:35 AM
  5. I can't find a job.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 42
    Last Post: 06-29-2003, 08:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21