Allocating memory to char* C language

后端 未结 7 1587
执笔经年
执笔经年 2021-01-05 06:47

Is it the correct way of allocating memory to a char*.

char* sides =\"5\";

char* tempSides;

tempSides = (char*)malloc(strlen(inSides) * sizeof(char));


        
相关标签:
7条回答
  • 2021-01-05 07:18

    Almost. Strings are NULL terminated, so you probably want to allocate an extra byte to store the NULL byte. That is, even though sides is 1 character long, it really is 2 bytes: {5,'\0'}.

    So it would be:

    tempSides = (char *)malloc((strlen(sides)+1)*sizeof(char));
    

    and if you wanna copy it in:

    strcpy(tempSides, sides);
    
    0 讨论(0)
  • 2021-01-05 07:30

    Multiplying the element count by sizeof(char) is a matter of personal preference, since sizeof(char) is always 1. However, if you do this for consistency, better use the recipient pointer type to determine the element size, instead of specifying type explicitly. And don't cast the result of malloc

    tempSides = malloc(strlen(inSides) * sizeof *tempSides);
    

    Of course, when working with zero-terminated strings you have to remember to allocate extra space for the terminating zero character. There's no way to say whether it is your intent to make tempSides a zero-terminated string in this case, so I can't say whether you need it.

    0 讨论(0)
  • 2021-01-05 07:31

    As has been pointed out, you missed allocating space for the terminating NUL chararacter. But I also wanted to point out a couple of other things that can make your code more concise.

    By definition, sizeof(char) is always 1, so you can shorten your allocation line to:

    tempSides = (char*)malloc(strlen(inSides) + 1);
    

    Another thing is that this looks like you are doing to duplicate the string. There is a built in function that does that for you:

    tempSides = strdup(inSides);
    

    This handles getting the length, allocating the correct number of bytes and copying the data.

    0 讨论(0)
  • 2021-01-05 07:35

    No, not really. As others have already noted, you need to allocate space for the NUL terminator.

    In addition, you generally should not cast the return from malloc. It can cover up a bug where you've forgotten to #include the correct header. Multiplying by sizeof(char) is also pointless, since the standards (both C and C++) define sizeof(char) to always be 1.

    Finally, every call to malloc should include a test of the result. I'd wrap the whole thing up into a function:

    char *dupe_string(char const *string) { 
        char *temp;
        if (NULL!=(temp=malloc(strlen(string)+1)))
            strcpy(temp, string);
        return temp;
    }
    
    0 讨论(0)
  • 2021-01-05 07:35

    The correct way of allocation dynamic memory to tempSides is as shown below:

    char* sides ="5";
    char* tempSides;
    tempSides = (char*)malloc((strlen(sides) + 1) * sizeof(char));
    

    char* stores a string data, similar to char[]. Strings are null (\0) terminated. So extra one byte should be allocated for null character storage.

    Dynamically allocated memory block must be freed using free() after it's use is over. If not freed, memory leak would happen.

    free(tempSides);
    

    One the memory is freed, NULL must be assigned to prevent it from being a dangling pointer.

    tempSides = NULL;
    
    0 讨论(0)
  • 2021-01-05 07:37

    Note that:

    1. Strings are zero-terminated (\0), and strlen() doesn't count it;
    2. By definition, sizeof(char) is 1 (byte), so it's not required;
    3. If you use a C (not C++) compiler, there's no need to cast it to char *;

    So that would be:

    char *tempSides = malloc(strlen(inSides) + 1);
    

    Still, if you want to duplicate the contents of inSides, you can use strdup, e.g.:

    char *tempSides = strdup(inSides);
    if (tempSides != NULL) {
        // do whatever you want...
        free(tempSides);
    }
    
    0 讨论(0)
提交回复
热议问题