我在这里检查了许多类型转换问题,但我无法找到解决问题的方法。
我有一组课程根据他们的操作情况互换。我无法提供完整的代码,但我将很快解释有问题的要点。他们过去一直工作,直到CodeBlocks破坏并清空了项目中的所有目标。我添加相同的文件后,它编译但从未定期工作。然后我调试并注意到gcc无法在编译时定期对齐数据。哪些选项可以解决它?
我正在给出解释每个问题的线条;
char* buf; // assumed allocated
int num = 0;
1 - *(int*)buf = num; // I expect four bytes of zero but it writes 1 byte
2 - *(int*)buf ++ = num; // I expect buf to be incremented by 4 but it is buf+1
3 - *((int*)buf) ++ = num; // cant compile (never compiled with gcc)
4 - num = *buf ++; // I expect buf to be +1, but it increments buf by 4 bytes
5 - *(word)buf ++ = (word)0; // I expect 2 bytes 0, writes only 1 byte 0
腐败可能带来哪些选择? 3从未编译,其他用于编译和定期工作。
答案 0 :(得分:1)
好的,这个是无可争议的:
*(int*)buf = num; // I expect four bytes of zero but it writes 1 byte
现在,第一个问题就在这里
char *
演员int *
- >如果指针没有适当对齐,则*(int*)buf ++ = num; // I expect buf to be incremented by 4 but it is buf+1
具有未定义的行为;此外,在一般指针转换的情况下,它会破坏严格的别名规则。
++
优先顺序,*((int*)buf) ++ = num; // cant compile (never compiled with gcc)
增量先于演员表。另外,如果 1 的情况没有未定义的行为,那么很可能会这样,因为你得到一个未对齐的指针,然后转换为指向int的指针。
int
这与演员阵容无关。问题是左侧构造将指针强制转换为指向int *
的指针,生成类型5++;
的值表达式。这不能增加,因为它不是变量;它和
num = *buf ++; // I expect buf to be +1, but it increments buf by 4 bytes
将5逐一递增并将结果值存储到5中意味着什么?
*(word)buf ++ = (word)0; // I expect 2 bytes 0, writes only 1 byte 0
嗯,这只是公然虚假。请记住,后缀运算符会在之后增加。
<div id="nav" style="position:fixed;float:left;overflow-y:hidden;width:10%;"></div>
<div style="margin-left:10%;float:left;overflow-y:auto;width:60%;word-break:break-all;word-wrap:break-word;" id="content"></div>
这与案例1相同。
答案 1 :(得分:0)
*(int*)buf = num; // I expect four bytes of zero but it writes 1 byte
这会将sizeof(int)
个字节写入缓冲区。您有什么证据可以用于#34;它写入1个字节&#34;?
*(int*)buf ++ = num; // I expect buf to be incremented by 4 but it is buf+1
++
有higher precedence而不是(C风格)演员。因此,在写入sizeof(int)
字节之后,就像前一个语句一样,这个字节将递增buf
以指向缓冲区中的下一个char
。您观察到的行为听起来是正确的。
*((int*)buf) ++ = num; // cant compile (never compiled with gcc)
强制转换表达式(int*)buf
创建一个新的临时指针,该指针是从buf
的值转换而来的。它不是变量buf
的名称,您无法像x + y = z;
那样对其进行修改。这听起来像你想要的:
*(int*)buf = num;
buf += sizeof(int);
num = *buf ++; // I expect buf to be +1, but it increments buf by 4 bytes
这将获得char
点buf
的单int
,将其转换为num
并将其存储在buf
中,然后将char
增加到buf
指向缓冲区中的下一个*(word)buf ++ = (word)0; // I expect 2 bytes 0, writes only 1 byte 0
。你有什么证据,它会使word
增加4&#34;?
(word)buf++
什么是++
?如果它是一个数字类型,这没有任何意义,也不应该编译:你不能取消引用数字表达式buf
。
如果它是指针类型,请首先请不要使用隐藏指针的typedef!请再次注意,由于char
的优先级高于强制转换,因此在执行*(word)buf = (word)0;
之后,Excel.DataLabel dl1 = (Excel.DataLabel)series.DataLabels(0);
dl1.NumberFormat = "0.00%;-0.00%;";
会将export default class Index extends React.Component {
componentDidMount() {
window.onGoogleLoginSuccess = this.onGoogleLoginSuccess.bind(this)
}
render() {
return (
<div
className="g-signin2"
data-onsuccess="onGoogleLoginSuccess"
data-theme="dark"
/>
}
}
增加一import Stack
def palidromeChecker(words):
palidromestack = Stack.Stack()
words = user_input("Type a word: ")
for x in words:
palidromestack.push(x)
palindrome = True
for x in words:
if x != palidromestack.top():
palindrome = False
break
palidromestack.pop()
if palindrome:
print (words, "is a palindrome")
else:
print (words, "is not a palindrome")
。
答案 2 :(得分:0)
我回到Borland C ++ 5.02解决了这个问题。
3 - *((int*)buf) ++ = num; // cant compile (never compiled with gcc)
是这里的主要问题。通过将该短语更改为
等代码,我的工作副本被gcc采用*(type*)buf = (type)num;
buf += sizeof(type);
我发布了一对工作副本(关于你们有些人说永远不会工作)来序列化一个节点,其中包含可能有其他节点子节点的子节点列表......(获取和放置不是那些节点)你知道,它们是我特有的)
int __fastcall HNode::send(char* buf)
{
char* tmp = buf;
if(this)
{
*((word*)tmp) ++ = *(word*)B; // first 16 bits (persistent info)
tmp += HspKod::send(tmp);
tmp += puts(tmp, str); // acn name
tmp += alt->send(tmp); // node list (null writes (word)0 only
} return tmp-buf;
}
int __fastcall HNode::read(char* buf)
{
char* tmp = buf;
if(this)
{
*(word*)B = *((word*)tmp) ++; // persistent info bits
tmp += HspKod::read(tmp);
tmp += gets(tmp, str); // acn name
int num = *((word*)tmp); // number of sub-nodes
if(num && alt==0)
alt = new HNods(this);
tmp += alt->read(tmp); // alt reads 2 bytes even if it is null
} return tmp-buf;
}