c++ - 字符串赋值错误

标签 c++ networking ns-3

我正在编写一个 ns3 应用程序,为此我需要将一个 vector 写入一个文件,读取该文件以再次构建该 vector 并从该 vector 中选取随机元素。这是代码:

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/mf-helper.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include "ns3/data-rate.h"

#include "ns3/mobility-module.h"
#include "ns3/wifi-module.h"
#include "ns3/ideal-wifi-manager.h"
#include "ns3/wifi-remote-station-manager.h"
#include "ns3/wifi-mode.h"
using namespace ns3;
using namespace std;

void writeFile(string, vector<string>);
void readFile(string, vector<string> &);
unsigned int Random(int,int);
bool Find(vector<string> , string);
void selectNodes(vector<string>);

vector<string> senders;

int main(int argc, char **argv)
{
    vector<string> vect;
    vect.push_back("10.1.1.1");
    vect.push_back("10.1.1.2");
    vect.push_back("10.1.1.3");
    vect.push_back("10.1.1.4");
    vect.push_back("10.1.1.5");
    vect.push_back("10.1.1.6");
    vect.push_back("10.1.1.7");

    writeFile("data.txt", vect);

    vector<string> ret;
    readFile("data.txt",ret);
    selectNodes(ret);
}

void writeFile(string name, vector<string> vs)
{
    ofstream outfile(name.c_str(), ios::out);
    ostream_iterator<string> oi(outfile, "\n");
    copy(vs.begin(), vs.end(), oi);
}

void readFile(string name, vector<string> &vect)
{   
    ifstream file(name.c_str());
    copy(istream_iterator<string> (file), istream_iterator<string>(), back_inserter(vect));
}

void selectNodes(vector<string> ret)
{
    srand(time(NULL));

    string src;
    string dest;

    unsigned int s= ret.size();
    src = ret[Random(1,s)];
    dest = ret[Random(1,s)];


    while(Find(senders, src))
    {
        src = ret[Random(1,s)];
    }

    while (src == dest)
    {
        src = ret[Random(1,s)];
        if (dest != src)
            break;
    }

    cout << "##Source: " << src << std::endl;
    cout << "##Destination: " << dest << std::endl;

    senders.push_back(src);
}

unsigned int Random(int nLow, int nHigh)
{
    return (rand() % (nHigh - nLow + 1)) + nLow;
}

bool Find(vector<string> senders, string addr)
{
    for(unsigned int i=0;i<senders.size();i++)
        if(senders[i] == addr)
            return 1;
    return 0;
}

此代码随机崩溃。这是 gdb 所说的

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xfffffffffffffff8
0x00007fff8ad5a220 in std::string::_Rep::_M_grab ()
(gdb) bt
#0  0x00007fff8ad5a220 in std::string::_Rep::_M_grab ()
#1  0x00007fff8ad5a29b in std::string::assign ()
#2  0x0000000100002a31 in selectNodes (ret=@0x7fff5fbff7c0) at test_write.cc:74
#3  0x0000000100003cf5 in main (argc=1, argv=0x7fff5fbff998) at test_write.cc:49

为什么字符串赋值失败?我发现有些人由于内存泄漏而遇到这个问题。但这里似乎并非如此。我错过了什么吗?

最佳答案

这些行有问题:

 src = ret[Random(1,s)];
 dest = ret[Random(1,s)];

因为 Random 返回的值可能等于 s 超出范围。 indexret 的最大值是 s-1

所以解决方案是,要么你这样写:

 src = ret[Random(1,s-1)];
 dest = ret[Random(1,s-1)];

或者,将Random定义为:

unsigned int Random(int nLow, int nHigh)
{
    return (rand() % (nHigh - nLow + 1)) + nLow  - 1;
}

我建议您按照上面的建议重新定义 Random,因为从数学上讲 Random 生成的值落在 [ n低,n高)。 Dikkstra 对此提供了合理的论据。读这个:

顺便说一句,你应该通过reference接受 vector 参数。

关于c++ - 字符串赋值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8381942/

相关文章:

MacOSX : how to capture network events? 或 : how does Private-Eye works?

linux - NS-3 dce 和 iperf3

c++ - 编译程序以使用 Basler 相机

c++ - 代码不会使用 Eigen 库语法问题进行编译?

c++ - C++ | BST对节点指针的引用与节点指针

networking - Rust Alpha 和 Old_IO

Android Socket 高 CPU 使用率

c++ - 在 C++ 中将字符串转换为 uint8_t 数组

c++ - 错误:/usr/bin/ld: final link failed: File truncated

c++ - 是否可以在 C++ 中为嵌入式平台创建新的原始类型?