Solved

Copy from dynamic array to static array

Posted on 2013-05-25
2
487 Views
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.
0
Comment
Question by:bachra04
2 Comments
 
LVL 16

Accepted Solution

by:
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.
0
 
LVL 33

Assisted Solution

by:sarabande
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();

Sara
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sameEnds challenge 3 133
Compile GLUT with Visual Studio 2015 1 107
gdb doesn't stop on breakpoint 2 45
Issues with C++ Class 19 77
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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

948 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now