# limits

***

**1. Array Size Limit**

```c
#define MAX_ARRAY_SIZE 10

int arr[MAX_ARRAY_SIZE];
```

This defines an array with a maximum size of 10 elements.

**2. Function Parameter Count Limit**

```c
#define MAX_PARAMS 10

void func(int a, int b, ...) {
  // Code here
}
```

This defines a function that can take up to 10 parameters, including the named parameters.

**3. Pointer Dereference Limit**

```c
int *ptr = NULL;

if (ptr != NULL) {
  *ptr = 10;
}
```

This checks if a pointer is valid before dereferencing it to prevent a segmentation fault.

**4. File Size Limit**

```c
#define MAX_FILE_SIZE 1024

FILE *fp = fopen("file.txt", "r");

if (fseek(fp, 0, SEEK_END) == 0) {
  long file_size = ftell(fp);
  if (file_size > MAX_FILE_SIZE) {
    // Handle file too large condition
  }
}
```

This checks the size of a file and compares it to a maximum size limit.

**5. String Length Limit**

```c
#define MAX_STRING_LENGTH 256

char str[MAX_STRING_LENGTH];
```

This defines a string with a maximum length of 256 characters.

**6. Loop Iteration Limit**

```c
#define MAX_ITERATIONS 1000

for (int i = 0; i < MAX_ITERATIONS; i++) {
  // Code here
}
```

This defines a loop that iterates a maximum number of times.

**7. Recursion Depth Limit**

```c
#define MAX_RECURSION_DEPTH 10

int func(int n) {
  if (n <= 0) {
    return 1;
  }
  if (n > MAX_RECURSION_DEPTH) {
    // Handle recursion too deep condition
  }
  return n * func(n - 1);
}
```

This defines a recursive function with a maximum recursion depth.

**8. Stack Size Limit**

```c
#define MAX_STACK_SIZE 1024

char stack[MAX_STACK_SIZE];
```

This defines a stack with a maximum size of 1024 bytes.

**9. Queue Size Limit**

```c
#define MAX_QUEUE_SIZE 100

Queue *queue = create_queue(MAX_QUEUE_SIZE);
```

This creates a queue with a maximum size of 100 elements.

**10. Linked List Size Limit**

```c
#define MAX_LINKED_LIST_SIZE 1000

struct node {
  int data;
  struct node *next;
};

struct node *head = NULL;

if (size_of_linked_list(head) < MAX_LINKED_LIST_SIZE) {
  // Add new node to linked list
}
```

This checks the size of a linked list and compares it to a maximum size limit.

**11. Binary Tree Height Limit**

```c
#define MAX_BINARY_TREE_HEIGHT 10

struct node {
  int data;
  struct node *left;
  struct node *right;
};

int height_of_binary_tree(struct node *root) {
  if (root == NULL) {
    return 0;
  }
  int left_height = height_of_binary_tree(root->left);
  int right_height = height_of_binary_tree(root->right);
  return 1 + max(left_height, right_height);
}
```

This calculates the height of a binary tree and checks if it exceeds a maximum height limit.

**12. Graph Node Count Limit**

```c
#define MAX_GRAPH_NODES 100

struct node {
  int data;
  struct node *next;
};

struct graph {
  struct node *nodes[MAX_GRAPH_NODES];
};

if (num_of_nodes_in_graph(graph) < MAX_GRAPH_NODES) {
  // Add new node to graph
}
```

This checks the number of nodes in a graph and compares it to a maximum node count limit.

**13. Array Index Range Check**

```c
int arr[10];

for (int i = 0; i < 10; i++) {
  if (i < 0 || i >= 10) {
    // Handle invalid index condition
  }
  arr[i] = i;
}
```

This checks the indices of an array to ensure they are within the valid range.

**14. Pointer Assignment Range Check**

```c
int *ptr = NULL;

if (ptr < &arr[0] || ptr > &arr[9]) {
  // Handle invalid pointer assignment condition
}
```

This checks the range of a pointer to ensure it points to a valid memory location within an array.

**15. Queue Enqueue Limit**

```c
#define MAX_QUEUE_SIZE 10

Queue *queue = create_queue(MAX_QUEUE_SIZE);

if (queue_is_full(queue)) {
  // Handle queue full condition
}
else {
  // Enqueue element
}
```

This checks if a queue is full and prevents enqueueing if it is full.

**16. Priority Queue Priority Range**

```c
struct p_queue {
  int *arr;
  int size;
};

void insert_in_p_queue(struct p_queue *p_queue, int priority, int data) {
  if (priority < 1 || priority > 10) {
    // Handle invalid priority condition
  }
  // Insert element in priority queue
}
```

This checks the priority range to ensure it is within valid limits.

**17. Circular Buffer Capacity Limit**

```c
#define MAX_CIRCULAR_BUFFER_SIZE 100

struct circular_buffer {
  int data[MAX_CIRCULAR_BUFFER_SIZE];
  int head;
  int tail;
  int count;
};

if (circular_buffer_is_full(circular_buffer)) {
  // Handle circular buffer full condition
}
else {
  // Enqueue element
}
```

This checks if a circular buffer is full and prevents enqueuing if it is full.

**18. Binary Search Tree Value Range**

