Pointer destruction = program crash

unsigned int *pMessageLength, MessageLength;
char *pszParsePos;

...
//DATA into pszParsePos
...

printf("\nMessage Length\nb1: %d\nb2: %d\nb3: %d\nb4: %d\n",
    pszParsePos[1],pszParsePos[2],pszParsePos[3],pszParsePos[4]);

pMessageLength= (unsigned int *)&pszParsePos[1];

MessageLength = *((unsigned int *)&pszParsePos[1]);

//Program Dies

Output:

Message length
b1: 0
b2: 0
b3: 0
b4: 1

I do not understand why this harms my program. Can someone explain this, or at least suggest an alternative method that won't fail?

Thank you for your time!

+3
source share
2 answers

, . , , , int , , *pszParsePos , int ( , , , , malloc), , &pszParsePos[1] .

MessageLength, -

MessageLength = (pszParsePos[1] << 24) | (pszParsePos[2] << 16) | (pszParsePos[3] << 8) | pszParsePos[4]

( , , ). , , , , .

+3

, :

, Blackfin Processor. -, , Blackfin , . , 32- / , 4 .

C [0], [1]. 4- char [3].

4- char, :

  • , 1.
  • DWORD 32- .

, 4- char 4- , , +1 , .

+1
source

Source: https://habr.com/ru/post/1720307/


All Articles