字符串和指针用C [英] String and pointers in C

查看:120
本文介绍了字符串和指针用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;INT主要(无效)
{
        字符* A =的malloc(5 * sizeof的(炭));
        一个=1;
        自由(一);
}

我有一个关于上述code两个问题:


  1. 为什么code给出了一个无差错(一)当我初始化是这样的:


  

在进行=1


和不给任何错误自由(一)当我初始化是这样的:


  

一个[0] ='1'。


<醇开始=2>

  • 当我初始化与
    一个[0] ='1'
    和使用打印字符串
    的printf(%S,一)为什么我得到的结果。


      

    1


  •   
      没有以'\\ 0',宣布第二个索引?



    解决方案

    与规则的的malloc 的和的免费的是,你必须只传递给的免费,是由一个呼叫分配到的的malloc 的指针(或类似的的realloc 或释放calloc 的)。你不这样做的:

     的char * A =的malloc(5 * sizeof的(炭));
    一个=1;
    自由(一);

    当你调用的免费的,你通过的的这是不通过调用的的malloc分配的。的这会导致你的程序有不确定的行为


    在您的理解的问题是,我怀疑你有什么不明白的 A =1的一样。

    我怀疑你能想象这在字符串中复制到您分配它没有记忆。它改变了指针的的指向不同的地址。

    事实上,分配给的的,而不是它指向的内存,您正在泄漏分配的内存。您code是完全类似这样的:

      INT I = 1;
    我= 2;

    我敢肯定,你可以看到初始化的 I 的是没有意义的,因为你立即覆盖的的 I 的一个新值。这正是你和你的指针做了什么。


    您可以复制与的strcpy 的字符串。像这样的:

     的char * A =的malloc(5 * sizeof的(炭));
    的strcpy(一个1);
    自由(一);

    同样的,当你写

     的char * A =的malloc(5 * sizeof的(炭));
    一个[0] ='1';
    的printf(%S,一);
    自由(一);

    您是通过分配给您的呼叫分配的缓冲区的内容的的malloc 的。因此调用免费是正确的。

    然而,当你这样做的字符串并不一定是空值终止。你观察到你的的printf 的似乎工作,但那只是偶然。

    中的内容的 A [1] 的界定不清,似乎,一个偶然的机会,的 A [1] 的确实包含空当您运行您的程序。

    这就是为什么你通常​​使用像一个字符串函数的的strcpy 的进行副本,以确保空终止。你当然也要注意不要溢出你的缓冲区。


    最后,顾名思义,的sizeof(char)的== 1 的,所以你可以写的的malloc 的是这样的:

     的char * A =的malloc(5);

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
            char* a = malloc(5 * sizeof(char));
            a = "1";
            free(a);
    }
    

    I have two questions regarding the above code:

    1. Why does the code gives an error with free(a) when I initialize a like this :

    a = "1"

    and doesn't give any error with free(a) when I initialize a like this :

    a[0] = '1'.

    1. When I initialize a with a[0] = '1' and print the string a using printf("%s", a) why do I get the result

      '1'

    without declaring the second index with a '\0'?

    解决方案

    The rules with malloc and free are that you must only pass to free a pointer that was allocated by a call to malloc (or one of its equivalents like realloc or calloc). You do not do that:

    char* a = malloc(5 * sizeof(char));
    a = "1";
    free(a);
    

    When you call free, you pass a which was not allocated by a call to malloc. This results in your program having undefined behaviour.


    The problem in your understanding is that I suspect you don't understand what a = "1" does.

    I suspect that you imagine this to copy the string in to the memory that you allocated it does not. It changes the pointer a to point to a different address.

    Indeed, by assigning to a, rather than the memory that it points to, you are leaking the allocated memory. Your code is exactly analogous to this:

    int i = 1;
    i = 2;
    

    I'm sure you can see that the initialization of i is pointless because you immediately overwrite the value of i with a new value. That's exactly what you did with your pointer.


    You can copy the string with strcpy. Like this:

    char* a = malloc(5 * sizeof(char));
    strcpy(a, "1");
    free(a);
    

    Similarly, when you wrote

    char* a = malloc(5 * sizeof(char));
    a[0] = '1';
    printf("%s", a);
    free(a);
    

    you were assigning the contents of the buffer allocated by your call to malloc. And hence the call to free is correct.

    However, the string is not necessarily null-terminated when you do that. You observed that your printf appeared to work but that was just by chance.

    The contents of a[1] are ill-defined and it seems that, by chance, a[1] did contain a null when you ran your program.

    This is why you typically use a string function like strcpy to perform copies and ensure null-termination. You do of course need to take care not to overrun your buffer.


    Finally, by definition, sizeof(char) == 1 so you can write the malloc like this:

    char* a = malloc(5);
    

    这篇关于字符串和指针用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