How to make one string assignment to malloc () arrays in C?

In C, I can initialize the array on the stack like this:

SOME_DATA_TYPE* x = (SOME_DATA_TYPE[5]) {v1, v2, v3, v4, v5};

Is there a similar single-line method for assigning values ​​to the malloc()-ed array on the heap?

SOME_DATA_TYPE* y = malloc(sizeof(SOME_DATA_TYPE)*5);  
// what comes here?  

Or do I need to iterate over an array and assign values ​​separately?

+4
source share
3 answers

The first problem of “initializing” the result malloc()is that the distribution may fail. Here it is yinitialized with some pointer value. The data to which he refers are still uncertain.

#define  element_count  5
SOME_DATA_TYPE *y = malloc(sizeof *y * element_count);
if (y == NULL) Handle_OutOfMemory();

C11 , , , y.

memcpy(y, (SOME_DATA_TYPE[element_count]) {v1, v2, v3, v4, v5}, sizeof *y * element_count);

.

// one-liner, but not robust code
SOME_DATA_TYPE *y = memcpy(malloc(sizeof *y * element_count), 
    (SOME_DATA_TYPE[element_count]) {v1, v2, v3, v4, v5}, sizeof *y * element_count);

sizeof *pointer_variable * element_count, sizeof (pointer_variable_dereferenced_type) * element_count, , , . .

+4

​​:

#include <stdarg.h>
#include <stdlib.h>

int * intMallocInit (int length, ...) {
   // Initialize Variable Arguments
   va_list args;
   va_start (args, length); 

    // Allocate Memory
    int * arr = malloc (sizeof(int) * length);
    if (arr == NULL) return NULL;

    // Initialize Array
    for (int i = 0; i < length; i++) {
        arr[i] = va_arg(args, int);
    }

    // Clean Up and Return
    va_end(args)
    return arr;
}

:

int len = 3;
int * myArray = intMallocInit (len, 4, 5, 2);
if (myArray == NULL) return 0; 

for (int i = 0; i < len; i++) {
    printf("%d,", myArray [i]);
}

: 4,5,2,

. floatMallocInit() .., "int" "float"

void enum & , .

, , , .

#include <stdarg.h>
#include <stdlib.h>
enum {INT, FLOAT /* etc...*/}

 int getSize(int type) {
     int typeSize = 0;
     switch (type) {
        case INT: 
             typeSize = sizeof (int);
             break;
        case FLOAT:
             typeSize = sizeof (float);
             break;
        // Etc...
        default:
             return -1;
    }
    return typeSize;
 }


void * mallocInit (int type, int length, ...) {
   // Initialize Variable Arguments
   va_list args;
   va_start (args, length); 

    // Get Size of type
    int typeSize = getSize(type);
    if (typeSize == -1) return NULL;

    // Allocate Memory
    void * arr = malloc (typeSize * length);
    if (arr == NULL) return NULL;

    // Initialize Array, Maybe va_copy can be used? No good reference
    for (int i = 0; i < length; i++) {
         switch(type) {
            case INT:
             arr[i] = va_args(args, int);
             break;
            // Etc..
        }
    }

    // Clean Up and Return
    va_end(args)
    return arr;
}

float myArray = mallocInit (FLOAT, 3, 1, 5, 7);
+2

One way to do this without going too far from C's natural memory management philosophy is to use the memdup-like function, since it is not standard, you may have to implement it yourself

SOME_DATA_TYPE *y = memdup(
  (SOME_DATA_TYPE[5]) {v1, v2, v3, v4, v5},
  5 * sizeof(SOME_DATA_TYPE)); 
+1
source

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


All Articles