动态分配数组(向量实现的动态大小)

发布于 2024-12-01 06:30:52 字数 206 浏览 3 评论 0原文

在 obj-c 中,我们可以按如下方式创建向量对象:

SomeClass* example[100];

或者

int count[7000];

但是如果我们仅在初始化类时才知道向量的大小怎么办? (也许我们需要 example[756] 或 count[15])

In the obj-c, we can create vector objects as follows:

SomeClass* example[100];

or

int count[7000];

But what if we know the size of the vector only at the time init the class?
(Maybe we need example[756] or count[15])

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

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

发布评论

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

评论(4

山色无中 2024-12-08 06:30:52

首先,这些不是向量对象,它们是编译时数组。编译时数组的特点之一是自动内存管理;也就是说,您不必担心这些数组的分配和释放。

运行时才知道其大小的数组,则需要使用 new[]delete[]

int size = somenumber;
int* arr = new int[size];

// use arr
arr[0] = 4;

// print the first value of arr which is 4
cout << arr[0];

如果您想创建一个直到 你已经完成了这个数组,你必须释放它:

delete[] arr;

如果你忘记用相应的delete释放由new创建的东西1,您将创建一个内存泄漏

不过,您可能最好使用 std::vector,因为它会自动为您管理内存:

// include the header
#include <vector>

using namespace std; // so we don't have std:: everywhere

vector<int> vec; // create a vector for ints
vec.push_back(4); // add some data
vec.push_back(5);
vec.push_back(6);

// vec now holds 4, 5, and 6

cout << vec[0]; // print the first element of vec which is 4

// we can add as many elements to vec as we want without having to use any
// deallocation functions on it like delete[] or anything
// when vec goes out of scope, it will clean up after itself and you won't have any leaks

1 确保对您希望使用的指针使用 delete在使用 new[x] 创建的指针上使用 newdelete[] 创建。 不要混合搭配它们。同样,如果您使用 std::vector,则不必担心这一点。

First of all, those aren't vector objects, they're compile-time arrays. One of the features of compile time arrays is automatic memory management; that is, you don't have to worry about allocation and deallocation of these arrays.

If you want to create an array whose size you don't know until runtime, you'll need to use new[] and delete[]:

int size = somenumber;
int* arr = new int[size];

// use arr
arr[0] = 4;

// print the first value of arr which is 4
cout << arr[0];

The catch is that after you're done with this array, you have to deallocate it:

delete[] arr;

If you forget to deallocate something created by new with a corresponding delete1, you'll create a memory leak.

You are probably better off using std::vector though because it manages memory for you automatically:

// include the header
#include <vector>

using namespace std; // so we don't have std:: everywhere

vector<int> vec; // create a vector for ints
vec.push_back(4); // add some data
vec.push_back(5);
vec.push_back(6);

// vec now holds 4, 5, and 6

cout << vec[0]; // print the first element of vec which is 4

// we can add as many elements to vec as we want without having to use any
// deallocation functions on it like delete[] or anything
// when vec goes out of scope, it will clean up after itself and you won't have any leaks

1 Make sure you use delete on pointers that you created with new and delete[] on pointers you make with new[x]. Do not mix and match them. Again, if you use std::vector, you don't have to worry about this.

御弟哥哥 2024-12-08 06:30:52

为什么不只使用 std::vector

//file.mm
#include <vector>
-(void)function
{
std::vector<int> count;
std::vector<SomeClass*> example;
count.push_back(10); // add 10 to the array;
count.resize(20); // make count hold 20 objects
count[10] = 5; //set object at index of 10 to the value of 5
}

Why not just use an std::vector

//file.mm
#include <vector>
-(void)function
{
std::vector<int> count;
std::vector<SomeClass*> example;
count.push_back(10); // add 10 to the array;
count.resize(20); // make count hold 20 objects
count[10] = 5; //set object at index of 10 to the value of 5
}
夜空下最亮的亮点 2024-12-08 06:30:52

然后你做类似的事情:

SomeClass **example = calloc(numClasses, sizeof(SomeClass *));

或:

int *count = malloc(num_of_counts * sizeof(int));

请注意,你应该:

#include <stdlib.h>

Then you do something like:

SomeClass **example = calloc(numClasses, sizeof(SomeClass *));

or:

int *count = malloc(num_of_counts * sizeof(int));

Note that you should:

#include <stdlib.h>
柳若烟 2024-12-08 06:30:52

C++ 无法创建可变大小的全局/局部数组,只能创建堆上的动态数组。

int main() {
    int variable = 100;
    SomeClass* example = new SomeClass[variable];
    //do stuff 
    delete [] example;  //DO NOT FORGET THIS.  Better yet, use a std::vector
    return 0;
}

我对 Objective-C 一无所知,但你的问题可能只是其中之一。

C++ cannot make global/local arrays of a variable size, only dynamic arrays on the heap.

int main() {
    int variable = 100;
    SomeClass* example = new SomeClass[variable];
    //do stuff 
    delete [] example;  //DO NOT FORGET THIS.  Better yet, use a std::vector
    return 0;
}

I don't know anything about objective-C, but your question is probably only one or the other.

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