views:

397

answers:

2

I have a pointer to my char array like this.

    unsigned char *recvBuf;
    recvBuf = (unsigned char *)malloc(sizeof(char)*RECVBUF);

I then pass that to a function defined as

void setHeader(MyHeader *myHeader, unsigned char *buffer)

Which copies the first 12 bytes into a struct

Then I try and pass it to another function later on

Record readRecord(unsigned char *buffer, int *arrayPosition, int buffsize)

But the program always becomes unresponsive when I try and access any part of the array using [], in the exact same way it was used in the previous functiom. And i'm also fine to access it like that before i pass it to the function.

Ideas would be much appreciated.

Thanks

EDIT:

Pointers are the same.

This is inside the second function

I added this code to the top and it works just fine

int k = *arrayPosition;
 printf("%p \n", buffer);

 printf("%c \n", buffer[k]);

This is the original code, the program becomes unresponsive adfter i = *arrayposition...

int i, j;
Record *myRecord;
Resource *myResource;
unsigned char name[255];
unsigned char data[50];
unsigned int offset;

i = *arrayPosition;

while(buffer[i] != '\0')
{
 if((buffer[i] & 0xC000) == 0xC000)
 {
+2  A: 

Really need more context to assist here, there's nothing obviously untoward from what you've said.

Basics:

  1. Check that you haven't indexed off the end of your memory block.
  2. Check that you're not passing &recvBuf where you mean to pass recvBuf? A compiler should catch this, but you'd trash your stack if you did this, maybe using a cast.
Don Neufeld
+1  A: 

Input is not enough to be sure of the problem that makes the pointer go bad but a good catch would be to check with gdb if the pointer changes between the function call you descripted.

Do you modify any char of the array? You should dereference the pointer with * in that case.

If you cannot use the pointer after the call causes can be:

  1. it is changed erroneously inside the function
  2. it is a pointer to an allocated value in the stack that is deallocated implicitly somewhere (but it sounds strange)
Jack