sizeof函数详解
时间: 2024-06-19 16:03:45 浏览: 15
sizeof是C/C++中的一种运算符,用于计算数据类型或变量所占用的内存大小(单位为字节)。它的用法为sizeof(type)或者sizeof(expression),其中type表示数据类型,expression表示变量或表达式。
sizeof返回的结果是一个无符号整数,其大小等于数据类型或变量所占用的字节数。在计算时,编译器会考虑到数据类型的对齐方式,因此结果可能会比实际大小稍大。
需要注意的是,sizeof并不是一个函数,而是一个编译器内置的运算符。因此,在使用时不需要包含任何头文件。
相关问题
malloc函数详解
malloc函数是C语言中用于动态分配内存的函数。它的原型为:
```c
void* malloc(size_t size);
```
malloc函数接受一个参数size,表示需要分配的内存大小(以字节为单位)。它返回一个指向分配内存的指针,如果分配失败,则返回NULL。
malloc函数在内存堆中分配一块连续的内存空间,大小由size参数指定。这块内存空间是未初始化的,其中的内容是不确定的。
使用malloc函数分配内存后,可以通过返回的指针来访问和操作这片内存区域。可以使用指针进行读写操作,可以存储任意类型的数据。
需要注意的是,使用完分配的内存后,应该使用free函数将其释放,以便将内存还给系统。
以下是一个示例代码,演示了malloc函数的基本用法:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr;
int n = 5;
// 分配内存
ptr = (int*) malloc(n * sizeof(int));
if (ptr == NULL) {
printf("内存分配失败\n");
return 1;
}
// 写入数据
for (int i = 0; i < n; i++) {
ptr[i] = i + 1;
}
// 读取数据
for (int i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
// 释放内存
free(ptr);
return 0;
}
```
这段代码中,首先使用malloc函数分配了n个int类型所占内存大小的空间,然后使用指针ptr访问和操作这片内存区域。最后使用free函数释放了分配的内存。
总结:malloc函数是C语言中动态分配内存的常用函数,它可以根据需要分配内存,并返回一个指向分配内存的指针。使用完分配的内存后,应该使用free函数将其释放。
recvfrom函数详解
`recvfrom` 函数用于接收一个来自指定套接字的数据报,并将发送方的地址和端口号存储在指定的缓冲区中。
函数原型如下:
```c
#include <sys/socket.h>
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
```
参数说明:
- `sockfd`:指定接收数据的套接字文件描述符。
- `buf`:指向接收缓冲区的指针。
- `len`:指定接收缓冲区的大小。
- `flags`:指定接收操作的标志位,通常设置为 0。
- `src_addr`:指向发送方地址信息的结构体指针。
- `addrlen`:指向发送方地址信息结构体长度的指针。
函数的返回值为接收到的字节数,如果出现错误则返回 -1。
注意事项:
- 如果套接字是非阻塞的,则 `recvfrom` 函数可能会返回 -1 并设置 `errno` 为 `EAGAIN` 或 `EWOULDBLOCK`。
- 如果接收缓冲区中的数据长度大于 `len`,则数据将被截断。
- `src_addr` 和 `addrlen` 参数可以设置为 `NULL`,表示不关心发送方地址信息。
示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addrlen = sizeof(client_addr);
char buffer[BUFFER_SIZE];
// 创建套接字
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
// 绑定地址
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(sockfd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 接收数据
ssize_t num_bytes;
while (1) {
memset(buffer, 0, BUFFER_SIZE);
num_bytes = recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&client_addr, &client_addrlen);
if (num_bytes < 0) {
perror("recvfrom failed");
exit(EXIT_FAILURE);
}
printf("Received message from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
printf("Data: %s\n", buffer);
}
close(sockfd);
return 0;
}
```
该示例代码创建一个 UDP 服务器,不断接收发送方发送的数据,并输出发送方的地址信息和数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)