Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9210685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:08:03+00:00 2026-06-18T01:08:03+00:00

I have a strange situation where using std::copy is causing a seg fault where

  • 0

I have a strange situation where using std::copy is causing a seg fault where as using memcpy appears to work correctly.

I am creating a PacketHeader object inside a Google Test function.

I have confirmed that the data going into the object is valid so I’m probably being stupid and have missed something obvious with the std::copy syntax.

GCC version : 4.4.6 20120305 (Red Hat 4.4.6-4)
GTest version : 1.6.0

Also linking with boost::options and libpcap

TEST(SetIP, GoodVals)
{


    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_t * handle = pcap_open_offline("./test_files/PCAP_Good.pcap", errbuf);

    pcap_pkthdr * header;
    const unsigned char * pkt_data;

    //Load one packet from the PCAP, doesn't matter what
    pcap_next_ex(handle, &header, &pkt_data);

    PacketHeader p(header);

}


PacketHeader::PacketHeader(const struct pcap_pkthdr * aPacketHeader) : prPacketHeader(NULL)
{
    prPacketHeader = new struct pcap_pkthdr;

    //This doesn't work
    //std::copy(aPacketHeader, aPacketHeader + sizeof(struct pcap_pkthdr), prPacketHeader);

    //This works
    memcpy(prPacketHeader, aPacketHeader, sizeof(struct pcap_pkthdr));

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:08:05+00:00Added an answer on June 18, 2026 at 1:08 am

    The expression

    aPacketHeader + sizeof(struct pcap_pkthdr)
    

    doesn’t do what you expect it to. It actually adds sizeof(pcap_pkthdr) * sizeof(pcap_pkthdr) to the pointer base address.

    Remember that aPointer + something is the same as aPointer[something].

    Either add only 1:

    aPacketHeader + 1
    

    or typecast to char*:

    reinterpret_cast<char*>(aPacketHeader) + sizeof(struct pcap_pkthdr)
    

    The obvious solution should of course to use simple struct-to-struct copying:

    *prPacketHeader = *aPacketHeader;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange situation where using @parent, or even explicit id-s dont work
I have a strange situation using asp ajax chart controls. I have an application
I have a strange situation which appears to indicate a GORM cacheing problem //begin
i have a strange situation: I'm using jpa/hibernate to get rows from a mySql
I have a strange situation. I'm using NexPlayer to render streaming video in openGL
I have a strange situation using SetWindowsHookEx I have a bcb 2009 project with
I have a strange (looks to me) and simple memory leak situation using ARC
I really have a strange situation. I'm making a Linux multi-threaded C application using
I have a strange situation in regards to xml serialization... If I run MyApp.exe
I have a strange situation here. In my android app, I have a layout

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.