C (++) malloc confusion

I simply will not allocate memory for arrays in C and mainly in C ++. I searched for examples, but for me there are no useful ones, at least it seems so.

So, if I have a typedef like this:

typedef struct
{
int x;
int y;
} Coordinate;

Coordinate* myList;

And I have an array of type Coordinate, how can I add elements to it dynamically. All I know is what I should use malloc, and then freein C and new/ deletein C ++. (Malloc scares me from me)

So, I aimed for such a function:

void AddSomething ( int x, int y )
{
// myList malloc/new magic here
}

My question is:

  • How should a line that allocates new memory for myList and then add a new element to it look like? Could you show me a working example for C and C ++?

  • malloc C? , ( - , malloc return)

+3
7

.

#include <vector>

typedef struct
{
int x;
int y;
} Coordinate;

std::vector<Coordinate> coordinates;

Coordinate newCoord;
newCoord.x = 1;
newCoord.y = 1;

coordinates.push_back(newCoord);

: malloc/free new/delete,

13:

Bruce Eckels ++ 1. , .

+14

C , :

myList = malloc(sizeof(Coordinate));

n, :

myList = malloc(n * sizeof(Coordinate));

++ n :

myList = new Coordinate[n];

++ , Coordinate . ++ std::vector<Coordinate> .

, malloc() ++, , new (-). , POD . , ++ malloc(), free(), ; new, delete - , . new , . , new, delete, , new, , delete[].

+5

, , . , - , std::vector? , , , .

- ( ) std::vector. , , , , , .

+3

Malloc

- - ; - .

malloc , , , . . , , , , .

free OS, .

++ new delete . "" , .

, ,

  • , , , , (OS : ", ".)
  • , ( ++, c)
  • .
  • , , ( ++, c). , , .
  • , .
+1

C malloc , "" . ++ new, . new . malloc "" , , .

new malloc "" . - . , , .

Coordinate * notObject = (Coordinate*)malloc(sizeof(Coordinate));
Coordinate * object = new Coordinate();

. malloc a void *, . new , .

0
  • , myList, , ? C ++?

: Coordinate, :

Coordinate *list = new Coordinate[ 42 ]; // list can hold at most 42 objects

, , , Coordinate:

typedef Coordinate_t {
    int x, y;
    Coordinate_t *next;
};

.

, C, new, malloc:

Coordinate *list = malloc(*list * 42); /* list can hold at most 42 objects */
  • malloc C? , ( - , ​​ mallocs).

The distribution function uses some OS-specific API to request some memory from free storage (and therefore, it depends on the implementation). For example: On * nix uses a system API called sbrkfriends.

0
source
#include <stdlib.h>

struct
{
int x;
int y;
} Coordinate;

Coordinate* myList = 0;

int myListLength = 0;  // initialize myListLength to size of myList

void AddSomething ( int x, int y )
{
    int i;
    // malloc returns a void pointer, so we cast it to be a Coordinate *
    Coordinate* newList = (Coordinate*)malloc(sizeof(Coordinate)*(myListLength+1));
    for(i=0; i < myListLength; ++i) {
        newList[i] = myList[i];
    }
    newList[myListLength].x = x;
    newList[myListLength].y = y;
    if(myList)
        free(myList);
    myList = newList;
    ++myListLength;
 }

Note that it is much better to use std :: vector if you can.

0
source

Source: https://habr.com/ru/post/1734079/


All Articles