[Solved] Manipulating memory returns invalid pointer error (C++)

  • Thread starter MegapolisPlayer
  • Start date
M

MegapolisPlayer

Guest
MegapolisPlayer Asks: Manipulating memory returns invalid pointer error (C++)
I have a class called Array with a copy constructor and a copy assignment operator (which is not important currently).

Array has a few member variables:

  • mArray is the internal raw array
  • mSize is the amount of elements
  • mReserved is the amount of currently allocated space
  • mMemoryAllocated is a boolean value which determines if any memory has been allocated by the class.

The class works normally, but when I copy it i get an error, specified below.

Here is the code:

Copy c.:

Code:
template<typename tType>
    Array<tType>::Array(const Array& aOther) noexcept {
        this->mArray = nullptr;
        this->mSize = 0;
        this->mReserved = 0;
        this->mMemoryAllocated = false;
        
        if(aOther.mMemoryAllocated) {
            this->mArray = (tType*)malloc(aOther.mReserved * sizeof(tType)); //pointer invalid???
            if(this->mArray == nullptr) {
                //my custom error handling
            }
            for(U64 Id = 0; Id < aOther.mSize; Id++) {
                this->mArray[Id] = aOther.mArray[Id];
            }
            this->mSize = aOther.mSize;
            this->mReserved = aOther.mReserved;
            this->mMemoryAllocated = true;
            return;
        }
    }

Destructor:

Code:
template<typename tType>
Array<tType>::~Array() noexcept {
        if(this->mMemoryAllocated) {
            //call destructors
            for(U64 Id = 0; Id < this->mSize; Id++) {
                this->mArray[Id].~tType();
            }
            free(this->mArray);
        }
}

Test code:

Code:
Array<int> TestArray;
        
TestArray.Add(56);
TestArray.Add(58);
TestArray.Add(66);
TestArray.Add(78);
TestArray.Add(560);
TestArray.Add(690);
TestArray.Add(456);
TestArray.Add(59);
TestArray.Add(57);
TestArray.Add(69);
TestArray.Add(75);
TestArray.Add(555);
TestArray.Add(699);
TestArray.Add(999);
        
{
    Array<int> TestArray2 = TestArray;
        
    TestArray.ShiftRight(3, 2);
    TestArray2.ShiftRight(3);
    TestArray2.Add(56); //crashes here as well
            
    IO << TestArray << "\n";
    IO << TestArray2 << "\n";
            
} //test array 2 issues

The error I'm getting is munmap_chunk: invalid pointer.

I have tried replacing the for loop with memcpy, but to no avail.

I have also tried debugging it with gdb, where it always points to either realloc or free calls (confirmed in the call stack)

IO is a wrapper for std::cout with some extra functionality.

I'm using clang(++) 14 on Linux (Mint 21) and the CodeLite 16.4 IDE.
 

Unreplied Threads

What Is ITIL® v4?

Lora Goody Asks: What Is ITIL® v4?
Specialist in ITIL 4: Adding Value for Stakeholders Expert in Directing, Planning, and Improving ITIL 4 Strategy at High Speed. These lessons will be used by IT professionals who require the technical and practical know-how to oversee dependable IT-enabled teams, services, and workflows. If you want to become an ITIL 4 Strategic Leader or Managing Professional, you must complete all the modules. After doing so, you will have a much better understanding of ITIL and be able to use it successfully.

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

[Solved] Count points in a grid

  • Aryan Irani
  • Geography
  • Replies: 0
***** Irani Asks: Count points in a grid
I have a grid that I have created and a path that is there. I want to calculate the number of points each grid has or I want to calculate the number of points each slab or rectangle has. enter image description here

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, solveforum.

[Solved] Delete polygons from layer 1 with polygons from layer 2 based on a common attribute between both layers

  • epcardiel
  • Geography
  • Replies: 0
epcardiel Asks: Delete polygons from layer 1 with polygons from layer 2 based on a common attribute between both layers
I have two layers with a large number of polygons. Layer 1 are the polygons I want to crop (green border) and layer 2 are the clipping polygons (red border). Both layers have a common identifier in layer 1 is ID_1 and in layer 2 is ID_2. enter image description here

The result should be that the polygon of layer 1 with ID_1 =1 is clipped by the polygon of layer 2 with ID_2=1 and so on with all matching polygons. enter image description here

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, solveforum.

[Solved] (GEE) constant value after applying scaling factor to LANDSAT

N_LLC Asks: (GEE) constant value after applying scaling factor to LANDSAT
After applying a scaling factor to my LANDSAT collection 2 level 2 data the values seem to be off.

