【问题标题】:Using char array as number for math in C++ [closed]在 C++ 中使用 char 数组作为数学的数字 [关闭]
【发布时间】:2011-12-03 19:43:46
【问题描述】:

我正在尝试在 C++ 中创建 128 位和 256 位整数,并注意到将 char** 转换为 int* 和 int* 转换为 int(以及向后)可用于将 char 数组转换为整数和整数到 char 数组。 此外,char* + int 工作正常。

但是,当我尝试char* + char* 时,编译器告诉我类型无效。有什么解决方法吗,还是我必须为操作员编写自己的函数?

例如:

int32_t intValue = 2147483647;
char *charPointer = *( char** ) &intValue;
charPointer += 2147483647;
charPointer += 2;
cout << ( *( int64_t* ) &charPointer )  << endl;

输出:4294967296

基本上,我所做的应该是这样的:

int32_t intValue = 2147483647;

记忆中的某处:

[ 05 06 07 08 09 0A 0B 0C ] ( address, in hex )
[ .. .. FF FF FF 7F .. .. ] ( value, in hex )

然后:

char *charPointer = *( char** ) &intValue;

记忆中的某处:

[ 58 59 5A 5B 5C 5D 5E 5F ] ( address, in hex )
[ .. .. 07 00 00 00 .. .. ] ( value, in hex )

然后:

charPointer += 2147483647;

我真的不知道这里会发生什么。 它似乎做了这样的事情:

[ 05 06 07 08 09 0A 0B 0C ] ( address, in hex )
[ .. .. FF FF FF FE .. .. ] ( value, in hex )

然后:

charPointer += 2;

这里也一样。 像这样的:

[ 05 06 07 08 09 0A 0B 0C ] ( address, in hex )
[ .. .. 00 00 00 00 01 .. ] ( value, in hex )

最后我只是打印它好像它是一个 8 字节整数:

cout << ( *( int64_t* ) &charPointer )  << endl;

那么,谁能解释为什么添加的不是指针的值,而是被指向的值?

【问题讨论】:

  • 请提供一些代码来说明您的意思。为什么要添加指向另一个指针的指针?
  • 我不认为 char* + int(或 int* 到 int(或向后))做你认为它做的事情
  • 问题的前提是无效的。但谷歌“bigint c++”
  • @RPFeltz:根据您对我的回答的反应,我可以看出这不会有建设性。所以我把它删了。我给出的+= 10000 例子很糟糕。但是试试int intValue = 2147483647; 和charValue += 2147483647; charValue += 2; 你会看到...
  • 根据您对我们的回应以及您的编辑,老实说,我无法判断您是否真的不了解发生了什么,或者您是否只是在拖钓。 (我希望是前者。)所以我不会继续玩这个反例游戏。但是考虑一下当“实际”数字溢出 64 位时会发生什么。或者如果你只是为 32 位编译...

标签: c++ math operators int arrays


【解决方案1】:

这些转化是存在的,但它们并没有按照您的想法进行。将指针转换为整数只是将其视为整数;它没有做任何实际的“数学”。例如,char * s = "abcd"; int i = (int) s; 不会每次都给出相同的结果,因为s 和i 都只是字符串开始的内存地址。两者都与字符串的实际内容无关。

同样,char* + int 只是进行偏移。写char * s = "abcd"; char * t = s + 2; 只是写char * s = "abcd"; char * t = &amp;(s[2]); 的另一种方式;即s是'a'的内存位置,t是'c'的内存位置(s,偏移两个char-width,即两个字节)。除了“指针算术”需要数学来计算字节偏移量和查找内存位置之外,没有发生实际的数学运算。

char * + char * 没有意义:将两个内存位置“添加”在一起意味着什么?

编辑:这是您添加到问题中的代码:

int intValue = 5198;
char *charValue = *( char** ) &intValue;
charValue += 100;
cout << ( *( int* ) &charValue )  << endl;

让我稍微扩展一下,这样会更清楚发生了什么:

int intValue = 5198;
int * intPtr = &intValue;
// intPtr is now the address of the memory location containing intValue
char ** charPtrPtr = (char**) intPtr;
// charPtrPtr is now the address of the memory location containing intValue,
// but *pretending* that it's the address of a memory location that in turn
// contains the address of a memory location containing a char.
char *charPtr = *charPtrPtr;
// charPtr (note: you called it "charValue", but I've renamed it for clarity)
// is now intValue, but *pretending* that it's the address of a memory
// location containing a char.
charPtr += 100;
// charPtr is now 100 more than it was. It's still really just an integer,
// pretending to be a memory location. The above statement is equivalent to
// "charPtr = &(charPtr[100]);", that is, it sets charPtr to point 100 bytes
// later than it did before, but since it's not actually pointing to a real
// memory location, that's a poor way to look at it.
char ** charPtrPtr2 = &charPtr;
// charPtrPtr2 is now the address of the memory location containing charPtr.
// Note that it is *not* the same as charPtrPtr; we used charPtrPtr to
// initialize charPtr, but the two memory locations are distinct.
int * intPtr2 = (int *) charPtrPtr2;
// intPtr2 is now the address of the memory location containing charPtr, but
// *pretending* that it's the address of a memory location containing an
// integer.
int intValue2 = *intPtr2;
// intValue2 is now the integer that results from reading out of charPtrPtr2
// as though it were actually pointing to an integer. Which, in a perverse
// way, is actually true: charPtrPtr2 pointed to a memory location that held
// a value that was never *really* a memory location, anyway, just an integer
// masquerading as one. But this will depend on the specific platform,
// because there's no guarantee that an "int" and a pointer are the same
// size -- on some platforms "int" is 32 bits and pointers are 64 bits.
cout << intValue2  << endl;

这有意义吗?

【讨论】:

  • 对不起,我想你误会了。我编辑了问题。
  • @RPFeltz:对不起,但我确实明白了。我已编辑我的答案以反映您的编辑。
  • 我不明白为什么人们不赞成你的回答。 +1
  • 无论如何它都没有任何意义。另外,我只希望将它用作容器,稍后我会做一些将其转换为十进制的东西,所以 int 的大小应该无关紧要。我会再次编辑它,包含大量数字和更具体的类型。
  • 指针为ints,并且您肯定将任意整数存储为char*s 将一无所获。 char * s = 300; 是一个错误。可以编写一个包含它的工作程序,但是该程序将在尽管这个错误决定的情况下工作,而不是因为它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
相关资源
最近更新 更多