Copy from dynamic array to static array

Posted on 2013-05-25
Last Modified: 2013-05-30
I have two question:

- First question:

I have an array statically allocated:

uint8_t dstArray[MAX_LENGTH];

uint8_t* pSrcArray = new uint8_t[100]; // 100 < MAX_LENGTH

// copied contents :

memcpy (dstArray, pSrcArray,100);

Is it safe to copy contents like that ? is there any more elegant way of doing that?

Second question:

I have String MyString that I have received ;

I need to use this method with an API that takes uint8_t* as a parameter:

myAPI( uint8_t* pArray);

Is it safe to cast the const char* to uint8_t ?

myAPI((uint8_t*)MyString .c_str());

Is there a better way of doing things.
Question by:bachra04
LVL 16

Accepted Solution

George Tokas earned 250 total points
ID: 39197863
1. Since your memory buffers are not overlaping each other memcpy is safe. A safer (as documents said) is memmove but in time critical applications you have to measure timings.A good thing though is to initialize the contents of the static memory to something (zero or 0xff) to be safe from garbage data especially when you are not sure about memory leaks or bugs.

2. uint8_t is a rough analogue to unsigned char, and I have made this cast many times without a problem. Doesn't mean that I am right though, just mentioning that the cast works without a problem in some applications I worked using ffmpeg..

George Tokas.
LVL 34

Assisted Solution

sarabande earned 250 total points
ID: 39198484
to 1)
a memcpy is safe but you always should do like
if (pSrcArray != NULL)
    memcpy (dstArray, pSrcArray,100*sizeof(uint8_t));

Open in new window

with a type like uint8_t it is rather sure that it doesn't exceed size of char but in case of a portation such types often are a subject of change, and by always using sizeof you make your code more stable.

to 2)
the c_str() would return a pointer to const memory. if you cast it to non-const any further function doesn't know that it was const and may do a wrong operation, for exmple 'delete'
on it. i always would copy it to non-const memory if the pointer was not used only locally. for locally used purposes you could do
if (!MyString.empty())
   uint8_t * p = (uint8_t *)&MyString[0];

Open in new window

different to your code the Mystring[0] was writeable memory. but be aware to not writing beyond string size, which is MyString.size();


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question