繁体   English   中英

数组给我错误::字符串下标超出范围; 但一切似乎都井然有序

[英]Arrays giving me errors:: String subscript out of range; but everything seems to be in order

我在使用Winsock编码时遇到了一些数组问题,在DirectX 11中则遇到了一个问题。在DirectX 11中,它实际上是我要发布的纹理数组。

这是Winsock问题:

int retval;
    retval = recv(hclientSocket, tempBuffer, sizeof(tempBuffer), 0);
    if (retval == 0)
    {
        break; // Connection has been closed
    }
    else if (retval == SOCKET_ERROR)
    {
        throw ErrorHandler("Failed to receive due to socket");
    }
    else
    {
        Encyrption enc;
        string done = enc.Cipher(tempBuffer, retval);
        retval = retval * 3;
        cout << retval; // it prints out 3
        for (int i = 0; i < retval; i++) {
            tempBuffer[i] = done[i]; //the error is being pointed here on the 6th time it runs through this, even though its only suppose to go through this 3 times 
        }

        if (send(hclientSocket, tempBuffer, retval, 0) == SOCKET_ERROR)
            throw ErrorHandler("Failed to send due to socket");
    }

好的,我大部分代码都是从Winsock教程中获得的,但是我想尝试一种不同的加密方法。

这是调用函数,因为最初打算传递并返回一个字符串,但是这次我传递一个char*并返回一个字符串,该字符串在上述代码中进行了转换。

加密采用一个字符并将其转换为3的字符串,例如a会变成bcac会变成cba ,这就是为什么我将retval乘以3的原因。它会打印出我想要打印的所有内容,但是完成后给出错误。

string pass = (string)message;
pass.resize(size);
for (int i = 0; i < size; i++) {
    if (!isalnum(pass[i])) {
        return "\n";
    }
    else {
        return Cipher(pass);
    }
}

好的,这是Directx11问题

我最近学习了如何通过纹理数组使用多纹理,而Im在发布它时遇到了麻烦。

#define TEXTURE_ELEMENTS_COUNT 2
ID3D11ShaderResourceView* m_textures[TEXTURE_ELEMENTS_COUNT];
for (int i = 0; i <= TEXTURE_ELEMENTS_COUNT; i++) {
    m_textures[i] = 0;
}
//some code here
for (int i = 1; i <= (TEXTURE_ELEMENTS_COUNT - 1); i++) {
    m_textures[i]->Release(); //it throws an exception right here, but I can't figure out why, I tried change `i` to zero, but it still throws it.
    m_textures[i] = 0;
}

感谢您抽出宝贵的时间浏览我的代码,我不知道自己在做什么错,数组有时使我失望,因为数组的假设是从零开始,有时使我难以想象。 无论如何,感谢您的任何预先输入。

您的元素数为2。因此,您在位置0和位置1处都有一个元素。因此,必须在“ i = 0”处开始循环,并在“ i = 1”后结束循环。 因此,从0开始并运行到“ i <maxCount”。

for (int i = 0; i < TEXTURE_ELEMENTS_COUNT; i++) { //FROM 0 TO i<MAXCOUNT
    m_textures[i] = 0; // HERE YOU CREATE A NULLPTR TO EVERY SRV
}
//some code here
for (int i = 0; i < (TEXTURE_ELEMENTS_COUNT); i++) { //USE THE SAME LOOP
    m_textures[i]->Release(); //IF THERE IS ALREADY A NULLPTR YOU HAVE AN INVALID ACCESS
    m_textures[i] = 0;
}

如果使用SDK,请尝试使用Safe_Release函数。 否则,请自行定义。

SAFE_RELEASE(m_textures[i]) 

有一个针对nullptr的测试:

#ifndef SAFE_RELEASE
#define SAFE_RELEASE(x) 
   if(x != NULL)        
   {                    
     x->Release();     
     x = NULL;         
  }
#endif

祝好运

暂无
暂无

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

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