Solved

Copy from dynamic array to static array

Posted on 2013-05-25
2
480 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
Comment Utility
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 32

Assisted Solution

by:sarabande
sarabande earned 250 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

762 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

11 Experts available now in Live!

Get 1:1 Help Now