Reputation: 81
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)
{
Upvotes: 1
Views: 3091
Reputation: 133619
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:
Upvotes: 1
Reputation: 23238
Really need more context to assist here, there's nothing obviously untoward from what you've said.
Basics:
Upvotes: 2