User Defined Types with Dynamic Size in C

I want to define a new data type consisting of an array with the size entered by the user. For example, if the user enters 128, then my program should create a new type, which is basically an array of 16 bytes. This definition of structure should be global, because after that I will use this type in my program. For this structure, you must have a dynamic size, because in the end I will have a huge database filled with this type of variable.

The code I have now is:

struct user_defined_integer; . . . void def_type(int num_bits) { extern struct user_defined_integer { int val[num_bits/sizeof(int)]; }; return; } 

(which does not work)

The closest thing to my question, I found, is here: I need to make a global array in C with the size entered by the user (Which does not help)

Is there a way to do this so that my structure is recognized throughout the file?

+5
source share
4 answers

While doing:

 extern struct user_defined_integer { int val[num_bits/sizeof(int)]; }; 

You should receive a warning:

 warning: useless storage class specifier in empty declaration 

because you have a blank ad. extern does not apply to user_defined_integer , but rather to the variable that comes after it. Secondly, this will not work, because a structure containing a variable-length array cannot have any binding.

 error: object with variably modified type must have no linkage 

However, variable-length arrays allocate memory at the point of declaration. Instead, you should choose dynamic memory.

 #include <stdlib.h> typedef struct { int num_bits; int* val; } user_defined_integer; void set_val(user_defined_integer* udi, int num_bits) { udi->num_bits = num_bits; udi->val = malloc(num_bits/sizeof(int)); } 
+6
source

What you need is the VLA member asked here . Basically, you declare a struct with a size field and one element to store as the last member and redistribute it.

Imported from this question:

 typedef struct Bitmapset { int nwords; uint32 words[1]; } Bitmapset; Bitmapset *allocate(int n) { Bitmapset *p = malloc(offsetof(Bitmapset, words) + n * sizeof *p->words); p->nwords = n; return p; } 
+2
source

I want to define a new data type consisting of an array with the size entered by the user. For example, if the user enters 128, then my program should create a new type, which is basically an array of 16 bytes.

This is not possible in C because C types are compilation time and do not exist at all at run time.

However, with a compiler complying with C99, you can use a flexible array element. You will need a struct containing some elements and ending in an array without any specific dimension, for example

 struct my_flex_st { unsigned size; int arr[]; // of size elements }; 

Here is a way to highlight it:

 struct my_flex_st *make_flex(unsigned siz) { struct my_flex_st* ptr = malloc(sizeof(struct my_flex_st) + siz * sizeof(int)); if (!ptr) { perror("malloc my_flex_st"); exit(EXIT_FAILURE); }; ptr->size = siz; memset (ptr->arr, 0, siz*sizeof(int)); return ptr; } 

Do not forget free , as soon as you no longer use it.

Of course, you will need to use pointers in your code. If you really want to have a global variable, declare it, for example,

 extern struct my_flex_st* my_glob_ptr; 
+2
source

Try this method -

 #include<stdio.h> #include<stdlib.h> #include<limits.h> struct user_defined_integer { int *val; }user_int; void memory_allocate(int num_bit) { int result; result = (num_bit+CHAR_BIT-1)/CHAR_BIT; // since 8 bit =1 byte user_int.val=malloc(result*sizeof(int)); if(user_int.val == NULL){ printf("Failed to allocate memory\n"); return ; } else printf("Allocated %d bytes for val\n",result); } int main() { int num_bit; printf("Enter the number of bits\n"); scanf("%d",&num_bit); memory_allocate(num_bit); // do your stuff here free(user_int.val); // free the memory at the end; return 0; } 
+1
source

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


All Articles