最初mallocate 0元素后重新分配并测量尺寸 [英] Initially mallocate 0 elements to later reallocate and measure size

查看:313
本文介绍了最初mallocate 0元素后重新分配并测量尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,将由每次调用时重新分配新的内存添加一个新的位置阵列功能。

I have a function that will add a new position to an array by reallocating new memory every time it is called.

问题是,每个呼叫我需要它在第一次调用一个位置添加到数组,起价1,但我明白,我必须重新分配前mallocate。

The problem is that, for each call I need it to add one position to the array, starting from 1 at first call, but I understand that I have to mallocate before reallocating.

所以我的问题是,我可以开始做一些像 P =的malloc(0),然后重新分配,例如使用 P =(INT * )的realloc(p,的sizeof(INT))我的函数内部? P 声明为为int * P

So my question is, can I initially do something like p = malloc(0) and then reallocate for example using p = (int *)realloc(p,sizeof(int)) inside my function? p is declared as int *p.

用不同的语法可能?

当然,我可以让我的函数的条件,将mallocate如果内存没有被分配之前并重新分配,如果有,但我正在寻找一个更好的办法。

Of course I could make a condition in my function that would mallocate if memory hasn't been allocated before and reallocate if it has, but I am looking for a better way.

和我有第二个问题......一旦重新分配更多的位置,我想知道数组的大小。

And the second problem I have is... Once reallocated more positions, I want to know the size of the array.

我知道,如果,例如,我宣布一个数组 A [10] 元素的数量将由的sizeof(定义)/的sizeof(A [0]),但出于某种原因,不声明为指针,然后重新分配数组。

I know that if, for example, I declare an array a[10], the number of elements would be defined by sizeof(a)/sizeof(a[0]), but for some reason that doesn't work with arrays declared as pointers and then reallocated.

任何意见?

推荐答案

您可以将您的指针初始化为 NULL ,让你第一次叫的realloc(yourPointer,yourSize),它将返回相同的值的malloc(yourSize)

You could initialize your pointer to NULL, so that the first time you call realloc(yourPointer, yourSize), it will return the same value as malloc(yourSize).

有关你的第二个问题,你可以使用包含您的指针和 A结构计数成员。

For your second problem, you could use a struct that contains your pointer and a count member.

struct MyIntVector {
    int * ptr;
    size_t count;
}

,那么你可能会想定义的malloc,realloc的,免费包装函数(在那里你可以重置 PTR 为NULL),这需要你的结构为一体参数,并升级为所需的结构。

Then you probably will want to define wrapper functions for malloc, realloc, and free (where you could reset ptr to NULL), that takes your struct as one of the parameters, and updates the struct as needed.

如果你想优化本作同时推1元,您可以添加一个 allocatedCount 成员,并且仅当的realloc 计数== allocatedCount ,用新的 allocatedCount 等于(例如)两倍的老 allocatedCount

If you want to optimize this for pushing 1 element at a time, you could add a allocatedCount member, and only realloc if count == allocatedCount, with a new allocatedCount equals (for example) twice the old allocatedCount.

您应该在 MyIntVector_Push(MyIntVector *,INT)函数实现这一点。

You should implement this in a MyIntVector_Push(MyIntVector *, int ) function.

您届时将有C ++ 的std ::矢量&lt的简化C版本; INT方式> (但没有自动释放当对象超出范围)

You will then have a simplified c version of c++ std::vector<int> (but without automatic deallocation when the object goes out of scope).

这篇关于最初mallocate 0元素后重新分配并测量尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