Solved

Copy from dynamic array to static array

Posted on 2013-05-25
2
504 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 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 34

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

690 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