简体   繁体   English

unsigned long和uint32_t?

[英]unsigned long and uint32_t?

I'm studying socket programming and I learned inet_addr function. 我正在研究套接字编程,并且了解了inet_addr函数。 But I'm confused how can I handle in_addr_t type. 但是我很困惑如何处理in_addr_t类型。

inet_addr function returns in_addr_t type which is uint32_t , then do I have to use uint32_t type variable to handle it? inet_addr函数返回in_addr_t类型,是uint32_t ,然后做我必须使用uint32_t类型变量来处理呢?

In the book, example handles it as unsigned long type, but I don't understand why this way is used. 在书中,示例将其作为unsigned long类型处理,但我不明白为什么使用这种方式。

unsigned long conv_addr = inet_addr(addr1); // unsigned long == uint32_t?

You do not have to use uint32_t to handle the value returned from inet_addr . 您不必使用uint32_t处理从inet_addr返回的值。 You can use any type that can represent any value that might be returned or that you might use in a calculation. 您可以使用任何可以表示可能返回或在计算中使用的任何值的类型。 But why not use the uint32_t or the in_addr_t type? 但是为什么不使用uint32_tin_addr_t类型呢?

You may use long type also for inet_addr() function since most of the architecture use 32 bits(4 bytes) for long type but it is not always applicable 您也可以对inet_addr()函数使用long类型,因为大多数体系结构对long类型都使用32位(4字节),但并不总是适用

Some architecture use 64 bits for long type.. LP64 convention use 64 bits for long type 某些体系结构将64位用于类型。LP64约定将64位用于长类型

But the size of uint32_t is always 32 bits independent of convention the compiler is following. 但是uint32_t的大小始终为32位 ,与编译器遵循的约定无关。

If you are writing programs using MSVC, it uses 32 bits for long type. 如果使用MSVC编写程序,则长类型使用32位。

For gcc, it depends on computer hardware and implementation of GCC. 对于gcc,它取决于计算机硬件和GCC的实现。

It is safe to use unsigned long for assigning to the return type of inet_addr() when you are confident with long size is 32 bits. 当您确信long大小为32位时,可以安全地使用unsigned long分配给inet_addr()的返回类型。

Recommended to use uint32_t (aka unsigned 32 bit memory) for inet_addr(), so you don't need to care about the size of long type at all... 建议inet_addr ()使用uint32_t (也称为无符号32位内存),因此您根本不需要关心long类型的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM