?
Solved

Copy from dynamic array to static array

Posted on 2013-05-25
2
Medium Priority
?
511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
George Tokas earned 1000 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 35

Assisted Solution

by:sarabande
sarabande earned 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
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.
Suggested Courses

765 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