screen capture of code and output

Code:
var addNDVI = function(image) {
      var ndvi = image.normalizedDifference(['SR_B4', 'SR_B5']).rename('NDVI');
      return image.addBands(ndvi);
    };


var collection1 = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
    .filterBounds(sch)
    .filterDate('2013-01-01', '2023-01-01')
    .select(['SR_B4', 'SR_B5'])
    .map(addNDVI).select('NDVI');

collection1 = collection1.map(
  function(img){
    var rescaled_NDVI = img.select('NDVI').multiply(0.0000275).add(-0.2)
    .rename('NDVI_rescaled')
    return img.addBands(rescaled_NDVI);
  }
);
  
var series1 = ui.Chart.image.seriesByRegion(
    collection1, sch, ee.Reducer.mean(), 'NDVI_rescaled', 30, 'system:time_start', 'label')
        .setChartType('ScatterChart')
        .setOptions({
          title: 'L5_SCH_1984-01-01_1986-12-31',
          vAxis: {title: 'NDVI'},
          lineWidth: 1,
          pointSize: 4,
});

Map.addLayer(sch, {color: 'FF0000'});
print(series1);
//var palette = {min: -1, max: 1, palette: ['blue', 'white', 'green']};
Map.addLayer(collection1);

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, solveforum.

Parallel transport in curved spacetime

  • Tanmoy Pati
  • Physics
  • Replies: 0
Tanmoy Pati Asks: Parallel transport in curved spacetime
This is sort of a very introductory question and I am not finding any reference regarding this. And let me know whether my answer is correct or not.

For example, we are parallelly transporting a vector on a surface (intrinsic parallel transport). We take two points and join by a curve and we will do the procedure along the curve. In terms of intrinsic coordinates $P=P(u)$ and $Q=Q(u+du)$. From $P(u)$ if we transport a vector, parallelly, the important condition is $$ \nabla_i V^j=0\\ \text{or}, \quad \partial_iV^j=-\Gamma^j_{ik}V^k\\ \text{or,} \quad V_\parallel^j(u+du)=V_\parallel^j(u)-\Gamma^j_{ik}V_\parallel^k(u) du^i+\mathcal{O}(du^2) $$ This transport is done in one path. If we take another path to do the same operation, if $R^a_{bcd} \neq 0$, we will get two different answers for the transported vector component at $Q(u+du)$. So, from the eqn how to interpret this? In particular, there should be some terms which will make $V_\parallel^j(u+du)$ different from one path to another.

Note: These changed $V_\parallel^j(u+du)$ will not make the vector from the point of view of the surface different, since the basis vector also have derivative and have $\Gamma$'s which can cancel the contributions from these $du^n$ terms.

One soln I was thinking, if we take a normal coordinate, for instance, $\Gamma$'s will vanish but $\partial \Gamma$'s will not vanish, hence they actually carry the information of curvature of the manifold. Hence to see actually $V_\parallel^j(u+du)$ are different at different paths, we have to expand upto $du^2$ at least.

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

What happens with the tip vortices of two aircraft flying in the opposite direction?

  • lWindy
  • Physics
  • Replies: 0
lWindy Asks: What happens with the tip vortices of two aircraft flying in the opposite direction?
Suppose that an aircraft flies with its wing through an tip vortex of another aircraft which flew in the opposite direction. Suppose that the shed wing tip vortex of both aircraft are exactly the same in strength, only rotating in opposite direction?

What will the net effect be? Will the angular momentum be canceled?

I would like to place this in the light of the existence of a root vortex experienced by wind turbines. From Burton's Wind Energy Handbook (3rd edition, p78) :

" For example, on a two blade rotor, unlike an aircraft wing, the bound circulations on the two blades shown in Figure 3.27 are opposite in sign and so combine in the idealised case of the blade root being at the rotational axis to shed a straight line vortex along the axis with strength equal to the blade circulation times the number of blades. "

With the aircraft example the circulation is opposite in sign as well. So why is the root vortex a summation of the two blades instead of cancellation?

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

Does parallelism of cardinal comparison between sets and their power sets, enact a form of choice?

Zuhair Al-Johar Asks: Does parallelism of cardinal comparison between sets and their power sets, enact a form of choice?
Let $ * $ range over cardinal relations $ \{<,=,>, <>\}$, if we add the following axiom to $\sf ZF$, would that prove a known form of choice?

Parallelism: $ \forall x \forall y \, ( |x| * |y| \leftrightarrow |\mathcal P(x)| * |\mathcal P(y)| )$

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.
Top