user3419585
user3419585

Reputation: 13

Using Malloc in C: Writing to a dynamic memory buffer

I have the following structure in C

struct _MY_LIST                  
{
    short sRecNum;
    short sConfirm;
    short sFCount;
}my_list;

How do I use malloc to allocate memory for this structure as well to write this structure to dynamic memory?

Upvotes: 0

Views: 1525

Answers (3)

mah
mah

Reputation: 39847

You've defined a structure and a variable composed of the structure, but you need to define a pointer to that structure instead.

Pointers are a difficult topic to master and what I'm about to post will give you a sharp knife to play with -- but you could end up cutting yourself with it if you don't tread lightly! Learning them will take far more than a single SO answer could provide, but at least be sure to read the comments I sprinkled into this code snippet.

struct _MY_LIST                  
{
    short sRecNum;
    short sConfirm;
    short sFCount;
} *my_list_pointer; /* the asterisk says this is a pointer */


/* dynamically allocate the structure */
my_list_pointer = malloc(sizeof(*my_list_pointer));


/* required error checking! */
if (my_list_pointer == NULL) {
    /* do whatever you need, but do _not_ dereference my_list_pointer */
    exit(-1);
}


/* write to the structure */
my_list_pointer->sRecNum = 50;


/* read from the structure */
short the_record_number = my_list_pointer->sRecNum;


/* when finished with the allocation, you must release it */
free(my_list_pointer);

/* now, you must NOT dereference my_list_pointer anymore unless you malloc it again! */

Upvotes: 5

ajay
ajay

Reputation: 9680

First of all, don't use identifiers starting with an underscore because they may clash with the identifiers reserved for the implementation of C, as mentioned by Jens in the comment.

struct myList {                
    short sRecNum;
    short sConfirm;
    short sFCount;
};

struct myList foo;

The above defines a structure of type struct myList. Now you can define variables of this type just like you would with any other type as type identifier;. You can combine the two and define a structure and create an instance of it in the same statement as

struct myList {                
    short sRecNum;
    short sConfirm;
    short sFCount;
} foo;

To dynamically allocate memory for a variable, use malloc declared in the header stdlib.h.

void *malloc(size_t size);

Here, size is the number of bytes to allocate and size_t is a type (typedef, hence _t) to represent the size of objects on the machine. It returns a pointer of type void * which is a generic type for pointers. A void * is assignment compatible to any pointer type, therefore you should not cast the result of malloc. malloc can fail to allocate memory when there isn't enough of it available. You need to check for it whenever you call malloc.

Use the sizeof operator to find the size of a type or a variable in bytes. Note that you don't need to use parentheses when the operand of sizeof is a variable.

struct myList *bar = malloc(sizeof *bar);

if(bar == NULL) {
    // memory allocation failed.
    // handle it
}
else {
    // assign values to the structure members

    bar->sRecNum = 1;
    bar->sConfirm = 2;
    bar->sFCount = 3;

    // do stuff with bar
    // after you are done free the memory allocated by malloc
    // it is a good practice to set bar to NULL after freeing it

    free(bar);
    bar = NULL;
}

Upvotes: 0

Johannes
Johannes

Reputation: 6717

#include <stdlib.h>

struct _MY_LIST                  
{
    short sRecNum;
    short sConfirm;
    short sFCount;
}my_list;


void main()
{   
    my_list * list;

    list = (my_list*) malloc(sizeof(my_list))

    my_list->sRecNum = 1;
    my_list->sConfirm = 2;
    my_list->sFCount = 3;

    free(list);

}

Never forget to free the pointer. I you can avoid it do not use malloc and free in ansi-c.

Here is an alternative if that is possible in your source.

struct _MY_LIST                  
{
    short sRecNum;
    short sConfirm;
    short sFCount;
}my_list;

void uselist(my_list * list);

void main()
{   
    my_list list;

    uselist(&list);
}

void uselist(my_list * list)
{

    list->sRecNum = 1;
    list->sConfirm = 2;
    list->sFCount = 3;

}

Upvotes: 1

Related Questions