标签: dynamic-memory-allocation

另一个动态内存分配错误

我正在尝试为多维数组(8行,3列)分配内存.

这是分配的代码(我确定错误很清楚)

char **ptr = (char **) malloc( sizeof(char) * 8);

for (i = 0; i < 3; i++)
    ptr[i] = (char *) malloc( sizeof(char) * 3);
Run Code Online (Sandbox Code Playgroud)

当我引用这个崩溃时发生崩溃:

ptr[3][0];
Run Code Online (Sandbox Code Playgroud)

xxxx.exe中0x0135144d处的未处理异常:0xC0000005:访问冲突写入位置0xabababab.

是否有针对此类主题的推荐参考/读物?

谢谢.

c malloc pointers dynamic-memory-allocation

0
推荐指数
2
解决办法
286
查看次数

在C++中动态分配列表

我制作了一个可爱的通用(即模板)List类来处理C++中的列表.原因是我发现这个std::list类在日常使用中非常难看,而且由于我经常使用列表,我需要一个新的.主要的改进是,我的班级,我可以用来[]从中获取物品.此外,还有待实现的是一个IComparer对事物进行排序的系统.

我正在使用这个ListOBJLoader,我的类加载Wavefront .obj文件并将它们转换为网格.OBJLoader包含指向以下"类型"的指针列表:3D位置,3D法线,uv纹理坐标,顶点,面和网格.顶点列表具有必须链接到所有3D位置,3D法线和uv纹理坐标列表中的某些对象的对象.面链接到顶点,网格链接到面.所以他们都是相互联系的.

为简单起见,让我们考虑一下,在某些情况下,只有两个指针列表:List<Person*>List<Place*>.Personclass包含,其中包括字段List<Place*> placesVisitedPlace类包含字段List<Person*> peopleThatVisited.所以我们有结构:

class Person
{
    ...
  public:
    Place* placeVisited;
    ...
};

class Place
{
    ...
  public:
    List<People*> peopleThatVisited;
};
Run Code Online (Sandbox Code Playgroud)

现在我们有以下代码:

Person* psn1 = new Person();
Person* psn2 = new Person();

Place* plc1 = new Place();
Place* plc2 = new Place();
Place* plc2 = new Place();


// make some links between …
Run Code Online (Sandbox Code Playgroud)

c++ pointers list dynamic-memory-allocation dangling-pointer

0
推荐指数
1
解决办法
8256
查看次数

我应该在什么情况下使用malloc?我很困惑应该在C中使用动态分配

如果我想创建一个2D数组,用户输入指定维度,我不能只在主函数中按顺序执行此操作吗?一旦我使用scanf获得尺寸,然后我创建一个具有这些尺寸的数组?根据我的理解,当运行时不知道所需的空间时,应该使用malloc.我不知道运行时所需的空间,但我没有动态分配内存,无论如何它都会工作,对吧?也许我完全误解了一些东西.

c malloc dynamic-memory-allocation

0
推荐指数
1
解决办法
125
查看次数

C和C++中的指针和动态内存

几天前我不得不使用C,当使用指针时,我有点意外.

C中的一个例子:

#include <stdio.h>
#include <stdlib.h>

void GetPointer(int* p) {
  p = malloc( sizeof(int) );
  if(p) {
    *p = 7;
     printf("IN GetPointer: %d\n",*p);
  } else {
    printf("MALLOC FAILED IN GetPointer\n");
  }
}

void GetPointer2(int* p) {
  if(p) {
    *p = 8;
     printf("IN GetPointer2: %d\n",*p);
  } else {
    printf("INVALID PTR IN GetPointer2");
  }
}

int* GetPointer3(void) {
  int* p = malloc(sizeof(int));
  if(p) {
    *p = 9;
    printf("IN GetPointer3: %d\n",*p);
  } else {
    printf("MALLOC FAILED IN GetPointer3\n");
  }
  return p;
}

int …
Run Code Online (Sandbox Code Playgroud)

c c++ pointers dynamic-memory-allocation

0
推荐指数
1
解决办法
186
查看次数

为什么动态分配的指针数组不需要解除引用来获取实际成员

所以我最近遇到了以下代码:

struct Student
{
    int *number;
    char *name;
    double *marks;
};

