tags:

views:

54

answers:

1

Hello all. I'm posting my first question here after having viewed many useful exchanges by others; it's very exciting! I'm thinking that this question will be fairly straightforward to answer, but I hope that someone can shed some light on it since its vexing me.

I have a function that accepts an array of bytes passed in as a byte pointer, multiple layers of the program lower. The variable is a member of the class defined as

m_GpsInputBuf[GpsInputBuflen];

The function in question receives a pointer to that array as shown in the code below. The function just shifts the bytes in the array to the left when the checksum for the packet is not validated and another sync character for the data packet is located within the body of the packet. All questions as to the purpose of the function aside though, the point that I'm interested in is the copying of the temp array to the parameter byte array pointer.

The copy to the pPacketbuf variable takes place just as I'd hoped, but when the function terminates, the pPacketbuf variable that exists at the calling level of the ScanAndShift() function is unaffected. This is related to the argument to ScanAndShift() being a copy of the pointer variable, I believe, but I'm too close to the problem to understand it right now. Any insight? Code below:

int CFlirPumpDlg::ScanAndShift(BYTE *pPacketbuf)
{
int idx3 = 0;
BYTE tempGpsInputBuf[GpsInputBuflen];

for(int idx = 1; idx < GpsInputBuflen; idx++)
{
    if(pPacketbuf[idx] == CFlirPumpApp::GPS_Start)
    {
        idx3 = idx;

        for(int idx2 = 0; idx2 < (GpsInputBuflen - idx); idx2++)
        {
            tempGpsInputBuf[idx2] = pPacketbuf[idx3++];
        }

        pPacketbuf = (BYTE *)&tempGpsInputBuf;
        return (GpsInputBuflen - idx);
    }
}

return 0;
}
+2  A: 

There are two way to go here.

1) You allocate a new buffer and populate that with the bytes. For that you need to change your function to take a double pointer like this:

int CFlirPumpDlg::ScanAndShift(BYTE **pPacketbuf)

But then, you have to allocate a new buffer instead of declaring it. Something like this:

BYTE* tempGpsInputBuf = malloc(...);

And you have to change this:

pPacketbuf = (BYTE *)&tempGpsInputBuf;

Into this:

pPacketbuf = tempGpsInputBuf;

Also all you indexing in pPacketbuf should be done like this:

*pPacketbuf[index]

And you have to think about freeing the 'old' buffer.

2) You can copy the bytes from tempGpsInputBuf back to pPacketbuf with a memcpy.

Or you can go with the 3. option and that is to change your algorithm to do all the work in the pPacketbuf. I don't know if this is possible or not.

Personally i would go with the 2. or 3. options.

Martin Ingvar Kofoed Jensen
That's great! I just talked to a programmer here and he suggested the memcpy approach, so a little verification was just what I needed. Thanks a lot!
Rich Hoffman
Glad I could help :) I can see that you are a new user at stackoverflow. Not that you should vote on my answer, but it might help other people if you vote/accept answers ;)
Martin Ingvar Kofoed Jensen
I will do so as soon as I get 15 rep and can, in fact, do so. I DID just define your answer as the accepted answer.
Rich Hoffman