按照我之前的问题(为什么从TCP套接字读取整数数组时会得到奇怪的结果?),我提出了以下代码,这似乎有用.代码示例适用于少量数组元素,但一旦变大,数据就会在最后被破坏.
这是通过TCP发送int数组的代码:
#define ARRAY_LEN 262144
long *sourceArrayPointer = getSourceArray();
long sourceArray[ARRAY_LEN];
for (int i = 0; i < ARRAY_LEN; i++)
{
sourceArray[i] = sourceArrayPointer[i];
}
int result = send(clientSocketFD, sourceArray, sizeof(long) * ARRAY_LEN);
Run Code Online (Sandbox Code Playgroud)
这是接收int数组的代码:
#define ARRAY_LEN 262144
long targetArray[ARRAY_LEN];
int result = read(socketFD, targetArray, sizeof(long) * ARRAY_LEN);
Run Code Online (Sandbox Code Playgroud)
前几个数字很好,但在数组的下方,数字开始变得完全不同.最后,当数字看起来像这样:
0
0
0
0
0
0
0
0
0
0
Run Code Online (Sandbox Code Playgroud)
但他们实际上是这样出来的?
4310701
0
-12288
32767
-1
-1
10
0
-12288
32767
Run Code Online (Sandbox Code Playgroud)
这是因为我使用了错误的发送/接收大小?
我的应用程序暂停在一行似乎没有任何问题的代码上,但是我的IDE似乎是在该行上挂起并出现错误:
gdb/mi(24/03/09 13:36)(退出.信号'SIGSEGV'收到.说明:分段错误.)
代码行只调用一个没有代码的方法.当您有空引用时,不是分段错误吗?如果是这样,空方法如何具有空引用?
这段代码似乎导致了这个问题:
#include <sys/socket.h>
#define BUFFER_SIZE 256
char *buffer;
buffer = (char*)GetSomePointer()->SomeStackMemoryString.c_str();
int writeResult = write(socketFD, buffer, BUFFER_SIZE);
bzero(buffer, BUFFER_SIZE);
int readResult = read(socketFD, buffer, BUFFER_SIZE);
Run Code Online (Sandbox Code Playgroud)
当使用该read(...)方法的行被注释掉时,问题就会消失.
我已将问题改为指向实际问题,并且已删除所有不相关的代码 - 我也回答了我自己的问题,以便阅读此内容的人明确知道问题所在,请在说"你是"之前阅读我的答案一个白痴!"
我有一个名为ImageMatrix的类,它以递归方式实现C++映射; 最终结果是我有一个3维数组.
typedef uint32_t VUInt32;
typedef int32_t VInt32;
class ImageMatrix
{
public:
ImageMatrixRow operator[](VInt32 rowIndex)
private:
ImageMatrixRowMap rows;
};
typedef std::map <VUInt32, VInt32> ImageMatrixChannelMap;
class ImageMatrixColumn
{
public:
VInt32 &operator[](VUInt32 channelIndex);
private:
ImageMatrixChannelMap channels;
};
typedef std::map<VUInt32, ImageMatrixColumn> ImageMatrixColumnMap;
class ImageMatrixRow
{
public:
ImageMatrixColumn operator[](VUInt32 columnIndex);
private:
ImageMatrixColumnMap columns;
};
typedef std::map<VUInt32, ImageMatrixRow> ImageMatrixRowMap;
Run Code Online (Sandbox Code Playgroud)
每个运算符只返回一个map-wrapper类,如下所示:
ImageMatrixRow ImageMatrix::operator[](VInt32 rowIndex)
{
return rows[rowIndex];
}
ImageMatrixColumn ImageMatrixRow::operator[](VUInt32 columnIndex)
{
return columns[columnIndex];
}
VInt32 &ImageMatrixColumn::operator[](VUInt32 channelIndex)
{
return channels[channelIndex];
}
Run Code Online (Sandbox Code Playgroud)
基本上,当我将值设置为100,并将值测试为cout时,它显示为0,而不是我设置它的数字.
for (VUInt32 a …Run Code Online (Sandbox Code Playgroud) 我想捕捉电视遥控器的输入,并检测我的应用程序中按下了哪些按钮.操作系统是Linux(Windows的答案对我来说不会有太大用处,但可能对其他人有用).我正在使用C++,但C代码也适用于我.
我想以类似于此的方式使用代码:
if (remoteControl.buttonPressed(PLAY_BUTTON))
{
fooBar.doSomethingFun();
}
Run Code Online (Sandbox Code Playgroud)
另外,我在想可能有一个我可以使用的通用库,它可以用于所有遥控器,还是我必须做一些非常低级的编码?
更新:此问题是由内存使用不良引起的,请参阅底部的解决方案.
这是一些半伪代码:
class ClassA
{
public:
virtual void VirtualFunction();
void SomeFunction();
}
class ClassB : public ClassA
{
public:
void VirtualFunction();
}
void ClassA::VirtualFunction()
{
// Intentionally empty (code smell?).
}
void ClassA::SomeFunction()
{
VirtualFunction();
}
void ClassB::VirtualFunction()
{
// I'd like this to be called from ClassA::SomeFunction()
std::cout << "Hello world!" << endl;
}
Run Code Online (Sandbox Code Playgroud)
C#等价物如下:删除了C#示例,因为它与实际问题无关.
ClassB::VirtualFunction调用函数时为什么不调用函数ClassA::SomeFunction?相反ClassA::VirtualFunction被称为......
当我强制实现虚函数 ClassA :: VirtualFunction时,如下所示:
class ClassA
{
public:
virtual void VirtualFunction() = 0; …Run Code Online (Sandbox Code Playgroud) 我正在使用WSGI并尝试使用以下代码访问get/post数据:
import os
import cgi
from traceback import format_exception
from sys import exc_info
def application(environ, start_response):
try:
f = cgi.FieldStorage(fp=os.environ['wsgi.input'], environ=os.environ)
output = 'Test: %s' % f['test'].value
except:
output = ''.join(format_exception(*exc_info()))
status = '200 OK'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Traceback (most recent call last):
File "/srv/www/vm/custom/gettest.wsgi", line 9, in application
f = cgi.FieldStorage(fp=os.environ['wsgi.input'], environ=os.environ)
File "/usr/lib64/python2.4/UserDict.py", line 17, in __getitem__
def __getitem__(self, key): return self.data[key]
KeyError: 'wsgi.input'
Run Code Online (Sandbox Code Playgroud)
是因为我的版本中不存在wsgi.input吗?
我在protobuf Python文档中看到了函数SerializeAsString ,但是像这样建议,这给了我二进制数据的字符串版本。有没有一种方法可以使用Python序列化和解析protobuf数据的二进制数组?
我们有一个C ++应用程序,将protobuf消息作为二进制数据存储在文件中。我们想使用Python读写文件。
用于读取文本文件的cplusplus.com示例显示可以使用该getline函数读取行.但是,我不想得到一整行; 我想只获得一定数量的角色.如何以保留字符编码的方式完成此操作?
我需要一个像这样的函数:
ifstream fileStream;
fileStream.open("file.txt", ios::in);
resultStream << getstring(fileStream, 10); // read first 10 chars
file.ftell(10); // move to the next item
resultStream << getstring(fileStream, 10); // read 10 more chars
Run Code Online (Sandbox Code Playgroud)
我想过读取一个char缓冲区,但这不会改变字符编码吗?
参照对于C O'Reilly的袖珍参考,我由说明书有点混乱为的分组*,/和%运营商.这本书说分组是从左到右发生的 - 现在我觉得我的分组与评估顺序混淆了.考虑到下面的等式,以及从书中建立的规则,我会想到......
int x = 4 / 3 * -3
Run Code Online (Sandbox Code Playgroud)
...评估0,因为......
1: 4 / 3 * -3
2: 4 / -9
3: 0
Run Code Online (Sandbox Code Playgroud)
...但是,它实际上是评估-3,似乎使用这种方法......
1: 4 / 3 * -3
2: 1 * -3
3: -3
Run Code Online (Sandbox Code Playgroud)
这是为什么?