C Simple Linear Array of Dynamically (Malloc'd) Structures

Here is a simple program which has init, insert, and search functions to create a linear array of malloc'd structures in a ring-buffer type style'd program. It was valgrinded and only has warnings due to the size of size_t on x86_64.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>

typedef struct mystruct_s {
        int fd;
        int a;
        int b;
} mystruct_t;

int current_elements = 0;
int max_elements = 0;

int init(int size, mystruct_t ** conn_list)
{
        if (size <= 0) {
                return (-1);
        }
        current_elements = 0;

        *conn_list = malloc(max_elements *sizeof(mystruct_t));

        if (*conn_list == NULL) {
                return (-1);
        }

        return (0);
}

void insert(int fd, mystruct_t ** conn_list, mystruct_t ** res)
{

        *res = NULL;
       
        if (current_elements == (max_elements -1)) {
                (*conn_list)[current_elements].fd = fd;
                (*conn_list)[current_elements].a = fd;
                (*conn_list)[current_elements].b = fd;
                *res = &(*conn_list)[current_elements];
                current_elements = 0;
        } else {
                (*conn_list)[current_elements].fd = fd;
                (*conn_list)[current_elements].a = fd;
                (*conn_list)[current_elements].b = fd;
                *res = &(*conn_list)[current_elements];
                current_elements++;
        }
}

int search(int fd, mystruct_t ** conn_list, mystruct_t ** res)
{
        int i = 0;
        if (*conn_list == NULL) {
                *res = NULL;           
                return (-1);
        }
        for (i = 0; i < max_elements; i++) {
                if ((*conn_list)[i].fd == fd) {
                        *res = &(*conn_list)[i];
                        return (1);
                }

        }
        *res = NULL;
        return (-1);
}

#define MAX_ELEMENTS 8
int main(int argc, char **argv)
{
        int size = MAX_ELEMENTS;
        mystruct_t conn_array[MAX_DEVS] = { 0 };
        mystruct_t *conn_list = NULL;

        max_elements = MAX_DEVS;

        if (init(size, &conn_list) < 0) {
                return (-1);
        }

        mystruct_t *ptr = NULL;
        int i = 0;
        for (i = 0; i < max_elements; i++) {
                conn_array[i].fd = i;
                insert(conn_array[i].fd, &conn_list, &ptr);
        }

        if ((search(conn_array[5].fd, &conn_list, &ptr)) > 0) {
                printf("Found: %d\n", ptr->fd);
        } else {
                printf("NOT found: %d\n", conn_array[5].fd);
        }

        if (conn_list != NULL) {
                free(conn_list);
        }
        return (0);
}

Blog tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <python> <c>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.