gpt4 book ai didi

c++ - 将指针设置在其内存范围之外

转载 作者:行者123 更新时间:2023-11-28 02:24:55 25 4
gpt4 key购买 nike

我正在编写一些代码来进行位图混合,我的函数有很多选项。我决定使用 switch 来处理这些选项,但随后我需要将 switch 放在循环中(我读到它会影响性能)或为每个 分配循环code>switch 大小写(使代码太大)。我决定使用第三种方式(见下文):

/* When I need to use static value */

BYTE *pointerToValue = (BYTE*)&blendData.primaryValue;
BYTE **pointerToReference = &pointerToValue;
*pointerToReference = *pointerToReference - 3;

/* When I need srcLine's 4th value (where srcLine is a pointer to BYTE array) */

BYTE **pointerToReference = &srcLine;

while (destY2 < destY1) {
destLine = destPixelArray + (destBytesPerLine * destY2++) + (destX1 * destInc);
srcLine = srcPixelArray + (srcBytesPerLine * srcY2++) + (srcX1 * srcInc);
for (LONG x = destX1; x < destX2; x++, destLine += destInc, srcLine += srcInc) {
BYTE neededValue = *(*pointerToReference + 3); //not yet implemented
destLine[0] = srcLine[0];
destLine[1] = srcLine[1];
destLine[2] = srcLine[2];
if (diffInc == BOTH_ARE_32_BIT)
destLine[3] = srcLine[3];
}
}

有时我可能需要使用 srcLine[3]blendData.primaryValuesrcLine[3] 可以通过 *(*pointerToReference + 3) 轻松访问,但是要访问 blendData.primaryValue 我需要减少指针3 为了保持相同的表达式 (*(*pointerToReference + 3)).

所以这是我的问题:

  1. 如果稍后将指针设置在其内存范围之外是否安全要带回来吗?
  2. 我 100% 确定它在超出范围时不会被使用,但可以我确定它不会导致任何类型的访问冲突?
  3. 也许有某种类似的替代方法可以使用一个变量捕获 srcLine[3]blendData.primaryValue 的值没有 if(),就像在我的代码示例中完成的那样?

最佳答案

因为 #2,没有使用,#1 的答案是肯定的,它是绝对安全的。因为#1,那么就不需要#3。 :-)

访问冲突只有在实际使用指针时才会发生。

关于c++ - 将指针设置在其内存范围之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31081829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com