CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Allocating and copying memory in one go...

    This code will copy the contents of src to dest

    Code:
    void SomeFunc(void *src, size_t size)
    {
    void* dest;
    
    	if (memcpy (dest = malloc (size), src, size))
    	{
    		// Do something
    	}
    }
    although it involves separate calls to memcpy() and malloc(). I just wondered if there's any single function which combines both jobs - e.g.

    Code:
    void SomeFunc(void *src, size_t size)
    {
    void* dest;
    
    	if (dest = some_copying_function (src, size))
    	{
    		// Do something
    	}
    }
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Allocating and copying memory in one go...

    Not that I'm aware of as part of the standard library. It's easy to write your own though.

    Note that in the code posted in post #1 there is an issue. If malloc() fails it returns NULL so memcpy() will try to copy data to location 0 which will cause an exception. The result of malloc() should be tested before being used.

    Consider
    Code:
    void* copymem(const void* src, size_t sz)
    {
    	void* newmem = malloc(sz);
    	if (newmem == NULL)
    		return NULL;
    
    	return memcpy(newmem, src, sz);
    }
    
    void SomeFunc(const void *src, size_t size)
    {
    void* dest = copymem(src, size);
    
    	if (dest)
    	{
    		// Do something
                    free(dest)
    	}
    }
    Note that the caller of copymem() is responsible for freeing memory using free().

    Are you writing c rather than c++?
    Last edited by 2kaud; January 4th, 2017 at 06:50 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Allocating and copying memory in one go...

    Hi 2kaud - ultimately it'll be C++ but I was just curious. It occurred to me that a standard library function to do both operations would have been quite useful. I just wondered if such a function existed but I'd managed to miss it somehow !
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Allocating and copying memory in one go...

    ultimately it'll be C++
    Then I would write it as a RAII class.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Allocating and copying memory in one go...

    John, did you consider realloc?

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Allocating and copying memory in one go...

    Hi Richard - as I understand it, realloc() needs to be given the address of a previously allocated memory block - so it's essentially still a 2-stage process (or can you pass NULL for that initial memblock parameter ?)
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Allocating and copying memory in one go...

    If you pass NULL for the memblock parameter it behaves the same as malloc(). It still doesn't provide the required copy facility. If realloc() can provide the extra required memory without moving (ie contiguous with the currently allocated memory), it will do so. Otherwise realloc() allocates a new block of the required size, copies from the old to the new, frees the old memory and returns a pointer to the newly allocated memory. You still need to copy from src to the newly allocated memory.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured