Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays inside structs in C

I´m struggling to understand this concept: I have a fixed size definition:

(from http://msdn.microsoft.com/pt-br/library/aa931918.aspx)

typedef struct _FlashRegion {
  REGION_TYPE regionType;
  DWORD dwStartPhysBlock;
  DWORD dwNumPhysBlocks;
  DWORD dwNumLogicalBlocks;
  DWORD dwSectorsPerBlock;
  DWORD dwBytesPerBlock;
  DWORD dwCompactBlocks;
} FlashRegion, *PFlashRegion;

this FlashRegion struct, is used in this another struct: (from: http://msdn.microsoft.com/pt-br/library/aa932688.aspx)

typedef struct _FlashInfoEx {
  DWORD cbSize;
  FLASH_TYPEflashType;
  DWORD dwNumBlocks;
  WORD dwDataBytesPerSector;
   DWORD dwNumRegions;
  FlashRegion region[1]; 
} FlashInfoEx, *PFlashInfoEx;

The problem is, I can have a variable number of FlashRegions inside a FlashInfoEx. The function that I´m debugging does this somewhere in the code:

 memcpy (pFlashInfoEx->region,  g_pStorageDesc->pRegionTable,
         g_pStorageDesc->dwNumRegions *  sizeof(FlashRegion));

That means that it copies an amount of regions to pFlashInfoEx (that I pass in the call of the function);

So, the code will overwrite memory if dwNumRegions is bigger than one. If that is the case, Should I create a FlashRegion [FIXED_SIZE] in my code and somehow place/overwrite in FlashInfoEx->region? How do I do that?

Thanks, Marcelo

like image 741
Marcelo Avatar asked Sep 08 '09 17:09

Marcelo


People also ask

Can you put an array in a struct in C?

A structure is a data type in C/C++ that allows a group of related variables to be treated as a single unit instead of separate entities. A structure may contain elements of different data types – int, char, float, double, etc. It may also contain an array as its member.

Can we declare array inside structure?

For the structures in C programming language from C99 standard onwards, we can declare an array without a dimension and whose size is flexible in nature. Such an array inside the structure should preferably be declared as the last member of structure and its size is variable(can be changed be at runtime).

What is flexible array in C?

Flexible Array members in a structure in C means we can declare array without its dimension within a structure and its size will be flexible in nature. Flexible array member must be the last member of class.


2 Answers

The concept is while FlashRegion looks like a fixed size structure, it is actually dynamically sized. The magic is done when allocating the structure - instead of calling (FlashRegion*)malloc(sizeof(FlashInfoEx)) or new FlashRegion, you call something like (FlashRegion*)malloc(sizeof(FlashInfoEx)+sizeof(FlashRegion)*(numRegions-1))

like image 58
Suma Avatar answered Sep 21 '22 12:09

Suma


This is a fairly common C idiom for variably sized structures. In order to make this work, you need to ensure you allocate enough memory for the array size you want, usually something like

pFlashInfoEx = malloc(offsetof(PFlashInfoEx, region) + g_pStorageDesc->dwNumRegions *  sizeof(FlashRegion));

this interacts badly with trying to use 'new' in C++; you have to allocate the memory manually:

void *mem = ::operator new(offsetof(PFlashInfoEx, region) + g_pStorageDesc->dwNumRegions *  sizeof(FlashRegion));
pFlashInfoEx = new(mem) PFlashInfoEx;
for (int i = 1; i < g_pStorageDesc->dwNumRegions; i++)
    new (&pFlashInfoEx->region[i]) FlashRegion;
like image 33
Chris Dodd Avatar answered Sep 20 '22 12:09

Chris Dodd