c-如何在没有malloc的情况下使用交换函数



我想创建没有malloc的void指针。但是通过交换值和使用void指针,我看不到不使用malloc的解决方案。

有没有一种方法可以在不使用malloc的情况下完成这种操作。我一直在考虑双分球,但这也不是解决办法。

#include <stdio.h> // Standard library functions for file input and output
#include <stdlib.h> // Standard library used for memory allocation, process control, and conversions
typedef unsigned char byte_t;
// Declaration of the function
void genericSwap(void *pdata1, void *pdata2, byte_t nBytes);
// Global variable
unsigned int dataFloat = 0; // this will help us as a flag to differentiate
// between a float or integer
int main() // main program
{
int sw;             // integer value for switch
int a = 0, b = 0;   // declaring integer a and b
char x = 0, y = 0;  // declaring character x and y
float c = 0, d = 0; // declaring float c and d
for (;;)            //  infinite loop
{
printf("nPlease enter your choice:n'1' for integer value.n'2' for "
"character value.n'3' for float value.n'0' for exit. n : ");
scanf_s("%d", &sw); // choose the input for the switch function
switch (sw) // switch function
{
case 1: // case 1 for selecting integer operations
printf("nPlease enter the values for a and b n : ");
scanf_s("%d%d", &a, &b); // choose the input for the integer operation
printf("nValues before swapna=%dtb=%d", a, b);
genericSwap(
&a, &b,
sizeof(int)); // calling the swap function with address of a and b
printf("nValues after swapna=%dtb=%d", a,
b); // printing the swap values
break;
case 2: // case 2 for selecting character operations
printf("nPlease enter the values for a and b n : ");
scanf_s(" %c", &x);
scanf_s(" %c", &y);
printf("nValues before swapna= %ctb= %c", x, y);
genericSwap(
&x, &y,
sizeof(char)); // calling the swap function with address of x and y
printf("nValues after swapna= %ctb= %c", x, y);
break;
case 3:          // case 3 for selecting float operations
dataFloat = 1; // indicate that the float operation has been selected
printf("nPlease enter the values for a and b n : ");
scanf_s("%f%f", &c, &d);
printf("nValues before swapna= %.2ftb= %.2f", c, d);
genericSwap(
&c, &d,
sizeof(float)); // calling the swap function with address of c and d
printf("nValues after swapna= %.2ftb= %.2f", c, d);
break;
case 0: // terminate the application
exit(1);
break;
default: // when no appropriate selection has been made
printf("nYou have entered the wrong choice");
break;
}
}
}
// define function
void genericSwap(void *pdata1, void *pdata2, byte_t nBytes) {
void *temp = NULL; // setting void pointer temp to NULL
temp = malloc(nBytes); // allocating memory to void pointer
if (nBytes > 1) // if the size is more then 1 byte this means it is another
// value then a character
{
if (dataFloat == 1) // if dataFloat is equal to 1 then it is a float type
{
dataFloat = 0;                         // resetting dataFloat to 0
*((float *)temp) = *((float *)pdata1); // typecasting the pointer to float
*((float *)pdata1) = *((float *)pdata2);
*((float *)pdata2) = *((float *)temp);
} else {
*((int *)temp) = *((int *)pdata1); // typecasting the pointer to integer
*((int *)pdata1) = *((int *)pdata2);
*((int *)pdata2) = *((int *)temp);
}
} else {
*((char *)temp) = *((char *)pdata1); // typecasting the pointer to character
*((char *)pdata1) = *((char *)pdata2);
*((char *)pdata2) = *((char *)temp);
}
free(temp); // de-allocating memory
temp = NULL; // making the pointer temp to NULL
}
void *genericSwap(void *a, void *b, size_t size)
{
uint64_t tmp;   //max size depends on the hardware architecture
unsigned char *ua = a;
unsigned char *ub = b;
if(a && b)
{
while(size >= sizeof(tmp))
{
memcpy(&tmp, ua, sizeof(tmp));
memcpy(ua, ub, sizeof(tmp));
memcpy(ub, &tmp, sizeof(tmp));
ua += sizeof(tmp);
ub += sizeof(tmp);
size -= sizeof(tmp);
}
while(size--) 
{
unsigned char tmp = *ua;
*ua++ = *ub;
*ub++ = tmp;
}
}
return a;
}

如果体系结构能够处理对32或64位数据的未对齐访问,那么编译器很可能会优化对memcpy的调用。

https://godbolt.org/z/9vxP4j

Cortex-M0要求数据对齐,而编译器没有优化memcpy。

您可以将缓冲区设置得更大(在本例中为64字节(,编译器也会生成非常高效的代码:https://godbolt.org/z/vxK1vj

最新更新