Question

I have used MapVirtualFile to map a file under Window using C++ VS2010. The void is

  void*                   m_pVoiceData;

I would now like to fill a structure with the data.

I have been using

void clsMapping::FeedPitchmarksFromMap(udtPitchmarks &uAudioInfo,unsigned long int uBytePos)
{

    unsigned long int iCountPM;
    memcpy(&iCountPM, &((char*)(m_pVoiceData))[uBytePos],sizeof(int));

    uAudioInfo.Pitchmarks.resize(iCountPM);

    for (unsigned long int i=0;i<iCountPM;i++)
    {
        iBytePos+=sizeof(int);
        unsigned long int iByteStart;
        memcpy(&iByteStart, &((char*)(m_pVoiceData))[iBytePos],4);

        iBytePos+=sizeof(int);
        unsigned long int iByteCount;
        memcpy(&iByteCount, &((char*)(m_pVoiceData))[iBytePos],4);

        iBytePos+=sizeof(int);
        unsigned int iF0;
        memcpy(&iF0, &((char*)(m_pVoiceData))[iBytePos],4);

        uAudioInfo.Pitchmarks[i].ByteStart =iByteStart;
        uAudioInfo.Pitchmarks[i].ByteCount =iByteCount;
        uAudioInfo.Pitchmarks[i].F0 =iF0;
    }
}

As one can see, I am currently using memcpy to get the data. Is there a faster way to do "get my data over"?

Was it helpful?

Solution

The "fastest" would probably be simple assignment:

unsigned long iCountPM = 
    *reinterpret_cast<unsigned long*>(reinterpret_cast<char*>(m_pVoiceData) + uBytePos);

Can be used instead for all those memcpy calls in the function.

OTHER TIPS

Assuming that your memory-mapped file has exactly the same alignment and padding as the type of your uAudioInfo.Pitchmarks[0] (presumably a struct of some kind), you can just use one single memcpy to copy the whole lot, rather than memcpying individual fields like you're currently doing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top