简体   繁体   English

如何将内存的连续块(​​动态)添加到预先分配的动态内存

[英]How to add contiguous blocks of memory (dynamically) to a previosly allocated dynamic memory

I am working with Arduino. 我正在使用Arduino。 My main task is to divide the entire room into small grids and do a mapping of the obstacles in the room using ultrasonic sensors. 我的主要任务是将整个房间分成小网格,并使用超声波传感器对房间中的障碍物进行映射。
For that i wish to allocate memory dynamically. 为此,我希望动态分配内存。 Now these sensors can detect only up to 4m. 现在,这些传感器最多只能检测到4m。 So i will divide that distance into blocks and allocate memory. 因此,我将把该距离分成多个块并分配内存。 But as the robo moves ahead it will discover more room and needs more memory appended to the previously allocated memory. 但是随着机器人的前进,它将发现更多的空间,并且需要更多的内存附加到先前分配的内存中。

So this is a question of adding contiguous block of memory to a previously allocated memory. 因此,这是将连续的内存块添加到先前分配的内存中的问题。

This is a small part of the code that is related to the problem. 这只是与问题相关的代码的一小部分。

//max_x and max_y are the number of blocks in x and y direction respectively.

int **grid = new *int[max_x];              
for(int i = 0; i <max_x; i++)
{
    int grid[i] = new int[max_y];
}

void foo();   //stores some values in the previously allocated memory.

dist_y = get_y();      //returns blocks available in y direction.
dist_x = get_x();      //returns blocks available in x direction.
if((dist_y > max_y) | (dist_x > max_x))
{
    append_grid((max_x-dist_x),(max_y-dist_y));  //add these many number of blocks.
}  

So how to add more memory depending on the distance measured? 那么如何根据测得的距离增加更多的内存呢?

I think this is what you are trying to do 我想这就是你想做的

#include <iostream>
using namespace std;

int max_x=10;
int max_y=12;

int** changeXY( int** g, int newX, int newY ){
int** temp = new int*[newY];
for(int i = 0; i < newY; i++)
    temp[i] = new int[newX];
for( int y = 0; y < max_y; y++)    
    for( int x = 0; x < max_x; x++)
        temp[x][y] = g[x][y];
        delete[] g;
        return temp;
}
int main() {

//Its easier to think about what you are creating as a vector
//full of vectors rather than a grid. Here you are creating a 
//pointer to a string of pointers of size max_y
//or assigning the number of columns
int **grid = new int*[max_y];            

//here you are making a loop to add elements to the pointer
//array you just created. if the pointer syntax is confusing remember
//that "grid[i]" is equivalent to "*(grid + i)"
for(int i = 0; i < max_y; i++)
    grid[i] = new int[max_x];   

grid = changeXY(grid,22,20);
delete[] grid;
return 0;
}

There were a couple mistakes in your syntax. 您的语法有几个错误。 First of all, when you declare a dynamic memory, the syntax is "new int*[max_y]", you are effectively telling the compiler what type to specify. 首先,当声明动态内存时,语法为“ new int * [max_y]”,这实际上是在告诉编译器要指定哪种类型。 Second, when adding the other dimension in the loop, you are adding it to the grid. 其次,在循环中添加另一个尺寸时,您将其添加到网格中。 Not the first dimension. 不是第一个维度。

OH, btw! 哦,顺便说一句! the way I wrote this, there is no handling and unless max_y and max_x are global like they are in this example the changeXY() function will have to also include them in the input parameters. 用我编写此代码的方式,没有任何处理,除非max_y和max_x是全局的(如本例中那样),changeXY()函数还必须将它们包括在输入参数中。 Hope this helps. 希望这可以帮助。

Now that i am aware of std::vector i think i can use it for this problem. 现在,我知道std :: vector了,我认为我可以将其用于此问题。 I haven't posted the entire code because I think it will add to too much of details that are not relevant. 我没有发布完整的代码,因为我认为它会增加过多的无关紧要的细节。

vector<vector<int> > grid;
dist_y = get_y();        //returns the number of blocks in Y direction
if(dist_y > threshold){
    forward();          //moves a step forward.
    dist_x = get_x();   //returns blocks available in X direction.
    vector<int> col;
    for(int i = 0; i < dist_x; i++) {
        col.push_back(1);   //1 indicates there is free space there.
    }
    grid.push_back(col);
}  

Repeating this loop will keep adding elements as required. 重复此循环将继续根据需要添加元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM