为什么指针分配超出范围?

时间:2019-09-18 17:19:43

标签: c pointers scope

我知道我在这里确实缺少一些根本的东西,但是当您遇到困难时,您就会陷入困境。 我写了一个小函数来反转c字符串的字节序。目的是第一个参数可以是空指针,也可以是作废的已分配指针,无论初始状态如何,这将返回已分配缓冲区中的主机顺序值。一切都会相应地起作用,直到功能超出范围为止。内存仍然存在,但指针为空。为什么会这样?

CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

    void toNetworkOrder(char* networkOrder, const char* hostOrder)
    {
        if(!networkOrder)
        {
            networkOrder = (char*)malloc(strlen(hostOrder) + 1);
        }
        else if(strlen(networkOrder) < strlen(hostOrder))
        {
            free(networkOrder);
            networkOrder = (char*)malloc(strlen(hostOrder) + 1);
        }
        memset(networkOrder, 0, strlen(hostOrder) + 1);

        const char *hPtr = hostOrder;
        char *nPtr = networkOrder;
        for(; hPtr < (hostOrder+strlen(hostOrder)); hPtr++, nPtr++)
        {
            *nPtr = (((*hPtr & 0x0F) << 4) | ((*hPtr & 0xF0) >> 4));
        }
    }

int main()
{
    const char* str1 = "Test Message\0";
    char* str2 = NULL;
    printf("Input string: 0x%x\t%s\n", str1, str1);
    printf("Input string: 0x%x\t%s\n", str2, str2);
    toNetworkOrder(str2, str1);
    printf("Input string: 0x%x\t%s\n", str1, str1);
    printf("Input string: 0x%x\t%s\n", str2, str2);

    return 0;
}

调试

(gdb) list                                                                                                                 
27              char *nPtr = networkOrder;                                                                                 
28              for(; hPtr < (hostOrder+strlen(hostOrder)); hPtr++, nPtr++)                                                
29              {                                                                                                          
30                  *nPtr = (((*hPtr & 0x0F) << 4) | ((*hPtr & 0xF0) >> 4));                                               
31              }                                                                                                          
32          }                                                                                                              
33                                                                                                                         
34      int main()                                                                                                         
35      {                                                                                                                  
36          const char* str1 = "Test Message\0";                                                                           
(gdb) break 32                                                                                                             
Note: breakpoint 1 also set at pc 0x400764.                                                                                
Breakpoint 2 at 0x400764: file main.c, line 32.                                                                            
(gdb) run                                                                                                                  
Starting program: /home/a.out                                                                                              
Input string: 0x400894  Test Message                                                                                       
Input string: 0x0       (null)                                                                                             

Breakpoint 1, toNetworkOrder (networkOrder=0x602010 "EV7G\002\324V77\026vV",                                               
    hostOrder=0x400894 "Test Message") at main.c:32                                                                        
32          }                                                                                                              
(gdb) x/13bx hostOrder                                                                                                     
0x400894:       0x54    0x65    0x73    0x74    0x20    0x4d    0x65    0x73                                               
0x40089c:       0x73    0x61    0x67    0x65    0x00                                                                       
(gdb) x/13bx networkOrder                                                                                                  
0x602010:       0x45    0x56    0x37    0x47    0x02    0xd4    0x56    0x37                                               
0x602018:       0x37    0x16    0x76    0x56    0x00
(gdb) n
main () at main.c:41                                                                                                       
41          printf("Input string: 0x%x\t%s\n", str1, str1);
(gdb) x/13bx networkOrder
No symbol "networkOrder" in current context.
(gdb) x/13bx 0x602010                                                                                                      
0x602010:       0x45    0x56    0x37    0x47    0x02    0xd4    0x56    0x37                                               
0x602018:       0x37    0x16    0x76    0x56    0x00

0 个答案:

没有答案