2

I am not 100% sure whether I correctly allocated an array with 10 elements

char *str;


str = (int*)malloc(10 * sizeof(int)); 

The task was to allocate memory of string with 10 elements.

Have I done it correctly or do I have to add "+ 1" because of the '\0' at the end.

Thanks!

6
  • 1
    char *str, why what where?! Commented Jan 30, 2018 at 18:25
  • 2
    Note that in c, you don't cast pointers returned by malloc Commented Jan 30, 2018 at 18:26
  • 1
    you are allocating too much memory, str is a char pointer not an int pointer. so you only have to allocate 11 bytes. which can be done like this str = malloc(11); Commented Jan 30, 2018 at 18:30
  • 1
    What is your "element"? Character or int? Make up your mind. Commented Jan 30, 2018 at 18:46
  • 2
    \0 terminator only applies to strings. If you need a string of 10 characters, then you need 11 bytes. Nowhere in your question did you mention a "string" or "character string", so it's unclear what you want. Commented Jan 30, 2018 at 18:47

1 Answer 1

6

You have allocated many times more than you need. (sizeof(int) times to be precise). The correct would be

#define MAXLEN 10
...
str = malloc(sizeof *str*(MAXLEN+1));

Note that this will be sizeof(char) which is 1. So you can do this also

str = malloc(MAXLEN+1);

Check the return value of malloc as shown below: (malloc might not be able to service the request, it might return a null pointer. It is important to check for this to prevent later attempts to dereference the null pointer).

str = malloc(MAXLEN+1);
if(!str){
    perror("malloc");
    exit(EXIT_FAILURE);
}

Also void* to char* conversion is implicit - you don't need to cast the return value of malloc.

sizeof(*str) is a cleaner way to get the size of the type of the element for which we are allocating memory in str. The benefit is when you are later changing the code and making str point to an allocated memory which will contain int-s you don't need to look for sizeof(char) and then replace it with sizeof(int) it is done here by using sizeof(*str) automatically.

Sign up to request clarification or add additional context in comments.

7 Comments

But why do you do sizeof(*str) instead of sizeof(char) for example?
@Desibre93.: Read the last part of the answer.
Thanks alot, but can I also write it simply like this str = malloc(11) ?
never write malloc(11). When you read the code later - whats 11? (why not 12 or 42?)
@Desibre93 basic programming technique: use constant defines for things like buffer sizes, string lengths, etc. Do not hard-code them in the code. For example, if you just used 11, then when you write a loop, say, to go through your buffer, you'd need a 10 or 11 there as well. Then later when you need to change the code to handle a different number, you have to find all the places that have that number. So use #define MAXLEN 10 for the number of characters, and MAXLEN+1 for the size of the needed buffer. Then if it has to change, you only have to change the value in #define MAXLEN 10.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.