Precision Related Error in C++

Fernando

While building a software I am getting the error:

../src/internet/model/nampt-socket.cc: In member function ‘ns3::Ptr<ns3::NaMPTSubflow> ns3::NaMPTSocket::SwitchToMultipathMode()’:
../src/internet/model/nampt-socket.cc:881:14: error: cast from ‘ns3::NaMPTSocket*’ to ‘uint32_t {aka unsigned int}’ loses precision [-fpermissive]
    (uint32_t)this,
              ^
Waf: Leaving directory `/home/vikas/ns-allinone-3.14.1/ns-3.14.1/build'
Build failed
 -> task in 'ns3-internet' failed (exit status 1): 
    {task 140476688662992: cxx nampt-socket.cc -> nampt-socket.cc.1.o}
['/usr/bin/g++', '-O0', '-ggdb', '-g3', '-Wall', '-Werror', '-Wno-error=deprecated-declarations', '-fstrict-aliasing', '-Wstrict-aliasing', '-fPIC', '-pthread', '-Ibuild', '-I.', '-DNS3_ASSERT_ENABLE', '-DNS3_LOG_ENABLE', '-DSQLITE3=1', '-DHAVE_IF_TUN_H=1', '-DENABLE_GSL', '../src/internet/model/nampt-socket.cc', '-c', '-o', 'src/internet/model/nampt-socket.cc.1.o']
Traceback (most recent call last):
  File "./build.py", line 214, in <module>
    sys.exit(main(sys.argv))
  File "./build.py", line 205, in main
    build_ns3(config, build_examples, build_tests, args, build_options)
  File "./build.py", line 98, in build_ns3
    run_command([sys.executable, "waf", "build"] + build_options)
  File "/home/vikas/ns-allinone-3.14.1/util.py", line 24, in run_command
    raise CommandError("Command %r exited with code %i" % (argv, retval))
util.CommandError: Command ['/usr/bin/python', 'waf', 'build'] exited with code 1

The error is that :

cast from ‘ns3::NaMPTSocket*’ to ‘uint32_t {aka unsigned int}’ loses precision [-fpermissive]

I searched for the error but using uintptr_t is not possible in my case. Is any other solution possible ?

THe code where the error comes from is:

Ptr<NaMPTSubflow> 
NaMPTSocket::SwitchToMultipathMode(void)
{
  NS_ASSERT(m_firstSocket != 0);
  NS_ASSERT(m_firstSocket->m_state == SYN_SENT || m_firstSocket->m_state == SYN_RCVD);
  NS_ASSERT(m_firstSocket->m_shellSocket == this);

  // change to NaMPTCC
  // the original socket will be discarded from m_tcp->m_sockets
  m_socketTypeId = NaMPTCC::GetTypeId();
  m_firstSocket = NaMPTSocket::ConvertCongestionControl(m_firstSocket);

  // subflow
  Ptr<NaMPTSubflow> sf = SetupSubflow(m_firstSocket, true);
  sf->m_bBypassSending = true;
  NS_ASSERT(sf->m_baseSocket == m_firstSocket);

  // session name: time-socket-src-des
  if (m_timeTag == 0)
    m_timeTag = Simulator::Now().GetSeconds();
  char buf[128];
  snprintf(buf, 128, "%.6f-%08x-%u.%u.%u.%u->%u.%u.%u.%u", 
            m_timeTag, 
            (uint32_t)this,
            (sf->m_localAddress.Get() >> 24)&0x000000FF,
            (sf->m_localAddress.Get() >> 16)&0x000000FF,
            (sf->m_localAddress.Get() >> 8)&0x000000FF,
            (sf->m_localAddress.Get() )&0x000000FF,
            (sf->m_peerAddress.Get() >> 24)&0x000000FF,
            (sf->m_peerAddress.Get() >> 16)&0x000000FF,
            (sf->m_peerAddress.Get() >> 8)&0x000000FF,
            (sf->m_peerAddress.Get() )&0x000000FF );
  m_sessionName = buf;

  // init connection
  m_localPort = m_firstSocket->m_endPoint->GetLocalPort();
  m_peerPort = m_firstSocket->m_endPoint->GetPeerPort();
  m_nextTx = m_firstSocket->m_nextTxSequence;
  m_nextTx ++; // syn consumes one byte
  m_inputQueue.SetHeadSequence(m_nextTx);

  // next used addr
  AddressSet_t::const_iterator it = m_addresses.find(sf->m_localAddress.Get());
  it++;
  if (it == m_addresses.end() )
    it = m_addresses.begin();
  m_nextAddr = it->first;

  // m_outputQueue is initialized after this function returns.

  m_firstSocket = 0; // this is important...
  return sf;
}
Moby Disk

I believe the correct way to printf a pointer is to use %p. So you want to change the code to this:

snprintf(buf, 128, "%.6f-%p-%u.%u.%u.%u->%u.%u.%u.%u", 
            m_timeTag, 
            (void *)this,
            (sf->m_localAddress.Get() >> 24)&0x000000FF,
            (sf->m_localAddress.Get() >> 16)&0x000000FF,
            (sf->m_localAddress.Get() >> 8)&0x000000FF,
            (sf->m_localAddress.Get() )&0x000000FF,
            (sf->m_peerAddress.Get() >> 24)&0x000000FF,
            (sf->m_peerAddress.Get() >> 16)&0x000000FF,
            (sf->m_peerAddress.Get() >> 8)&0x000000FF,
            (sf->m_peerAddress.Get() )&0x000000FF );

This avoids the need to cast to any kind of integer. %p also formats in hexadecimal while avoiding the need to specify the field size in the printf statement. It will automatically adjust the size when compiling for 32-bit or 64-bit.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related