标签: dynamic-memory-allocation

链接列表练习,我做错了什么?

大家好.我正在做一个涉及动态内存分配,指针,类和异常的链表练习.有人愿意批评它并告诉我我做错了什么以及我应该在风格和上面列出的那些主题方面做得更好吗?

/*

Linked List exercise

*/

#include <iostream>
#include <exception>
#include <string>
using namespace std;

class node{
public:
    node * next;
    int * data;
    node(const int i){
        data = new int;
        *data = i;
    }
    node& operator=(node n){
        *data = *(n.data);
    }
    ~node(){
        delete data;
    }
};

class linkedList{

public:

    node * head;
    node * tail;
    int nodeCount;

    linkedList(){
        head = NULL;
        tail = NULL;
    }

    ~linkedList(){
        while (head){
            node* t = head->next;
            delete head;
            if (t) head = t; …
Run Code Online (Sandbox Code Playgroud)

c++ pointers linked-list dynamic-memory-allocation

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

动态内存分配"删除"

如果我int动态分配对象的内存位置,如下所示:

int *x = new int;

完成它之后,想要释放堆上的内存,我将执行以下操作:

delete x;

现在,如果我没有做到以下几点:

x = NULL;

x指向另一个地址吗?更新: another而不是many

说我没做x = NULL,做了另一个 delete x;,会发生什么?

c++ heap dynamic-memory-allocation delete-operator

1
推荐指数
2
解决办法
1761
查看次数

从何时开始使用可变长度数组?

请告诉我为什么以下代码甚至在以下代码上运行strict C-99 compiler:

#include <stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int a[n];
    a[1]=10;
    a[2]=5;
    printf("%d %d",a[1],a[2]);
}
Run Code Online (Sandbox Code Playgroud)

变量声明必须在C中的任何其他语句之前发生吗?如果我们想要一个动态分配的数组,我们必须使用内存分配函数,malloc()但它是如何获取和输入整数并分配该大小的数组?

c arrays memory-management dynamic-memory-allocation

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

使用动态分配的内存(指针)

当我正在尝试学习C++时,我正在玩指针和动态内存,并且在编译时我不断收到此错误.

error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

int * ageP;    
ageP = new (nothrow) int;

if (ageP == 0)
{
    cout << "Error: memory could not be allocated";
}
else
{
    cout<<"What is your age?"<<endl;
    cin>> ageP;                       <--this is the error line
    youDoneIt(ageP);                                            
    delete ageP;
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?在此先感谢您的帮助.

c++ io pointers dynamic-memory-allocation visual-c++

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

这个用于增长数组的C++代码可能会破坏吗?

我终于决定继续学习C++,并在一本增加数组大小的书中遇到了下面的代码.该函数采用指向具有原始大小的数组的指针,并返回一个大小为double的新数组.

int *doubleArraySize(int *p_array, int *p_size) {
    *p_size *= 2;

    int *p_new_array = new int[*p_size];
    for(int i = 0; i < *p_size; i++)
        p_new_array[i] = p_array[i];

    delete[] p_array;
    return p_new_array;
}
Run Code Online (Sandbox Code Playgroud)

当我们到达for循环时,值*p_size已经加倍.这意味着(至少对我而言)当我们访问时,p_array[i]我们最终会进入不属于的内存区域p_array.这是一个问题吗?这个代码可以崩溃吗?如果没有,我错过了什么?

c++ arrays memory-leaks dynamic-memory-allocation

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

我可以用新的内存分配一块内存吗?

所以考虑到这个结构:

typedef struct {
    int* arr1;
    int* arr2;
} myStruct;
Run Code Online (Sandbox Code Playgroud)

这个答案描述了使用单个malloc来同时分配a myStruct和它的数组:

myStruct* p = malloc(sizeof(*p) + 10 * sizeof(*p->arr1) + 10 * num * sizeof(*p->arr2);

if(p != NULL) {
    p->arr1 = (int*)(p + 1);
    p->arr2 = p->arr1 + 10;
}
Run Code Online (Sandbox Code Playgroud)

我想知道有没有类似的方法来做到这一点new
显然,我希望能够分配到我在运行时收到的大小,就像使用C示例一样.

c c++ malloc new-operator dynamic-memory-allocation

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

使用free()释放new分配的内存空间

据我了解,delete[]用于释放分配的内存空间new.free()也可以用来释放那个内存空间.那么在释放内存free()代替时我将面临哪些类型的问题delete[]

c++ free dynamic-memory-allocation

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

调试断言失败:_BLOCK_TYPE_IS_VALID

在C++中使用以下代码:

class Foo {
    vector<Foo*> otherFoos;
};

int _tmain(int argc, _TCHAR* argv[])
{
  Foo* data = new Foo[5];
  delete data;
}
Run Code Online (Sandbox Code Playgroud)

我得到以下错误: 在此输入图像描述

我正在使用Visual Studio 2013.我不知道我的代码有什么问题.

c++ memory-management dynamic-memory-allocation

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

带字符串数组的C程序malloc

在程序开始时,我需要为未知数量的字符串(大小未知)动态分配内存,以便以后使用。要获得用户的字符串数,我需要:

int main(int argc, char *argv[]){

int number = atoi(argv[1]);
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切都很好。现在,“ number”保存着用户在命令行上输入的用于执行代码的数字。现在是我不太了解的部分。现在,我需要动态存储字符串的长度以及字符串的内容。例如,我希望程序运行如下:

Enter the length of string 1: 5
Please enter string 1: hello
Enter the length of string 2: ...
Run Code Online (Sandbox Code Playgroud)

为此,我认识到必须创建一个字符串数组。但是,我不太了解指针的概念,而没有。我想要的可能是如何简化此过程?

c string dynamic-memory-allocation

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

为什么会发生堆损坏?

我一直在研究学校的一些练习题,其中之一是必须将输入的整数写到动态分配的字符串中。该代码可以很好地完成工作,直到释放分配的内存(发生堆损坏)为止。

有人可以解释为什么会发生这种情况以及我在做什么错吗?

int main() {

  char *string = NULL;
  char **string2 = &string;

  Conversion(string2);
  printf("Entered number converted to string: %s", string);
  free(string);

  return 0;
}

int Conversion(char **string) {

    int num = 0, temp = 0, dcount = 0;
    printf("Enter number: ");
    scanf(" %d", &num);

    temp = num;

    while (temp != 0) {
      temp /= 10;
      dcount++;
    }

    *string = (char*)malloc(dcount*sizeof(char));
    if (*string == NULL) {
      printf("Error during memory allocation!");
      return -1;
    }

    sprintf(*string, "%d", num);

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

c string pointers segmentation-fault dynamic-memory-allocation

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