-3
struct forcePin {
    char _name[512];
};

struct forcePin *_forcePin[500000];    
_forcePin[i] = (struct forcePin *) malloc (sizeof (struct forcePin));

May I know what is the line as shown below doing?

_forcePin[i] = (struct forcePin *) malloc (sizeof (struct forcePin));

I am not familiar with c,if you can tell me how to make this line to be in C++ format as well.Thanks

Chinna
  • 3,930
  • 4
  • 25
  • 55
Jaden Ng
  • 141
  • 1
  • 12

5 Answers5

7

Dynamic memory allocation is so important in C that you should really learn it properly.

What the line in question does is allocating memory from the heap, namely sizeof(struct forcePin) bytes. The malloc function returns a generic pointer to this allocated memory, and that pointer is assigned to the pointer _forcePin[i].

One thing about that line, you should not type-cast the return value of the malloc function.


In C++ you use the new statement to allocate pointers:

_forcePin[i] = new forcePin;

However, in C++ using pointers and dynamic heap allocations is discouraged. I would instead recommend you to use a std::vector of non-pointer structures:

struct forcePin {
    std::string name;
};

std::vector<forcePin> forcePin;
forcePin.push_back(forcePin{});
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
4

It is calling the standard library function malloc(), to allocate sizeof (struct forcePin) bytes of dynamic ("heap") memory.

It is then pointlessly casting the returned pointer, and storing it in the variable _forcePin[i].

It's not the optimal way to write this code, in my opinion it should be:

_forcePin[i] = malloc(sizeof *_forcePin[i]);

Note that if the allocation is broken out of a loop (as the i implies), then that code looks like it's allocating 512 * 500,000 bytes, or around 244 MB of memory. Since it's done in half a million allocation calls, there will be considerable overhead, too.

If all the memory really is needed, it would be better to try for a single malloc() call and then split the allocated buffer into the 500,000 parts. Doing it that way would very likely be faster since malloc() can be expensive and half a million calls is a lot, but it would certainly save memory since there would be a one overhead cost rather than 500,000.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    Don't you feel a little bit sad when people **still** cast the friggin' void pointer, 4 years after your excellent explanation as to why it's discouraged? :) –  Jul 10 '13 at 08:29
  • 1
    @H2CO3 Haha! :D Yes, it's saddening. It seems to be a powerful "anti-meme", no idea why it's so entrenched. I see it in textbooks, makes me want to shout. Luckily I can post links here, instead. :) – unwind Jul 10 '13 at 08:51
1
_forcePin[i] = (struct forcePin *) malloc (sizeof (struct forcePin));

Allocates a memory block of size forcePin, and casts the allocated memory from (void *) to the forcePin type. In C++ you would do:

_forcePin[i] = new forcePin();

or better yet, you can have:

std::vector<forcePin> vec;
vec.push_back(forcePin());
perreal
  • 94,503
  • 21
  • 155
  • 181
1

This line is your generic C-style memory allocation:

  1. allocate memory (malloc) for exact number of bytes as is needed for structure forcePin (sizeof(struct forcePin)).
  2. as the return value of malloc is a void pointer (void *) pointing to newly allocated memory, cast it to the pointer to forcePin structure ((struct forcePin *))

C++ version would be something like:

_forcePin[i] = new forcePin;
  • in C++, struct keyword is unnecessary when refering to struct type)
  • don't forget to free the memory when not needed by delete _forcePin[i]

Because it looks like you just may want to create all 500000 forcePins, you may do in in one step:

forcePin _forcePins = new forcePin[500000];
Jan Spurny
  • 5,219
  • 1
  • 33
  • 47
0

The line you ask about allocate a block of memory (see malloc()), and stores its address into the _forcePin array, at index i.

In C++, you would have used new, ie: _forcePin[i] = new forcePin

Xaqq
  • 4,308
  • 2
  • 25
  • 38