When to allocate memory in C?

I have a code that it should move to bytes. It works, but my question is different?

What am I doing wrong and what am I doing right with regard to highlighting example_byte and new_byte ? Is this too much trouble for this simple program? Should I just not use malloc and let the compiler do the dirty work better?

Here are some opinions on this in the comments section: link .

#include <stdio.h> #include <malloc.h> typedef unsigned __int8 byte; byte move(byte* our, int indexold, int indexnew) { byte oldvalue; byte newvalue; byte valuetochange; valuetochange = 0x01 & ((*our)>>indexold); // get the value of the bit to be moved printf("value to change : %d\n", valuetochange); oldvalue = (*our) & (~(1<<(indexold))); // del the bit from position indexold oldvalue = oldvalue & (~(1<<(indexnew))); // del the bit from position indexnew printf("deleted: %x\n", oldvalue); newvalue = oldvalue | (valuetochange<<(indexnew)); // write bit in new position (indexnew) return newvalue; } int main() { byte* example_byte; byte* new_byte; example_byte = (byte*)malloc(sizeof(byte)); new_byte = (byte*)malloc(sizeof(byte)); *example_byte = 0xc3; // hex 0xc3 = binary 1100 0011 printf("\n"); //***************************************************** // example 1 (move bit from position 1 to position 5) // example_byte 1100 0011 // ^ ^ // memorize bit -> valuetochange = 0x01 & ((*our)>>indexold) = 1 // 1100 0011 & 1111 1101 = 1100 0001 delete bit from oldindex (1) // 1100 0001 & 1101 1111 = 1100 0001 delete bit from newindex (5) // new_byte 1100 0001 | 0010 0000 = 1110 0001 *new_byte = move(example_byte, 1, 5); printf("old byte : %x\n", *example_byte); // 0xc3 (1100 0011) printf("new byte : %x\n", *new_byte); // 0xe1 (1110 0001) printf("\n"); //***************************************************** // example 2 (move bit from position 6 to position 3) // example_byte 1100 0011 // ^ ^ // memorize bit -> valuetochange = 0x01 & ((*our)>>indexold) = 1 // 1100 0011 & 1011 1111 = 1000 0011 delete bit from oldindex (6) // 1000 0011 & 1111 0111 = 1000 0011 delete bit ftom newindex (3) // new_byte 1000 0011 | 0000 1000 = 1000 1011 *new_byte = move(example_byte, 6, 3); printf("old byte : %x\n", *example_byte); // 0xc3 (1100 0011) printf("new byte : %x\n", *new_byte); // 0x8b (1000 1011) printf("\n"); //***************************************************** // example 3 (move bit from position 2 to position 6) // example_byte 1100 0011 // ^ ^ // memorize bit -> valuetochange = 0x01 & ((*our)>>indexold) = 0 // 1100 0011 & 1111 1011 = 1100 0011 delete bit from oldindex (2) // 1100 0011 & 1011 1111 = 1000 0011 delete bit from oldindex (6) // new_byte 1000 0011 | 0000 0000 = 1000 0011 *new_byte = move(example_byte, 2, 6); printf("old byte : %x\n", *example_byte); // 0xc3 (1100 0011) printf("new byte : %x\n", *new_byte); // 0x83 (1000 0011) printf("\n"); //***************************************************** // example 4 (move bit from position 2 to position 4) // example_byte 1100 0011 // ^ ^ // memorize bit -> valuetochange = 0x01 & ((*our)>>indexold) = 0 // 1100 0011 & 1111 1011 = 1100 0011 delete bit from oldindex (2) // 1100 0011 & 1110 1111 = 1100 0011 delete bit from oldindex (4) // new_byte 1100 0011 | 0000 0000 = 1100 0011 *new_byte = move(example_byte, 2, 4); printf("old byte : %x\n", *example_byte); // 0xc3 (1100 0011) printf("new byte : %x ", *new_byte); // 0xc3 (1100 0011) printf("\n"); free(new_byte); free(example_byte); return 0; } 
+4
source share
3 answers

If it is one byte, there is no reason to allocate memory. You can simply declare a local variable of type byte and pass it.

You need to use malloc() when you need to allocate an array of elements, and you don't know the number of elements at compile time. You also need it when you need to allocate a large structure that must be stored in memory after the function that created it exits. If you are dealing with a variable of a primitive type, you should simply declare it as a local variable, which means that it will be automatically assigned on the stack.

+8
source

You only need to allocate memory when you need something that will be visible outside the scope in which you declared it (and any areas nested inside it), or if you need to dynamically allocate memory for some other reason like creating a data structure of unknown size. Otherwise, you should simply declare a local variable.

+13
source

As Dima said: just use the byte and pass its address to the move function using the & operator.

 byte example_byte; byte new_byte; example_byte = 0xc3; // hex 0xc3 = binary 1100 0011 new_byte = move(&example_byte, 1, 5); 

You can also change the type of the move(...) parameter to byte , which will be easier to use.

+3
source

Source: https://habr.com/ru/post/1340347/


All Articles