\r\n

51Degrees Device Detection C/C++  4.4

A device detection library that is used natively or by 51Degrees products

Data

Detailed Description

Structure containing memory allocated to store a variable.

Terms

Data : a structure containing memory allocated to a certain item. This can be of any type.

Introduction

A Data structure contains memory allocated for the purpose of storing a variable. This differs from a direct pointer in that the memory can be reused for another purpose. By keeping track of how much data has been allocated, the same allocation can be reused if memory of equal or smaller size is needed, otherwise more memory can be automatically allocated in the same method call.

Creation

A Data structure does not need to be created by a method, it only needs to be allocated in memory. However, it does need to be reset before any operations are carried out with it.

Allocation

To allocate memory for a variable in a Data structure, the fiftyoneDegreesDataMalloc method can be called in a similar way to the standard malloc method. Any existing variable in the Data structure will either be overwritten by the new variable, or the previous variable will be freed and new memory allocated.

Example Usage

// Allocate a data structure
// Reset the data ready to allocate something
// Create the data to store
const char *string = "some data";
size_t size = strlen(string) * sizeof(char);
// Allocate the memory inside the data structure
void *dataPtr = fiftyoneDegreesDataMalloc(data, size);
// Check the allocation was succesful
if (dataPtr != NULL) {
// Set the data in the data structure and size it uses
strcpy((char*)dataPtr, string);
data->used = size;
}

Collaboration diagram for Data:

Structs

struct  fiftyoneDegreesData
Data structure used for reusing memory which may have been allocated in a previous operation. More...

Typedefs

typedef unsigned char  byte
Alias for unsigned char.

Functions

void  fiftyoneDegreesDataReset (fiftyoneDegreesData *data)
Resets the data structure ready for a new operation. More...
void *  fiftyoneDegreesDataMalloc (fiftyoneDegreesData *data, size_t bytesNeeded)
Ensures the data structure contains sufficient bytes. More...

Function Documentation

◆ fiftyoneDegreesDataMalloc()

void* fiftyoneDegreesDataMalloc ( fiftyoneDegreesData *   data,
size_t   bytesNeeded  
)

Ensures the data structure contains sufficient bytes.

If insufficient bytes are available then the current memory is freed and a new block of memory is allocated.

Parameters
data - pointer to the data structure to be checked for sufficient bytes
bytesNeeded - the number of bytes the data needs to be able to store
Returns
a pointer to the memory held within data

◆ fiftyoneDegreesDataReset()

void fiftyoneDegreesDataReset ( fiftyoneDegreesData *   data )

Resets the data structure ready for a new operation.

MUST be called before using an instance of fiftyoneDegreesData.

Parameters
data - to be reset
On This Page