How do I do a memset for a pointer to an array?

2019-07-30 07:18发布

How do I do a memset for a pointer to an array?

int (*p)[2];

p=(int(*))malloc(sizeof(*p)*100);

memset(p,0,sizeof(*p)*100);

Is this allocation an correct?

5条回答
等我变得足够好
2楼-- · 2019-07-30 07:30

The elegant way:

typedef int int_arr_2[2];

int_arr_2* p;

p = malloc(sizeof(int_arr_2)*100);
memset(p,0,sizeof(int_arr_2)*100);

The best way:

typedef int int_arr_2[2];

int_arr_2* p;

p = calloc(100, sizeof(int_arr_2));

calloc, unlike malloc, guarantees that all bytes are set to zero.

查看更多
贼婆χ
3楼-- · 2019-07-30 07:34

The memset() line is proper.

For C you don't need malloc casting.
In C++ if you still want to do this, the type cast should be as:

p = (int(*)[2]) malloc(sizeof(*p)*100);  // or `static_cast<...>
  // ^^^^^^^^^

But I would suggest to change your approach for using std::vector instead. Cleaner, better and "semi-automatic":

std::vector<int*> vi(100);  // or std::vector vi(100, nullptr);

Another way with raw pointers is to use new[]:

int **p = new[100]();  // allocates and sets to 0

But you have to manage this memory later on by deallocating with delete[]

查看更多
在下西门庆
4楼-- · 2019-07-30 07:34

In C (not C++) you would just do

int (*p)[2] = malloc(sizeof(*p)*100);

memset(*p,0,sizeof(*p)*100);

that is, variables can be initialized with expressions and malloc doesn't need (and should not have) a cast. Then *p is an "lvalue" of your array type that decays to a pointer when passed to memset.

查看更多
可以哭但决不认输i
5楼-- · 2019-07-30 07:38

you can use calloc.

calloc will replace both malloc and memset.

p = calloc(100, sizeof (*p));
查看更多
看我几分像从前
6楼-- · 2019-07-30 07:48

I'll summarize a lot of answers (although I've ignored some of the stylistic variants in favor of my own preferences).

In C:

How to use malloc:

int (*p)[2] = malloc(100 * sizeof(*p));

How to use memset:

memset(p, 0, 100 * sizeof(*p));

How to do both in one statement:

int (*p)[2] = calloc(100, sizeof(*p));

In C++, the same is possible except that you need to cast the results of malloc and calloc: static_cast<int(*)[2]>(std::malloc(100 * sizeof(*p)).

However, C++ provides alternative ways to allocate this:

int (*p)[2] = new int[100][2](); // like calloc.
delete[] p; // *not* delete p

C++ also provides vector, which is usually nice, but unfortunately you cannot create a vector of C-style arrays. In C++03 you can workaround like this:

struct my_array {
    int data[2];
};

std::vector<my_array> p(100);
// no need to free/delete anything

I don't think that zeros the elements, although I might be wrong. If I'm right, then to zero you need:

my_array initvalue = {0};
std::vector<my_array> p(100, initvalue);

another way to represent 2 ints:

std::vector<std::pair<int,int> > p(100);

If you can use Boost:

std::vector<boost::array<int, 2> > p(100);

In C++11:

std::vector<std::array<int, 2>> p(100);

I've listed these in increasing order of how good they usually are, so use the last one that isn't blocked by whatever constraints you're working under. For example, if you expect to take a pointer to the first element of one of the inner arrays-of-2-int, and increment it to get a pointer to the second, then std::pair is out because it doesn't guarantee that works.

查看更多
登录 后发表回答