使用指针的数据类型的大小?

时间:2015-05-16 19:36:51

标签: c++ visual-c++ c++14

我想查看计算机上数据类型的大小。所以我用这个代码开始使用int数据类型:

#include <iostream>
using namespace std;
int main()
{
int t, *tpntr1, *tpntr2;
tpntr1 = &t;
cout << "The first address: \t" << tpntr1;
tpntr2 = ++tpntr1;
cout << "\n The second address : \t" << tpntr2;
unsigned int size= (tpntr2 - tpntr1);
cout << "\n the size of int : \t" << size<<"\n";
return 0;
}

在visual studio中编译后,返回的大小为0.在特定的运行中,第一个地址是0028FBA4,第二个地址是0028FBA8,但差异是零。有人可以指出我在这里做错了什么吗?我想它的内容与十六进制到十进制的转换有关。

2 个答案:

答案 0 :(得分:0)

这里有一个提示:将#include <iostream> using namespace std; int main() { int t, *tpntr1, *tpntr2; tpntr1 = &t; tpntr2 = ++tpntr1; unsigned int size= (tpntr2 - tpntr1); cout << "The first address: \t" << tpntr1; cout << "\n The second address : \t" << tpntr2; cout << "\n the size of int : \t" << size<<"\n"; return 0; } 移到最后,看看发生了什么:

tpntr2

地址相同。您真的将tpntr1分配给tpntr2 = ++tpntr1

如果您将tpntr2 = tpntr1; tpntr2++; 更改为

1

它会部分地做你想要的。请注意,这将打印<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>transport</groupId> <artifactId>transport</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>transport</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <!-- Specify the maven code generator plugin --> <groupId>org.jooq</groupId> <artifactId>jooq-codegen-maven</artifactId> <version>3.6.1</version> <!-- The plugin should hook into the generate goal --> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <!-- Manage the plugin's dependency. In this example, we'll use a PostgreSQL database --> <dependencies> <dependency> <groupId>org.firebirdsql.jdbc</groupId> <artifactId>jaybird-jdk18</artifactId> <version>2.2.7</version> </dependency> </dependencies> <!-- Specify the plugin configuration. The configuration format is the same as for the standalone code generator --> <configuration> <!-- JDBC connection parameters --> <jdbc> <driver>org.firebirdsql.jdbc.FBDriver</driver> <url>jdbc:firebirdsql:localhost/3050:/Users/vladstarikov/Dropbox/IdeaProjects/Database/untitled/src/KURSOVA.FDB</url> <user>SYSDBA</user> <password>masterkey</password> </jdbc> <!-- Generator parameters --> <generator> <name>org.jooq.util.DefaultGenerator</name> <database> <name>org.jooq.util.firebird.FirebirdDatabase</name> <includes>.*</includes> <excludes></excludes> <inputSchema></inputSchema> </database> <target> <packageName>org.jooq.util.maven.example</packageName> <directory>target/generated-sources/jooq</directory> </target> </generator> </configuration> </plugin> </plugins> </build> </project> 作为大小。我可能会弄错,但是指针运算会考虑到类型的大小(有人请纠正我,如果不是这样的话。我不能解释它)。

答案 1 :(得分:0)

Carcigenicate指出,你的主要问题是++修改了价值。但是,由于指针算法的工作原理,您可能也无法获得所需的结果。

更正后的形式如下:

tpntr1 = &t;
tpntr2 = tpntr1;
tpntr2++;

但是,您的结果将为1,因为tpntr2 - tpntr1将返回整数的差异。由于int是4个字节(至少在我的实现中),这将打印差值为1,即使内存地址相隔4个。

如果你想要实际的字节(可能是4),你需要转换为一种绕过这种行为的类型。你有两个选择:将指针强制转换为int,这将使它成为对数字的简单数学运算,或reinterpret_cast<char*>,它将告诉它将指针视为对一个字节的内存。例如,这将显示大小为4:

int main()
    {
    int t, *tpntr1, *tpntr2;
    tpntr1 = &t;
    tpntr2 = tpntr1;
    tpntr2++;
    unsigned int size= (reinterpret_cast<char*>(tpntr2) - reinterpret_cast<char*>(tpntr1));
    cout << "The first address: \t" << tpntr1;
    cout << "\n The second address : \t" << tpntr2;
    cout << "\n the size of int : \t" << size<<"\n";
    return 0;
}