int main(){
    int n;
    Student *s;
    s = new Student;
    cout << "Enter the number of subjects the student learns: ";
    cin >> n;
    s->number= new int;
    s->name=new char[20];
    s->marks=new double[n];
    cout << "Enter the name of the student: ";
    cin >> s->name;
    cout << "Enter the number in class of " << s->name << ": ";
    cin >> *(s->number);
    for (int i = 0 ; i < n …
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers dynamic-memory-allocation dereference

0
推荐指数
1
解决办法
1096
查看次数

C++中的动态内存点

我试图用C++编写代码,从文件读取,一系列点,将其存储在动态数组中,然后打印回来.

这是我给出的规范:

"我们希望利用我们可以使用动态内存的事实,因此我们不是根据我们的估计在开始时分配足够大的内存量,而是实现以下算法:

最初,分配的内存非常少.

在循环的每次迭代中(从文件读取并存储到动态数组中),我们跟踪:

  • 数组最大大小(分配的内存).
  • 数组中的元素数.

当由于新插入而导致元素数量大于数组最大大小时,内存重新分配需要按如下方式进行:

  • 分配另一个具有更大最大大小的动态数组.
  • 将前一个数组中的所有元素复制到新数组中.
  • 释放为前一个阵列分配的内存区域.
  • 获取指向前一个数组的指针以指向新数组.
  • 在数组末尾添加新项.这就是我的问题所在.

从我下面的代码中,我认为其他一切都很好但是最后一个要求,即在数组末尾添加新项目.

当数组Max_Size超过文件的元素数时,代码工作正常,但是当我尝试扩展num_elements时,结果是文件中的额外数字只保存为零

.

另外,分配还不允许使用向量.对不起,我忘了提这个,我是stackoverflow的新手,有点编程.

请帮忙

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

struct point {  
    double x;
    double y;
};

int main () {

    ifstream inputfile;

    inputfile.open("datainput.txt");

    if(!inputfile.is_open()){
    cout << "could not open file" << endl;
        exit(EXIT_FAILURE);
    }

    //initially very little memory is allocated
    int Max_Size = 10;
    int num_elements = 0;
    point *pp = new point[Max_Size];


    //read from file and store in dynamic array …
Run Code Online (Sandbox Code Playgroud)

c++ arrays algorithm memory-management dynamic-memory-allocation

0
推荐指数
1
解决办法
231
查看次数

检测到堆腐败

这是我记忆的方式.

Expression = new char[MemBlock.length()];
VarArray = new char[Variables.length()];
for (unsigned int i = 0; i < MemBlock.length(); i++)
{
    Expression[i] = MemBlock.at(i);
}
Expression[MemBlock.length() + 1] = NULL;
for (unsigned int i = 0; i < Variables.length(); i++)
{
    VarArray[i] = Variables.at(i);
}
VarArray[Variables.length() + 1] = NULL;
Run Code Online (Sandbox Code Playgroud)

当我尝试删除它时,我收到错误...

Logic::~Logic(){
delete[] VarArray;  -> happens on this line.
VarArray = NULL;
delete[] Expression;
Expression = NULL;
}
Run Code Online (Sandbox Code Playgroud)

在整个代码中,我不会对新阵列进行任何更改,但它告诉我我讨厌一些问题,我不能指出问题所在,任何帮助都会很棒.

c++ dynamic-memory-allocation delete-operator visual-studio-2013

0
推荐指数
1
解决办法
2971
查看次数

关于操作本机数组的快速问题

#include <iostream>

int
main(void)
{
    int n;
    std::cin >> n;

    int x[n];

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

从C的角度来看,上面的代码不应该正确编译,因为我猜想数组'x'的大小还没有定义.

只有在用户传递变量'n'的参数后,才能定义'x'的大小,但编译和运行时代码没有崩溃,我想知道为什么.

如果它是用C语言编写的,我认为人们会将malloc用于运行时可配置数组.

也许我对数组,内存分配,编译器以及c和c ++之间的关系有一些误解.如果我错了,请纠正我.

c c++ arrays dynamic-memory-allocation

0
推荐指数
1
解决办法
53
查看次数

c中的动态内存分配会抛出一定大小的错误

我正在尝试创建一个数组(动态)并用随机数填充它.

我在Linux上.该程序编译没有错误.这是C代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void create_array(int **, int);
void populate_array(int *X, int size, int low, int high);
void display_array(int *X, int size);


int main()
{
    int *A = NULL;
    int size = 7;
    int low = 10;
    int high = 1000;
    create_array(&A, size);
    populate_array(A, size, low, high);
    display_array(A, size);
    return 0;
}

void create_array(int **X, int size)
{
    *X = (int *)(malloc(size));
}

void populate_array(int *X, int size, int low, int high)
{
    srand(time(0));
    for (int …
Run Code Online (Sandbox Code Playgroud)

c dynamic-memory-allocation

0
推荐指数
1
解决办法
69
查看次数

无法删除类指针

Class Example
{
    // ...
};

int main()
{
    Example** pointer = new Example*[9];

    for(int i = 0; i < 10; i++)
    {
        pointer[i] = new Example();
    }

    for(int i = 0; i < 10; i++)
    {
        delete pointer[i];
    }

    delete[] pointer; // <--- This is problem
    pointer = nullptr;
}
Run Code Online (Sandbox Code Playgroud)

我试图将对象的地址保存在数组中。当我尝试删除它们时,for循环工作得很好,但delete[] pointer会导致“写到堆缓冲区的内存末端”错误。我究竟做错了什么?我也应该删除吗?在此处输入图片说明

c++ dynamic-memory-allocation

0
推荐指数
1
解决办法
65
查看次数