Behaviour of malloc(1) in C

Asked

Viewed 267 times

4

If I use char *char_commandout = (char *) malloc(1);, the allocated byte will store the " 0" at position 0 of the vector or will allocate a space for the value I want to store (at position 0) and one for the " 0" (at position 1)?

  • One of the answers solved your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

2 answers

8

Victor already gave the basic answer to the question, I’ll add that it’s common for people to use a memset() to reset memory when necessary. When necessary calloc() can be used to allocate and already zero. In theory it is possible to have optimizations, maybe even in conjunction with the operating system to gain performance, but do not count on this, including because even the c/re/malloc() need to be the original. The pro malloc() is the cleaning of data.

In case you interpret the data as a string, and in C you can interpret as you like, a 1 byte zeroed will be a string 0 characters long, the \0 will be the terminator of string.

In C it is not appropriate to do cast of the outcome of malloc(), this can hide mistakes, so what you want is just:

char *char_commandout = calloc(1);

But overall this doesn’t make sense because you already know the content and it can’t change in a useful and correct way, even if you need a string 0 the ideal size is to do this statically, even used the Flyweight.

Another curiosity that makes no sense is to use

malloc(sizeof(char))

I put in the Github for future reference.

It is guaranteed by specification that char has always the size 1, in the background it is 1 byte. There are those who argue that one day can change, which is laughable, mainly because it is always said by an "experienced programmer".

Usually allocations in heap above 8 bytes, even 16 make very little sense and in the cases it does it would be the case to rethink whether it can do otherwise. In small things above the memory consumption to control the allocation is greater than the data itself. Of course, it’s not the only way to decide, there are things that need an indefinite allocation, but it’s also almost always possible to avoid this. In practice the allocation will be much larger than 1 byte, but you will only be able to access 1 byte, and then you try to access more than one before or after it, it will work, but for your sake hopefully the code breaks next.

  • In my code, I intend to create a string with 2 bytes (one for a char and the other for the " 0") and then use realloc to increase the size of the string according to what the user enters (dynamic allocation). My question is whether when using malloc(1), I can already use this space to store the first char of my string. I’m sorry if you already answered that, but I didn’t get this part.

  • I don’t know if I get it, but reserving a byte and wanting to add two doesn’t work. But as I said this strategy (even 2 bytes) should not even be adequate. There’s actually a guy who doesn’t even need to make that allocation that you’re looking for.

  • I’ll put the function I created in my question

  • @Cooper just has one small detail, we already answered what you asked, now we need to ask another question. And apparently you haven’t read my answer, and for that, and not only that, I can tell you that your code doesn’t make any sense.

7


Neither of the two. The malloc allocates the memory area, but does not wipe it. Therefore, any content that was abandoned there will continue there (this content is affectionately called rubbish). Therefore, the contents of the pointer after the malloc can be anything because it contains garbage.

It is recommended to write something in allocated memory to prevent you from accessing junk content, which would probably make the program behave incorrectly.

If you call malloc(1), a pointer pointing to 1 byte of allocated memory will be returned. It may even be that the malloc internally reserve a larger area for performance, but the only guarantee that exists is that 1 byte of reserved memory will be returned to you (reserved, but not cleaned), and that if you access beyond the limit (the second byte, at position 1)you will be accessing an unrestricted memory area (which may cause a segmentation failure, access the content of some other variable or read or write trash somewhere).

  • 1

    Affectionately called garbage ROFL

  • 4

    @Marcelouchimura he said this because the snowflakes always end up thinking "there, he said garbage, this is ugly, he is evil, he is not polite, has no empathy", then you have to turn around to give the name of that without offending someone and roll mimimi, what is the excretory orifice ofídico. Well, it may not be his official intention, but that’s it... :)

  • The police of political correctness even in the IT area, old man... and I thought, as a child, that I would live to pick up the joystick of Atari kkk

  • For those who don’t know, Atari is a tough black, bigger than a PS4.

  • Now there’s a small one, and it’s expensive for the donkey. You can make the size of a flash drive (not those q is just a cap) that costs about 100 real and q has all his games

  • In my code, I intend to create a string with 2 bytes (one for a char and the other for the " 0") and then use realloc to increase the size of the string according to what the user enters (dynamic allocation). My question is whether when using malloc(1), I can already use this space to store the first char of my string. I’m sorry if you already answered that, but I didn’t get this part.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.