Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Archcady Looking at the code around, got few questions, why this allocation is needed at first place, why ap_num is incremented also if the condition above is false (thus no memcpy is done) ? That memcpy is done on non POD data , should not.
@pan- Thanks for pointers
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @0xc0170 I think in this part we use allocation is to borrow the data structure of this class so we might use this
scan_handler->ap_details[]
somewhere else as a pointer to an instance ofWiFiAccessPoint
.line 80
scan_handler->ap_num__;
seems like being placed here on purpose,scan_handler->ap_details[scan_handler->ap_num]
has reserved its place for accesspoint data, so if the condition above is false, the space is left zero and ap_num++ to start process next accesspoint data.Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Archcady Better use placement new then. The behavior of the program is undefined (from the C++ standard standpoint) if a non POD object is initialized with memcpy.
You can replace:
by
new(&scan_handler->ap_details[scan_handler->ap_num]) WiFiAccessPoint(ap);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @pan- , BTW do I need to call destructor explicitly here? I'm not familiar with placement new in C++.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Archcady Placement new does not allocate memory from the heap. It use the address supplied in the placement param to construct the object.
In this specific case, the object will be constructed at the address
&scan_handler->ap_details[scan_handler->ap_num]
. You'll have to call directly the destructor when theap_details
memory block goes away or thedelete[]
operator ifap_details
have been allocated withnew[]
(which I doubt since the data structure is defined locally).