Why exactly malloc is used?

This question already has an answer here:

  • Difference between declared string and allocated string 3 answers

  • char *strarray[5];
    

    will allocate array of five pointers to the characters (which can be interpreted as strings).

    What this will not allocate is the memory space to place these five strings to. So, if you want to make all pointers to point to the different strings, you need to allocate them yourself.

    However, as these are pointers, you don't really need to allocate anything, if you don't want to. That is why your first example is working. strarray[0]="hello"; will make the first element of the array to point to the location where "hello" string literal is stored (the compiler will put this literal somewhere in the memory for you, so your pointer will point to the valid memory location).

    To sum up, in the first example, your pointer is pointing to the memory already allocated and initialized with "hello" (compiler did this for you) - you're not copying the string, you're just assigning value to the pointer. In the second example your pointer is pointing to some undefined location, which you didn't explicitly reserved, but you're trying to write into it.


    This is wrong: char *strarray[5]; This is stack-allocating 5 char* pointers. I think you wanted to write char strarray[6] , which creates 6 chars on the stack. That can accommodate "hello" and its implicit NULL-terminator (the NULL terminator tells C functions that a string has ended).

    Having written char strarray[6]; , strcpy(strarray, "hello"); is safe. Then you can call:

    printf("%sn",strarray);
    

    where I have dropped the [0]: the %s expects a char* not a char : strarray[0] is a char type.

    You use malloc to allocate memory on the heap. Might I suggest you read Kernighan and Ritchie (C Programming Language) for more details on that?


    malloc() : Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.

    void* malloc (size_t size);  
    

    size_t is an unsigned int type.

    it is needed if you don't know how much memory you wan't to create,this is not correct.
    you should know how much minimum memory required.

    with the help of malloc() you can allocating memory at run time not at compile time.

    strarray[0]="hello";   
    //here you need not to allocate memory as this is an assignment 
    

    You need to allocate some space before using a pointer as destination string in strcpy()

    strcpy() Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

    the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.

    for this you need to allocate Memory first .

    strcpy itself doesn't allocate memory for the destination string

    链接地址: http://www.djcxy.com/p/60878.html

    上一篇: “objectclass不等于”不起作用

    下一篇: 为什么使用malloc?