动态 allcocation 对象和 int c++

发布于 2025-01-11 05:29:33 字数 483 浏览 2 评论 0原文

你好我有一个疑问下面的代码是如何工作的?

#include <iostream>
using namespace std;

int main()
{
    int* arr = new int;
    arr[0] = 94;
    arr[1] = 4;
    cout << arr[0] << endl;
}

为什么这会显示错误我应该做什么

#include <iostream>
using namespace std;

struct test 
{
    int data;
};
int main()
{
    test* arr = new test;
    arr[0] -> data= 4;
    arr[1] -> data= 42;
    cout << arr[0]->data << endl;
}

hello I have a doubt how does the code below works??

#include <iostream>
using namespace std;

int main()
{
    int* arr = new int;
    arr[0] = 94;
    arr[1] = 4;
    cout << arr[0] << endl;
}

and why does this shows me a error what should I do

#include <iostream>
using namespace std;

struct test 
{
    int data;
};
int main()
{
    test* arr = new test;
    arr[0] -> data= 4;
    arr[1] -> data= 42;
    cout << arr[0]->data << endl;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事随风而去 2025-01-18 05:29:33

在您的代码中:

#include <iostream>
using namespace std;

int main()
{
    int* arr = new int;
    arr[0] = 94; // This will work
    arr[1] = 4; // This will cause undefined behaviour
    cout << arr[0] << endl;
}

Int 上述代码,arr 是指向单个 int 的指针,因此您可以使用以下任一方式访问该 int

arr[0]
*arr

。 .但是 arr1 不起作用,因为没有分配了足够的内存 大批。

要解决此问题,您必须为 arr: .. 分配更多内存并

int* arr = new int[2];

更改 arr: 的大小,

int arr_size = 2;
int* arr = new int[arr_size]; // size of arr = 2
arr[0] = 12;
arr[1] = 13;

int* new_arr = new int[arr_size + 1];

for (int i = 0; i < arr_size; i++)
{
    new_arr[i] = arr[i];
}

delete[] arr;
arr = new_arr;

// size of arr = 3

但是当 arr 具有大量元素时,所有这些都会变得计算昂贵且耗时。因此,我建议使用 C++std::vector

使用 std::vector 的第二个程序:

#include <iostream>
#include <vector>

struct test
{
    int data;
};
int main()
{
    std::vector<test> vec{ test(4), test(42) };

    std::cout << vec[0].data << std::endl;
}

有关 的更多信息>std::vector,点击此处

另外,请考虑不在代码中使用以下行:

using namespace std;

..因为它被认为是不好的做法。有关这方面的更多信息,请查看 为什么是“using namespace std”被认为是不好的做法

In your code:

#include <iostream>
using namespace std;

int main()
{
    int* arr = new int;
    arr[0] = 94; // This will work
    arr[1] = 4; // This will cause undefined behaviour
    cout << arr[0] << endl;
}

Int the above code, arr is a pointer to a single int, so you can access that one int using either:

arr[0]
*arr

..but arr1 won't work as there is not enough memory allocated for the array.

To fix this, you must allocate more memory for arr:

int* arr = new int[2];

..and to change the size of arr:

int arr_size = 2;
int* arr = new int[arr_size]; // size of arr = 2
arr[0] = 12;
arr[1] = 13;

int* new_arr = new int[arr_size + 1];

for (int i = 0; i < arr_size; i++)
{
    new_arr[i] = arr[i];
}

delete[] arr;
arr = new_arr;

// size of arr = 3

But all of this gets computationally expensive and time-consuming when arr has a huge number of elements. So I recommend using C++'s std::vector:

Your 2'nd program using std::vector:

#include <iostream>
#include <vector>

struct test
{
    int data;
};
int main()
{
    std::vector<test> vec{ test(4), test(42) };

    std::cout << vec[0].data << std::endl;
}

For more info on std::vector, click here.

Also, consider not using the following line in your code:

using namespace std;

..as it's considered as bad practice. For more info on this, look up to why is "using namespace std" considered as bad practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文