```c
struct node {
  int data;
  struct node *left;
  struct node *right;
};

void insert_in_bst(struct node **root, int data) {
  if (data < root->data) {
    // Insert in left subtree
  }
  else if (data > root->data) {
    // Insert in right subtree
  }
  else {
    // Handle duplicate value condition
  }
}
```

This checks the value range of a binary search tree to ensure that each node has a distinct value.

**19. Hash Table Size Limit**

```c
#define MAX_HASH_TABLE_SIZE 100

struct hash_table {
  int data[MAX_HASH_TABLE_SIZE];
};

if (hash_table_is_full(hash_table)) {
  // Handle hash table full condition
}
else {
  // Insert element
}
```

This checks if a hash table is full and prevents inserting if it is full.

**20. Function Parameter Type Check**

```c
void func(int a, float b) {
  if (a < 0 || b < 0) {
    // Handle invalid parameter type condition
  }
  // Code here
}
```

This checks the types of parameters passed to a function to ensure they are valid.

**21. File Read Limit**

```c
FILE *fp = fopen("file.txt", "r");

if (fp == NULL) {
  // Handle file open error condition
}
else {
  char buffer[MAX_READ_BUFFER_SIZE];
  while (fgets(buffer, MAX_READ_BUFFER_SIZE, fp) != NULL) {
    // Process data
  }
  fclose(fp);
}
```

This checks if a file can be opened for reading and limits the amount of data read at once.

**22. File Write Limit**

```c
FILE *fp = fopen("file.txt", "w");

if (fp == NULL) {
  // Handle file open error condition
}
else {
  fprintf(fp, "Hello world!");
  fclose(fp);
}
```

This checks if a file can be opened for writing and limits the amount of data written at once.

**23. Socket Connection Limit**

```c
#define MAX_SOCKET_CONNECTIONS 10

int socket = socket(AF_INET, SOCK_STREAM, 0);

if (socket < 0) {
  // Handle socket creation error condition
}
else {
  int optval = 1;
  setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));

  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(PORT);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);

  int bind_result = bind(socket, (struct sockaddr *)&addr, sizeof(addr));

  if (bind_result < 0) {
    // Handle bind error condition
  }
  else {
    int listen_result = listen(socket, MAX_SOCKET_CONNECTIONS);

    if (listen_result < 0) {
      // Handle listen error condition
    }
    else {
      // Accept client connections
    }
  }
}
```

This checks the maximum number of concurrent socket connections allowed.

**24. Process Memory Limit**

```c
#include <sys/resource.h>

struct rlimit limit;

getrlimit(RLIMIT_AS, &limit);

if (limit.rlim_cur < memory_limit) {
  // Handle process memory limit condition
}
```

This checks the current memory limit of a process and compares it to a desired limit.

**25. Process File Descriptor Limit**

```c
#include <sys/resource.h>

struct rlimit limit;

getrlimit(RLIMIT_NOFILE, &limit);

if (limit.rlim_cur < file_descriptor_limit) {
  // Handle process file descriptor limit condition
}
```

This checks the current file descriptor limit of a process and compares it to a desired limit.

**26. Signal Handler Limit**

```c
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGINT);
sigaddset(&sigset, SIGTERM);

int result = sigprocmask(SIG_UNBLOCK, &sigset, NULL);

if (result == -1) {
  // Handle signal handler limit condition
}
```

This checks the limit on the number of signal handlers that can be registered for a given process.

**27. Thread Creation Limit**

```c
#include <pthread.h>

pthread_attr_t attr;
pthread_attr_init(&attr);

int result = pthread_attr_setstacksize(&attr, stack_size_limit);

if (result != 0) {
  // Handle thread creation limit condition
}
```

This checks the limit on the number of threads that can be created in a process.

**28. Process Priority Limit**

```c
#include <sys/resource.h>

int priority = getpriority(PRIO_PROCESS, 0);

if (priority < priority_limit) {
  // Handle process priority limit condition
}
```

This checks the limit on the priority of a process.

**29. Process Runtime Limit**

```c
#include <sys/time.h>

struct timeval timeout;
timeout.tv_sec = timeout_value;
timeout.tv_usec = 0;

int result = setitimer(ITIMER_REAL, &timeout, NULL);

if (result == -1) {
  // Handle process runtime limit condition
}
```

This checks the limit on the amount of time a process can run.

**30. Input Buffer Size Limit**

```c
#define MAX_INPUT_BUFFER_SIZE 1024

char buffer[MAX_INPUT_BUFFER_SIZE];

fgets(buffer, MAX_INPUT_BUFFER_SIZE, stdin);

if (strlen(buffer) > MAX_INPUT_BUFFER_SIZE) {
  // Handle input buffer size limit condition
}
```

This checks the limit on the size of input that can be accepted from the standard input stream.

**31. Output Buffer Size Limit**

```c
#define MAX_OUTPUT_BUFFER_SIZE 1024

char buffer[MAX_OUTPUT_BUFFER_SIZE];

sprintf(buffer, "Hello world!");

if (strlen(buffer) > MAX_OUTPUT_BUFFER_SIZE) {
  // Handle output buffer size limit condition
}
```

This checks the limit on the size of output that can be written to the standard output
