-1

If I am not correct, the codes following are used to copy an array of bytes to a position of memory in C#:

byte[] byteName = Encoding.ASCII.GetBytes("Hello There");

int positionMemory = getPosition();

Marshal.Copy(byteName, 0, new IntPtr(positionMemory), byteName.length);

How can I achieve this in native C++?

6
  • 2
    strcpy/strncpy. These are very basic functions that any C book will tell you. Commented May 15, 2012 at 23:20
  • @Pubby I am not C, C++ programmer. I am a C# programmer. I need to convert the code in C# for a C++ project. thanks for pointing that to me. Commented May 15, 2012 at 23:26
  • 2
    I downvoted mostly because I get 'memcpy' as first result when googling "C++ copy bytes". You also should check out std::copy which is more general purpose. Anyway, good luck. Commented May 15, 2012 at 23:29
  • 2
    btw, you don't really need to use Marshal to copy byte array to another byte array. The Array class have all you need. Only use marshaling when you have no other choice. Commented May 15, 2012 at 23:30
  • 1
    @olidev: There's very little reason to copy stuff into random areas of memory in C++, don't do that unless programming microcontrollers. Commented May 15, 2012 at 23:36

2 Answers 2

6

use a pointer and memcpy:

void * memcpy ( void * destination, const void * source, size_t num );

Suppose you want to copy an array A of length n into an array B

memcpy (B, A, n * sizeof(char));

This is more C than C++, the string class have copy capabilities you can use.

  size_t length;
  char buffer[20];
  string str ("Test string...");
  length=str.copy(buffer,6,5);
  buffer[length]='\0';

Here's a more specific sample with a complete code:

#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>

using namespace std;
int main()
{

    string s("Hello World");
    char buffer [255];
    void * p = buffer; // Or void * p = getPosition()
    memcpy(p,s.c_str(),s.length()+1);
    cout << s << endl;
    cout << buffer << endl;
    return 0;
}

let me know if you need more details

Sign up to request clarification or add additional context in comments.

2 Comments

He seems to be wanting to copy a string literal "Hello There" to the location returned by getPosition(). Can you show this more clearly in your samples?
As you already pointed out, copying memory blocks to a random position is not really safe. Didn't want to go there, but since you asked :p
1

memcpy(), memmove(), CopyMemory(), and MoveMemory() can all be used as native equivilents of Marshal.Copy(). As for the position handling, all the .NET code is doing as casting the integer to a pointer, which you can do in C++ as well. The .NET code you showed is equivilent to the following:

std::string byteName = "Hello There"; 
int positionMemory = getPosition(); 
memcpy(reinterpret_cast<void*>(positionMemory), byteName.c_str(), byteName.length()); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.