Shared pointee is not the same in the caller and the callee - c++
I am working on a module of the ns-3 simulator. For people unfamiliar with it, they exhibit a custom-made shared pointer: Ptr<T>. These objects are usually created using the CreateObject<T>(…) function which basically calls new T(…) and wraps it in a Ptr<T>.
I’m having troubles using one of these that I created in a constructor:
SatUtHelper::SatUtHelper (SatTypedefs::CarrierBandwidthConverter_t carrierBandwidthConverter,
uint32_t fwdLinkCarrierCount,
Ptr<SatSuperframeSeq> seq,
SatMac::ReadCtrlMsgCallback readCb,
SatMac::ReserveCtrlMsgCallback reserveCb,
SatMac::SendCtrlMsgCallback sendCb,
RandomAccessSettings_s randomAccessSettings)
: m_superframeSeq (seq),
m_llsConf (),
[...snip...]
{
[...snip...]
m_llsConf = CreateObject<SatLowerLayerServiceConf> ();
}
and that I use like so:
Ptr<SatRandomAccessConf> randomAccessConf = CreateObject<SatRandomAccessConf> (m_llsConf, m_superframeSeq);
The SatRandomAccessConf constructor begins like so:
SatRandomAccessConf::SatRandomAccessConf (Ptr<SatLowerLayerServiceConf> llsConf, Ptr<SatSuperframeSeq> superframeSeq)
: m_configurationIdPerAllocationChannel (),
m_slottedAlohaControlRandomizationIntervalInMilliSeconds (),
m_allocationChannelCount (llsConf->GetRaServiceCount ()),
m_crdsaSignalingOverheadInBytes (5),
m_slottedAlohaSignalingOverheadInBytes (3)
{
NS_LOG_FUNCTION (this);
if (m_allocationChannelCount < 1)
{
NS_FATAL_ERROR ("SatRandomAccessConf::SatRandomAccessConf - No random access allocation channel");
}
m_slottedAlohaControlRandomizationIntervalInMilliSeconds = (llsConf->GetDefaultControlRandomizationInterval ()).GetMilliSeconds ();
DoSlottedAlohaVariableSanityCheck ();
and contains some checks past that point which blows up due to having llsConf in an incoherent state.
I have used GDB to inspect the state of the llsConf object:
I put a breakpoint on the DoSlottedAlohaVariableSanityCheck ();
I called methods on llsConf that indeed returned garbage values;
I checked values of m_allocationChannelCount and m_slottedAlohaControlRandomizationIntervalInMilliSeconds that were not the same as the return values from the methods of llsConf;
I checked in upper frames the values on m_llsConf which were as expected;
I checked the content of the objects in both frames that differed in the caller and the callee; the content of llsConf having a suspiciously large amount of attributes set to 0.
Relevant parts of the GDB session:
(gdb) bt
#0 ns3::SatRandomAccessConf::DoSlottedAlohaVariableSanityCheck (this=0x55555a160a00) at ../contrib/satellite/model/satellite-random-access-container-conf.cc:146
#1 0x00007ffff76fe0e1 in ns3::SatRandomAccessConf::SatRandomAccessConf (this=0x55555a160a00, llsConf=..., superframeSeq=...) at ../contrib/satellite/model/satellite-random-access-container-conf.cc:75
#2 0x00007ffff781fa26 in ns3::CreateObject<ns3::SatRandomAccessConf, ns3::Ptr<ns3::SatLowerLayerServiceConf>, ns3::Ptr<ns3::SatSuperframeSeq> > (a1=..., a2=...) at ./ns3/object.h:557
#3 0x00007ffff781d494 in ns3::SatUtHelper::Install (this=0x555555841990, n=..., beamId=10, fCh=..., rCh=..., gwNd=..., ncc=..., cbChannel=..., cbRouting=...) at ../contrib/satellite/helper/satellite-ut-helper.cc:425
#4 0x00007ffff781b307 in ns3::SatUtHelper::Install (this=0x555555841990, c=..., beamId=10, fCh=..., rCh=..., gwNd=..., ncc=..., cbChannel=..., cbRouting=...) at ../contrib/satellite/helper/satellite-ut-helper.cc:231
#5 0x00007ffff77b03bc in ns3::SatBeamHelper::Install (this=0x5555557ecb20, ut=..., gwNode=..., gwId=3, beamId=10, ulFreqId=3, flFreqId=1, routingCallback=...) at ../contrib/satellite/helper/satellite-beam-helper.cc:475
#6 0x00007ffff77f0262 in ns3::SatHelper::DoCreateScenario (this=0x5555557ec810, beamInfos=std::map with 0 elements, gwUsers=1) at ../contrib/satellite/helper/satellite-helper.cc:566
#7 0x00007ffff77ee873 in ns3::SatHelper::CreateUserDefinedScenario (this=0x5555557ec810, infos=std::map with 0 elements) at ../contrib/satellite/helper/satellite-helper.cc:389
#8 0x00007ffff7844e0b in ns3::SimulationHelper::CreateSatScenario (this=0x5555557ec5a0, scenario=ns3::SatHelper::NONE, mobileUtsFolder="contrib/satellite/data/utpositions/mobiles/")
at ../contrib/satellite/helper/simulation-helper.cc:1170
#9 0x00007ffff784a1dc in ns3::SimulationHelper::ConfigureAttributesFromFile (this=0x5555557ec5a0, filePath="/tmp/sns-3-qs4ebg47/parameters.xml", overrideManualConfiguration=true) at ../contrib/satellite/helper/simulation-helper.cc:1555
#10 0x0000555555557225 in main (argc=3, argv=0x7fffffffe798) at ../contrib/satellite/examples/sat-generic-launcher.cc:60
(gdb) p llsConf->GetRaServiceCount()
$1 = 0 '\000'
(gdb) p llsConf->GetDefaultControlRandomizationInterval()
$2 = {static g_markingTimes = 0x7ffff39447a0 <ns3::Time::StaticInit()::markingTimes>, m_data = 93824994540112}
(gdb) p llsConf->GetDefaultControlRandomizationInterval().GetMilliSeconds()
$3 = 93824994
(gdb) p m_allocationChannelCount
$4 = 1
(gdb) p m_slottedAlohaControlRandomizationIntervalInMilliSeconds
$5 = 100
(gdb) p llsConf
$6 = {m_ptr = 0x7fffffffd718}
(gdb) p *(llsConf.m_ptr)
$7 = {<ns3::Object> = {<ns3::SimpleRefCount<ns3::Object, ns3::ObjectBase, ns3::ObjectDeleter>> = {<ns3::ObjectBase> = {_vptr.ObjectBase = 0x5555557af810}, m_count = 1434627024}, m_tid = {m_tid = 21845}, m_disposed = false,
m_initialized = false, m_aggregates = 0x91c8f8cb8ee26900, m_getObjectCount = 0}, static m_minDaServiceEntries = 2 '\002', static m_minRaServiceEntries = 1 '\001', static m_maxDaServiceEntries = 4 '\004',
static m_maxRaServiceEntries = 3 '\003', m_dynamicRatePersistence = 0 '\000', m_volumeBacklogPersistence = 0 '\000', m_defaultControlRandomizationInterval = {
static g_markingTimes = 0x7ffff39447a0 <ns3::Time::StaticInit()::markingTimes>, m_data = 93824994540112}, m_daServiceEntryCount = 102 'f', m_daServiceEntries = {{m_constantAssignmentProvided = false, m_rbdcAllowed = false,
m_volumeAllowed = false, m_constantServiceRateStream = {m_ptr = 0x7fffffffdb50}, m_maximumServiceRateKbps = 54420, m_minimumServiceRateKbps = 63361, m_maximumBacklogInKbytes = 32767}, {m_constantAssignmentProvided = 54,
m_rbdcAllowed = 62, m_volumeAllowed = 103, m_constantServiceRateStream = {m_ptr = 0x0}, m_maximumServiceRateKbps = 51468, m_minimumServiceRateKbps = 63349, m_maximumBacklogInKbytes = 32767}, {m_constantAssignmentProvided = false,
m_rbdcAllowed = false, m_volumeAllowed = false, m_constantServiceRateStream = {m_ptr = 0x7ffff7710ce6 <ns3::SatRequestManager::AssignedDaResources(unsigned char, unsigned int)>}, m_maximumServiceRateKbps = 0,
m_minimumServiceRateKbps = 0, m_maximumBacklogInKbytes = 0}, {m_constantAssignmentProvided = 192, m_rbdcAllowed = 189, m_volumeAllowed = 112, m_constantServiceRateStream = {m_ptr = 0x0}, m_maximumServiceRateKbps = 65,
m_minimumServiceRateKbps = 0, m_maximumBacklogInKbytes = 0}}, m_raServiceIndexDefault = 0 '\000', m_raServiceEntryCount = 0 '\000', m_raServiceEntries = {{m_maxUniquePayloadPerBlock = 118 'v',
m_maxConsecutiveBlockAccessed = 130 '\202', m_minimumIdleBlock = 112 'p', m_backOffTimeInMilliSeconds = 32767, m_highLoadBackOffTimeInMilliSeconds = 0, m_backOffProbability = 0, m_highLoadBackOffProbability = 0,
m_numberOfInstances = 0 '\000', m_averageNormalizedOfferedLoadThreshold = 6.9533487051292957e-310, m_isCrdsaAllowed = false, m_isSlottedAlohaAllowed = false}, {m_maxUniquePayloadPerBlock = 54 '6',
m_maxConsecutiveBlockAccessed = 62 '>', m_minimumIdleBlock = 103 'g', m_backOffTimeInMilliSeconds = 32767, m_highLoadBackOffTimeInMilliSeconds = 0, m_backOffProbability = 0, m_highLoadBackOffProbability = 0,
m_numberOfInstances = 0 '\000', m_averageNormalizedOfferedLoadThreshold = 7.5592043813710721e-322, m_isCrdsaAllowed = false, m_isSlottedAlohaAllowed = false}, {m_maxUniquePayloadPerBlock = 113 'q',
m_maxConsecutiveBlockAccessed = 0 '\000', m_minimumIdleBlock = 0 '\000', m_backOffTimeInMilliSeconds = 0, m_highLoadBackOffTimeInMilliSeconds = 0, m_backOffProbability = 0, m_highLoadBackOffProbability = 0,
m_numberOfInstances = 0 '\000', m_averageNormalizedOfferedLoadThreshold = 6.9533487349484301e-310, m_isCrdsaAllowed = false, m_isSlottedAlohaAllowed = false}}}
(gdb) up
#2 0x00007ffff781fa26 in ns3::CreateObject<ns3::SatRandomAccessConf, ns3::Ptr<ns3::SatLowerLayerServiceConf>, ns3::Ptr<ns3::SatSuperframeSeq> > (a1=..., a2=...) at ./ns3/object.h:557
557 return CompleteConstruct (new T (a1,a2));
(gdb) up
#3 0x00007ffff781d494 in ns3::SatUtHelper::Install (this=0x555555841990, n=..., beamId=10, fCh=..., rCh=..., gwNd=..., ncc=..., cbChannel=..., cbRouting=...) at ../contrib/satellite/helper/satellite-ut-helper.cc:425
425 Ptr<SatRandomAccessConf> randomAccessConf = CreateObject<SatRandomAccessConf> (m_llsConf, m_superframeSeq);
(gdb) p m_llsConf->GetRaServiceCount()
$8 = 1 '\001'
(gdb) p m_llsConf->GetDefaultControlRandomizationInterval().GetMilliSeconds()
$9 = 100
(gdb) p m_llsConf
$10 = {m_ptr = 0x5555557af810}
(gdb) p *(m_llsConf.m_ptr)
$11 = {<ns3::Object> = {<ns3::SimpleRefCount<ns3::Object, ns3::ObjectBase, ns3::ObjectDeleter>> = {<ns3::ObjectBase> = {_vptr.ObjectBase = 0x7ffff7d93850 <vtable for ns3::SatLowerLayerServiceConf+16>}, m_count = 6}, m_tid = {
m_tid = 406}, m_disposed = false, m_initialized = false, m_aggregates = 0x5555564d2540, m_getObjectCount = 0}, static m_minDaServiceEntries = 2 '\002', static m_minRaServiceEntries = 1 '\001',
static m_maxDaServiceEntries = 4 '\004', static m_maxRaServiceEntries = 3 '\003', m_dynamicRatePersistence = 5 '\005', m_volumeBacklogPersistence = 7 '\a', m_defaultControlRandomizationInterval = {
static g_markingTimes = 0x7ffff39447a0 <ns3::Time::StaticInit()::markingTimes>, m_data = 100000000}, m_daServiceEntryCount = 4 '\004', m_daServiceEntries = {{m_constantAssignmentProvided = false, m_rbdcAllowed = false,
m_volumeAllowed = false, m_constantServiceRateStream = {m_ptr = 0x5555561d07d0}, m_maximumServiceRateKbps = 9216, m_minimumServiceRateKbps = 10, m_maximumBacklogInKbytes = 384}, {m_constantAssignmentProvided = false,
m_rbdcAllowed = false, m_volumeAllowed = false, m_constantServiceRateStream = {m_ptr = 0x555555e48b20}, m_maximumServiceRateKbps = 9216, m_minimumServiceRateKbps = 10, m_maximumBacklogInKbytes = 384}, {
m_constantAssignmentProvided = false, m_rbdcAllowed = false, m_volumeAllowed = false, m_constantServiceRateStream = {m_ptr = 0x55555581d600}, m_maximumServiceRateKbps = 9216, m_minimumServiceRateKbps = 10,
m_maximumBacklogInKbytes = 384}, {m_constantAssignmentProvided = false, m_rbdcAllowed = false, m_volumeAllowed = false, m_constantServiceRateStream = {m_ptr = 0x5555557a95f0}, m_maximumServiceRateKbps = 9216,
m_minimumServiceRateKbps = 16, m_maximumBacklogInKbytes = 384}}, m_raServiceIndexDefault = 0 '\000', m_raServiceEntryCount = 1 '\001', m_raServiceEntries = {{m_maxUniquePayloadPerBlock = 1 '\001',
m_maxConsecutiveBlockAccessed = 6 '\006', m_minimumIdleBlock = 0 '\000', m_backOffTimeInMilliSeconds = 150, m_highLoadBackOffTimeInMilliSeconds = 500, m_backOffProbability = 1, m_highLoadBackOffProbability = 1,
m_numberOfInstances = 3 '\003', m_averageNormalizedOfferedLoadThreshold = 0.98999999999999999, m_isCrdsaAllowed = true, m_isSlottedAlohaAllowed = true}, {m_maxUniquePayloadPerBlock = 3 '\003',
m_maxConsecutiveBlockAccessed = 4 '\004', m_minimumIdleBlock = 2 '\002', m_backOffTimeInMilliSeconds = 250, m_highLoadBackOffTimeInMilliSeconds = 500, m_backOffProbability = 10000, m_highLoadBackOffProbability = 30000,
m_numberOfInstances = 2 '\002', m_averageNormalizedOfferedLoadThreshold = 0.5, m_isCrdsaAllowed = true, m_isSlottedAlohaAllowed = true}, {m_maxUniquePayloadPerBlock = 3 '\003', m_maxConsecutiveBlockAccessed = 4 '\004',
m_minimumIdleBlock = 2 '\002', m_backOffTimeInMilliSeconds = 250, m_highLoadBackOffTimeInMilliSeconds = 500, m_backOffProbability = 10000, m_highLoadBackOffProbability = 30000, m_numberOfInstances = 5 '\005',
m_averageNormalizedOfferedLoadThreshold = 0.5, m_isCrdsaAllowed = true, m_isSlottedAlohaAllowed = true}}}
Now if I add the line Ptr<SatLowerLayerServiceConf> uglyHack = llsConf; before the breakpoint, I can in fact see that, somehow, they are equal:
(gdb) p uglyHack
$1 = {m_ptr = 0x5555557af810}
(gdb) p llsConf
$2 = {m_ptr = 0x7fffffffd718}
(gdb) up
#2 0x00007ffff781fa56 in ns3::CreateObject<ns3::SatRandomAccessConf, ns3::Ptr<ns3::SatLowerLayerServiceConf>, ns3::Ptr<ns3::SatSuperframeSeq> > (a1=..., a2=...) at ./ns3/object.h:557
557 return CompleteConstruct (new T (a1,a2));
(gdb)
#3 0x00007ffff781d4c4 in ns3::SatUtHelper::Install (this=0x555555841990, n=..., beamId=10, fCh=..., rCh=..., gwNd=..., ncc=..., cbChannel=..., cbRouting=...) at ../contrib/satellite/helper/satellite-ut-helper.cc:425
425 Ptr<SatRandomAccessConf> randomAccessConf = CreateObject<SatRandomAccessConf> (m_llsConf, m_superframeSeq);
(gdb) p m_llsConf
$3 = {m_ptr = 0x5555557af810}
Does someone have any idea how the parameter passing and the copy assignment can allow such behaviour?
Related
How to read the structure values in gdb?
For example, when I type p dest-msgs[count] The following values are displayed. How to understand the significance of these values ? p dest->msgs[count] $9 = {hdr = {ver = 5 '\005', magic = 18 '\022', evt_len = 588}, evt = {hdr = {ver = 3 '\003', magic = 18 '\022', evt_len = 552}, service_id = 1, instance = 0, comp_id = -2136604671, comp_name = "tsd", '\0' <repeats 14 times>, flags = 0, objid = 0, file = "ts.c", '\0' <repeats 11 times>, func = "tsTimeMcast_h\000\000", line = 1667, local_time_sec = 1483252020, local_time_usec = 28575, time_sec = 1483252020, time_usec = 28675, global_seq = 4014, external_seq = 2146, iq_drop = 0, rq_drop = 0, arg_offset = "\000\002\005\006", '\0' <repeats 11 times>, arg = "LOCL\000\000\000\000External\000\000\000\000EXT\000LOCL", '\0' <repeats 227 times>, msgdef = {msg_id = 0, attributes = 0, class = 0, severity = 0, msg_value = "\000\000\000\000\000\000\000\000\000", message = '\0' <repeats 128 times>, arg_type = 0, arg_num = 0, cat_name = "\000\000\000\000\000\000\000"}}, name = '\0' <repeats 31 times>}
Let's see what we have here: p dest->msgs[count] So, you print one particular element from an array, it seems. Gdb responds: $9 = { This $9 is just an artificial name that you could use later to refer to this result. The result begins with an opening brace. This means that it is a structure. It goes on: hdr = {ver = 5 '\005', magic = 18 '\022', evt_len = 588}, The first member of the structure has the name hdr and its value is, see the next opening brace, again a structure. This nested structure has three members (the closing brace tells us where we have to stop counting) named ver, magic, and evt_len. You see their respective values. Then follows evt = {hdr = {ver = ... that is, evt is the second member of the outer structure, which is again a structure, which has member hdr, which is again a structure. Et cetera.
change label values when an entry value is changed
My problem at the moment is I am trying to change a label(label 16) to the first value of entry_values[0] which isn't working I have tried passing it in as a variable and many other things, after about an hour of research I couldn't find a solution.I think the main problem is that it sets the label before the code with the entry is run so that it wont change. when I set it to a textvariable it produces an empty string (I think) but when I use just text it puts in a 0 where I expect my number. def sub_menu(root): global subpage subpage = Frame(root) button5 = Button(subpage, text="Save Generation Data", command = lambda: save_entries()) button5.grid(row = 1, column = 6, sticky = E) button6 = Button(subpage, text="Return To Main Page", command = lambda: switch_page("main")) button6.grid(row = 0, column = 6, sticky = W) juveniles_label0 = Label(subpage,text="Juveniles") adults_label1 = Label(subpage,text="Adults") seniles_label2 = Label(subpage,text="Seniles") population_label3 = Label(subpage,text="Population (Thousands)") survival_rate_label4 = Label(subpage,text="Survival Rate (Between 0 and 1)") birth_rate_label5 = Label(subpage,text="Birth Rate") number_of_gens_label6 = Label(subpage,text="Number of Generations") disease_trigger_label7 = Label(subpage,text="Disease Trigger Point") global entry0 entry0 = Entry(subpage) global entry1 entry1 = Entry(subpage) global entry2 entry2 = Entry(subpage) global entry3 entry3 = Entry(subpage) global entry4 entry4 = Entry(subpage) global entry5 entry5 = Entry(subpage) global entry6 entry6 = Entry(subpage) global entry7 entry7 = Entry(subpage) global entry8 entry8 = Entry(subpage) juveniles_label0.grid(row = 0, column = 1) adults_label1.grid(row = 0, column = 2) seniles_label2.grid(row = 0, column = 3) population_label3.grid(row = 1, column = 0) survival_rate_label4.grid(row = 2, column = 0) birth_rate_label5.grid(row = 3, column = 0) number_of_gens_label6.grid(row = 3, column = 2) disease_trigger_label7.grid(row = 4, column = 0) entry0.grid(row = 1, column = 1) entry1.grid(row = 1, column = 2) entry2.grid(row = 1, column = 3) entry3.grid(row = 2, column = 1) entry4.grid(row = 2, column = 2) entry5.grid(row = 2, column = 3) entry6.grid(row = 3, column = 1) entry7.grid(row = 3, column = 3) entry8.grid(row = 4, column = 1) return subpage def save_entries(): #entry recieve point save_page = Frame(root) """ if e0 < 0: make a check to check if value is < 0 dont accept and if a value is inputed or not using if type(string_name) == str """ e0 = entry0.get() if e0 >= 0: entry_values[0] = (e0) e1 = entry1.get() if e0 >= 0: entry_values[1] = (e1) e2 = entry2.get() if e0 >= 0: entry_values[2] = (e2) e3 = entry3.get() if e0 >= 0: entry_values[3] = (e3) e4 = entry4.get() if e0 >= 0: entry_values[4] = (e4) e5 = entry5.get() if e0 >= 0: entry_values[5] = (e5) e6 = entry6.get() if e0 >= 0: entry_values[6] = (e6) e7 = entry7.get() if e0 >= 0: entry_values[7] = (e7) e8 = entry8.get() if e0 >= 0: entry_values[8] = (e8) print entry_values return save_page def display_values(root): sub2 = Frame(root) global entry_values label8 = Label(sub2, text = "Juveniles") label9 = Label(sub2, text = "Adults") label10 = Label(sub2, text = "Seniles") label11 = Label(sub2, text = "Population(Thousands)") label12 = Label(sub2, text = "Survival Rate(Between 1 and 0)") label13 = Label(sub2, text = "Birth Rate") label14 = Label(sub2, text = "Number of Generations") label15 = Label(sub2, text = "Disase Trigger Point") label16 = Label(sub2, text = entry_values[0]) label17 = Label(sub2, textvariable = entry_values[1]) label18 = Label(sub2, textvariable = "") label19 = Label(sub2, textvariable = "") label20 = Label(sub2, textvariable = "") label21 = Label(sub2, textvariable = "") label22 = Label(sub2, textvariable = "") label23 = Label(sub2, textvariable = "") label24 = Label(sub2, textvariable = "") button7 = Button(sub2, text="Return To Main Page", command = lambda: switch_page("main")) label8.grid(row = 0, column = 1) label9.grid(row = 0, column = 2) label10.grid(row = 0, column = 3) label11.grid(row = 1, column = 0) label12.grid(row = 2, column = 0) label13.grid(row = 3, column = 0) label14.grid(row = 3, column = 3) label15.grid(row = 4, column = 0) label16.grid(row = 1, column = 1) label17.grid(row = 1, column = 2) label18.grid(row = 1, column = 3) label19.grid(row = 2, column = 1) label20.grid(row = 2, column = 2) label21.grid(row = 2, column = 3) label22.grid(row = 3, column = 1) label23.grid(row = 3, column = 3) label24.grid(row = 4, column = 1) button7.grid(row = 0, column = 0) return sub2
In order to change the text of a label you can do: label["text"] = textVar or label.config(text=textVar) So in your above code, when the entry changes, reconfigure the label using one of the above options.
Finding whether a number has P^Q form or not?
I have recently appeared online coding Test. I was struck one question i.e A number N is given finding the above number is P^Q(P power Q) form or not. I did the question using Brute force method (satisfying for individual number) but that result in time out. SO I need Efficient algorithm. Input: 9 out put : yes Input: 125 out put : yes Input: 27 out put : yes Constraints: 2<N<100000
if we assume non trivial cases then the constraints would be something like this: N = <2,100000) P>1 Q>1 This can be solved by sieves that mark all powers bigger then 1 up to N of the result. Now the question is do you need to optimize single query or many of them ? If you need just single query then you do not need the sieve table in memory, you just iterate until hit the N and then stop (so in worst case when N is not in form P^Q this would compute the whole sieve). Otherwise init such table once and then just use it. As N is small I go for the full table. const int n=100000; int sieve[n]={255}; // for simplicity 1 int/number but it is waste of space can use 1 bit per number instead int powers(int x) { // init sieve table if not already inited if (sieve[0]==255) { int i,p; for (i=0;i<n;i++) sieve[i]=0; // clear sieve for (p=sqrt(n);p>1;p--) // process all non trivial P for (i=p*p;i<n;i*=p) // go through whole table sieve[i]=p; // store P so it can be easily found later (if use 1bit/number then just set the bit instead) } return sieve[x]; } first call took 0.548 ms on mine setup the others are non measurable small times it returns the P so if P!=0 the number is in form P^Q so you can use it as bool directly, and also you can easily get Q by dividing or you can create another sieve with Q to be even more fast if you need also the P,Q Here all found non trivial powers N<100000 4 = 2^q 8 = 2^q 9 = 3^q 16 = 2^q 25 = 5^q 27 = 3^q 32 = 2^q 36 = 6^q 49 = 7^q 64 = 2^q 81 = 3^q 100 = 10^q 121 = 11^q 125 = 5^q 128 = 2^q 144 = 12^q 169 = 13^q 196 = 14^q 216 = 6^q 225 = 15^q 243 = 3^q 256 = 2^q 289 = 17^q 324 = 18^q 343 = 7^q 361 = 19^q 400 = 20^q 441 = 21^q 484 = 22^q 512 = 2^q 529 = 23^q 576 = 24^q 625 = 5^q 676 = 26^q 729 = 3^q 784 = 28^q 841 = 29^q 900 = 30^q 961 = 31^q 1000 = 10^q 1024 = 2^q 1089 = 33^q 1156 = 34^q 1225 = 35^q 1296 = 6^q 1331 = 11^q 1369 = 37^q 1444 = 38^q 1521 = 39^q 1600 = 40^q 1681 = 41^q 1728 = 12^q 1764 = 42^q 1849 = 43^q 1936 = 44^q 2025 = 45^q 2048 = 2^q 2116 = 46^q 2187 = 3^q 2197 = 13^q 2209 = 47^q 2304 = 48^q 2401 = 7^q 2500 = 50^q 2601 = 51^q 2704 = 52^q 2744 = 14^q 2809 = 53^q 2916 = 54^q 3025 = 55^q 3125 = 5^q 3136 = 56^q 3249 = 57^q 3364 = 58^q 3375 = 15^q 3481 = 59^q 3600 = 60^q 3721 = 61^q 3844 = 62^q 3969 = 63^q 4096 = 2^q 4225 = 65^q 4356 = 66^q 4489 = 67^q 4624 = 68^q 4761 = 69^q 4900 = 70^q 4913 = 17^q 5041 = 71^q 5184 = 72^q 5329 = 73^q 5476 = 74^q 5625 = 75^q 5776 = 76^q 5832 = 18^q 5929 = 77^q 6084 = 78^q 6241 = 79^q 6400 = 80^q 6561 = 3^q 6724 = 82^q 6859 = 19^q 6889 = 83^q 7056 = 84^q 7225 = 85^q 7396 = 86^q 7569 = 87^q 7744 = 88^q 7776 = 6^q 7921 = 89^q 8000 = 20^q 8100 = 90^q 8192 = 2^q 8281 = 91^q 8464 = 92^q 8649 = 93^q 8836 = 94^q 9025 = 95^q 9216 = 96^q 9261 = 21^q 9409 = 97^q 9604 = 98^q 9801 = 99^q 10000 = 10^q 10201 = 101^q 10404 = 102^q 10609 = 103^q 10648 = 22^q 10816 = 104^q 11025 = 105^q 11236 = 106^q 11449 = 107^q 11664 = 108^q 11881 = 109^q 12100 = 110^q 12167 = 23^q 12321 = 111^q 12544 = 112^q 12769 = 113^q 12996 = 114^q 13225 = 115^q 13456 = 116^q 13689 = 117^q 13824 = 24^q 13924 = 118^q 14161 = 119^q 14400 = 120^q 14641 = 11^q 14884 = 122^q 15129 = 123^q 15376 = 124^q 15625 = 5^q 15876 = 126^q 16129 = 127^q 16384 = 2^q 16641 = 129^q 16807 = 7^q 16900 = 130^q 17161 = 131^q 17424 = 132^q 17576 = 26^q 17689 = 133^q 17956 = 134^q 18225 = 135^q 18496 = 136^q 18769 = 137^q 19044 = 138^q 19321 = 139^q 19600 = 140^q 19683 = 3^q 19881 = 141^q 20164 = 142^q 20449 = 143^q 20736 = 12^q 21025 = 145^q 21316 = 146^q 21609 = 147^q 21904 = 148^q 21952 = 28^q 22201 = 149^q 22500 = 150^q 22801 = 151^q 23104 = 152^q 23409 = 153^q 23716 = 154^q 24025 = 155^q 24336 = 156^q 24389 = 29^q 24649 = 157^q 24964 = 158^q 25281 = 159^q 25600 = 160^q 25921 = 161^q 26244 = 162^q 26569 = 163^q 26896 = 164^q 27000 = 30^q 27225 = 165^q 27556 = 166^q 27889 = 167^q 28224 = 168^q 28561 = 13^q 28900 = 170^q 29241 = 171^q 29584 = 172^q 29791 = 31^q 29929 = 173^q 30276 = 174^q 30625 = 175^q 30976 = 176^q 31329 = 177^q 31684 = 178^q 32041 = 179^q 32400 = 180^q 32761 = 181^q 32768 = 2^q 33124 = 182^q 33489 = 183^q 33856 = 184^q 34225 = 185^q 34596 = 186^q 34969 = 187^q 35344 = 188^q 35721 = 189^q 35937 = 33^q 36100 = 190^q 36481 = 191^q 36864 = 192^q 37249 = 193^q 37636 = 194^q 38025 = 195^q 38416 = 14^q 38809 = 197^q 39204 = 198^q 39304 = 34^q 39601 = 199^q 40000 = 200^q 40401 = 201^q 40804 = 202^q 41209 = 203^q 41616 = 204^q 42025 = 205^q 42436 = 206^q 42849 = 207^q 42875 = 35^q 43264 = 208^q 43681 = 209^q 44100 = 210^q 44521 = 211^q 44944 = 212^q 45369 = 213^q 45796 = 214^q 46225 = 215^q 46656 = 6^q 47089 = 217^q 47524 = 218^q 47961 = 219^q 48400 = 220^q 48841 = 221^q 49284 = 222^q 49729 = 223^q 50176 = 224^q 50625 = 15^q 50653 = 37^q 51076 = 226^q 51529 = 227^q 51984 = 228^q 52441 = 229^q 52900 = 230^q 53361 = 231^q 53824 = 232^q 54289 = 233^q 54756 = 234^q 54872 = 38^q 55225 = 235^q 55696 = 236^q 56169 = 237^q 56644 = 238^q 57121 = 239^q 57600 = 240^q 58081 = 241^q 58564 = 242^q 59049 = 3^q 59319 = 39^q 59536 = 244^q 60025 = 245^q 60516 = 246^q 61009 = 247^q 61504 = 248^q 62001 = 249^q 62500 = 250^q 63001 = 251^q 63504 = 252^q 64000 = 40^q 64009 = 253^q 64516 = 254^q 65025 = 255^q 65536 = 2^q 66049 = 257^q 66564 = 258^q 67081 = 259^q 67600 = 260^q 68121 = 261^q 68644 = 262^q 68921 = 41^q 69169 = 263^q 69696 = 264^q 70225 = 265^q 70756 = 266^q 71289 = 267^q 71824 = 268^q 72361 = 269^q 72900 = 270^q 73441 = 271^q 73984 = 272^q 74088 = 42^q 74529 = 273^q 75076 = 274^q 75625 = 275^q 76176 = 276^q 76729 = 277^q 77284 = 278^q 77841 = 279^q 78125 = 5^q 78400 = 280^q 78961 = 281^q 79507 = 43^q 79524 = 282^q 80089 = 283^q 80656 = 284^q 81225 = 285^q 81796 = 286^q 82369 = 287^q 82944 = 288^q 83521 = 17^q 84100 = 290^q 84681 = 291^q 85184 = 44^q 85264 = 292^q 85849 = 293^q 86436 = 294^q 87025 = 295^q 87616 = 296^q 88209 = 297^q 88804 = 298^q 89401 = 299^q 90000 = 300^q 90601 = 301^q 91125 = 45^q 91204 = 302^q 91809 = 303^q 92416 = 304^q 93025 = 305^q 93636 = 306^q 94249 = 307^q 94864 = 308^q 95481 = 309^q 96100 = 310^q 96721 = 311^q 97336 = 46^q 97344 = 312^q 97969 = 313^q 98596 = 314^q 99225 = 315^q 99856 = 316^q it took 62.6 ms including first init call (and string output to memo which is much slower then the computation itself) without the string it took just 1.25 ms
Strange seg fault error
I'm getting a strange segfault error. I've pasted the relevant parts of the code below. When I run the code in gdb I get the following output: DERPE DERPH0 1 Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5f3ffff0 0x000000010000fcee in Interval::contains (this=0x100103c90, other={isValid = 93, s = 1, e = 7, n = 5}) at interval.cpp:33 33 if (!contains(other.s)) { A.cpp void A::foo(){ ... std::cout << "DERPE" << std::endl; std::cout << "DERPH" << id << " " << curr << std::endl; \\graph is a vector<B> std::cout << "DERPI" << id << " " << curr << " " << graph[id].bar(graph[curr]) << std::endl; ... } B.cpp bool B::bar(B &other) const { // each B contains 2 Intervals, adj, opp return (other.adj.contains(this->adj) and (!this->opp.isValid or other.adj.contains(this->opp) or (other.opp.isValid and other.opp.contains(this->opp)))); } Interval.cpp bool Interval::contains(Interval other) const { if (!contains(other.s)) { return false; } ... } When I try to mess around with gdb I get the following: (gdb) p other $1 = { isValid = 93, s = 1, e = 7, n = 5 } (gdb) p this $2 = (const Interval *) 0x100103c90 (gdb) p *this $3 = { isValid = 93, s = 1, e = 0, n = 5 } (gdb) p other.s $4 = 1 (gdb) p (*this).contains(other.s) Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5f3fff50 Interval::contains (this=Cannot access memory at address 0x7fff5f3fff50 ) at interval.cpp:28 28 bool Interval::contains(int x) const { The program being debugged was signaled while in a function called from GDB. GDB remains in the frame where the signal was received. To change this behavior use "set unwindonsignal on" Evaluation of the expression containing the function (Interval::contains(int) const) will be abandoned. (gdb) p this Cannot access memory at address 0x7fff5f3fff50 Any ideas on how to approach this? I'm lost. EDIT: The full header file for Interval is as follows: Interval.h #ifndef INTERVAL_H #define INTERVAL_H #include <vector> struct Interval { bool isValid; int s, e, n; Interval() {}; Interval(int n); Interval(int start, int end, int n); bool operator==(const Interval other) const; int length() const; bool contains(int x) const; bool contains(Interval other) const; }; #endif I don't have a copy constructor, but I figured that everything is just primitives, so the default copy constructor would work. I played around with it some more and this seems relevant: (gdb) backtrace 10 #0 0x000000010000fcce in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:33 #1 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 #2 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 #3 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 #4 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 #5 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 #6 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 #7 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 #8 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 #9 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 (gdb) backtrace #0 0x000000010000fcce in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:33 ... #67667 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 (gdb)
I figured it out. Infinite backtrace meant that the recursion in contains was not handled properly (I was missing a base case), so it infinitely recursed -> stack overflow -> seg fault.
Why the mysql reconnect failed and cause a segment fault?
This is the stacktrace of the crash of my program Program terminated with signal 11, Segmentation fault. #0 0x00002ae1caf18008 in safer_scalable_free () from /home/is_admin/tbb40_233oss/build/linux_intel64_gcc_cc4.1.2_libc2.5_kernel2.6.18_release/libtbbmalloc.so.2 (gdb) bt #0 0x00002ae1caf18008 in safer_scalable_free () from /home/is_admin/tbb40_233oss/build/linux_intel64_gcc_cc4.1.2_libc2.5_kernel2.6.18_release/libtbbmalloc.so.2 #1 0x00002ae1cad12189 in free () from /home/is_admin/tbb40_233oss/build/linux_intel64_gcc_cc4.1.2_libc2.5_kernel2.6.18_release/libtbbmalloc_proxy.so.2 #2 0x00002ae1cb56fb60 in mysql_close_free (mysql=0x2ae1cc491600) at /home/is_admin/MRPlatform/src/mysql-5.5.17/sql-common/client.c:3643 #3 0x00002ae1cb570ef1 in mysql_close (mysql=0x2ae1cc491600) at /home/is_admin/MRPlatform/src/mysql-5.5.17/sql-common/client.c:3744 #4 0x00002ae1cb573ad4 in mysql_reconnect (mysql=0x2ae1cc491600) at /home/is_admin/MRPlatform/src/mysql-5.5.17/sql-common/client.c:3561 #5 0x00002ae1cb573c37 in cli_advanced_command (mysql=0x2ae1cc491600, command=<value optimized out>, header=0x0, header_length=0, arg=0x0, arg_length=0, skip_check=0 '\000', stmt=0x0) at /home/is_admin/MRPlatform/src/mysql-5.5.17/sql-common/client.c:852 #6 0x00002ae1cb56a3e1 in mysql_ping (mysql=0x1) at /home/is_admin/MRPlatform/src/mysql-5.5.17/libmysql/libmysql.c:969 #7 0x000000000042ded6 in TDRNetwork::DataAccess::ExecuteSQL(char const*) () #8 0x000000000041c511 in MRSystem::Master::Process(TDRNetwork::JobMsg*) () #9 0x000000000042a33f in MRSystem::MasterWorker::Run() () #10 0x000000000042fc99 in TDRNetwork::Thread::StartThread(void*) () #11 0x00000033b080673d in start_thread () from /lib64/libpthread.so.0 #12 0x00000033b00d44bd in clone () from /lib64/libc.so.6 At frame 6 I noticed that the mysql=0x1 which is outof bound. It seems the mysql link auto closed after 8 hours if no action taken. So why the reconnection failed? Thanks. EDIT: the error occurs at client.c:3643 my_free(mysql->info_buffer); #if defined(EMBEDDED_LIBRARY) || MYSQL_VERSION_ID >= 50100 my_free(mysql->info_buffer); mysql->info_buffer= 0; #endif And the info_buffer is out of bound: (gdb) p *mysql $2 = {net = {vio = 0x0, buff = 0x0, buff_end = 0x2aaaabcf5080 "", write_pos = 0x2aaaabcf3080 "", read_pos = 0x2aaaabcf3080 "", fd = 10, remain_in_buf = 0, length = 0, buf_length = 0, where_b = 0, max_packet = 8192, max_packet_size = 1073741824, pkt_nr = 1, compress_pkt_nr = 0, write_timeout = 31536000, read_timeout = 31536000, retry_count = 1, fcntl = 0, return_status = 0x0, reading_or_writing = 0 '\000', save_char = 0 '\000', unused1 = 0 '\000', unused2 = 0 '\000', compress = 0 '\000', unused3 = 0 '\000', unused = 0x0, last_errno = 0, error = 2 '\002', unused4 = 0 '\000', unused5 = 0 '\000', last_error = '\000' <repeats 511 times>, sqlstate = "00000", extension = 0x0}, connector_fd = 0x0, host = 0x2aaaabea34b8 "127.0.0.1", user = 0x2aaaabeb3bf8 "", passwd = 0x2aaaabeb3c00 "\370;뫪*", unix_socket = 0x0, server_version = 0x2aaaabea34d0 "5.5.17", host_info = 0x2aaaabea34a0 "", info = 0x0, db = 0x2aaaabeb3c08 "", charset = 0x2aaaab661060, fields = 0x0, field_alloc = {free = 0x0, used = 0x0, pre_alloc = 0x0, min_malloc = 32, block_size = 8160, block_num = 4, first_block_usage = 0, error_handler = 0}, affected_rows = 18446744073709551615, insert_id = 0, extra_info = 0, thread_id = 472, packet_length = 7, port = 3306, client_flag = 959117, server_capabilities = 18446744071563114495, protocol_version = 10, field_count = 0, server_status = 2, server_language = 33, warning_count = 0, options = {connect_timeout = 0, read_timeout = 0, write_timeout = 0, port = 0, protocol = 0, client_flag = 0, host = 0x0, user = 0x0, password = 0x0, unix_socket = 0x0, db = 0x0, init_commands = 0x0, my_cnf_file = 0x0, my_cnf_group = 0x0, charset_dir = 0x0, charset_name = 0x0, ssl_key = 0x0, ssl_cert = 0x0, ssl_ca = 0x0, ssl_capath = 0x0, ssl_cipher = 0x0, shared_memory_base_name = 0x0, max_allowed_packet = 0, use_ssl = 0 '\000', compress = 0 '\000', named_pipe = 0 '\000', unused1 = 0 '\000', unused2 = 0 '\000', unused3 = 0 '\000', unused4 = 0 '\000', methods_to_use = MYSQL_OPT_CONNECT_TIMEOUT, client_ip = 0x0, secure_auth = 0 '\000', report_data_truncation = 0 '\000', local_infile_init = 0, local_infile_read = 0, local_infile_end = 0, local_infile_error = 0, local_infile_userdata = 0x0, extension = 0x0}, status = MYSQL_STATUS_READY, free_me = 0 '\000', reconnect = 1 '\001', scramble = "[k)(,EVacN<#96B(?E{_", unused1 = 0 '\000', unused2 = 0x0, unused3 = 0x0, unused4 = 0x0, unused5 = 0x0, stmts = 0x0, methods = 0x2aaaab650100, thd = 0x0, unbuffered_fetch_owner = 0x0, **info_buffer = 0x1 <Address 0x1 out of bounds>**, extension = 0x2aaaabebb708}