Double Ended Queue, Call by Reference (Create Header)

0

i have a simple problem with my c code .... I want to call the variable with call by reference.

I have this function:

void createHeader(HEADER *yourHeader) 
{
yourHeader = malloc(sizeof(HEADER));
if (yourHeader == NULL)
    printError();
yourHeader->first = NULL;
yourHeader->last = NULL;
yourHeader->length = 0;  
}

My main-call:

createHeader(&header);
if (header == NULL)
    exit(-1);

Now I have the following problem: "The program '[6044] My-C-Project.exe' has exited with code -1 (0xffffffff)", so that means my function createHeader doens't work ... Can someone please explain what's the mistake is?

Regards

Alex

c
list
pointers
malloc
asked on Stack Overflow May 12, 2019 by BlindRob

1 Answer

0

Change the function to:

HEADER *createHeader(void) 
{
    HEADER *yourHeader = malloc(sizeof(HEADER));
    if (yourHeader == NULL)
        printErrorAndExit();
    yourHeader->first = NULL;
    yourHeader->last = NULL;
    yourHeader->length = 0;
    return yourHeader;
}

Use:

Header *header = createHeader();
if (header == NULL)
    exit(-1);

Alternatively, use a pointer to a pointer as the argument:

void createHeader(HEADER **yourHeader) 
{
    *yourHeader = malloc(sizeof(HEADER));
    if (*yourHeader == NULL)
        printError();
    (*yourHeader)->first = NULL;
    (*yourHeader)->last = NULL;
    (*yourHeader)->length = 0;
}

Use:

Header *header = NULL;
createHeader(&header);
if (header == NULL)
    exit(EXIT_FAILURE);

These two techniques can be applied to most 'allocate pointer' operations — creating linked lists is a particularly common variation. Note that fopen() and fclose() match the first model; the allocator (fopen()) takes some arguments to tell it what to do and the function returns a pointer to a structure that can be used and eventually passed to fclose() for release.

Note that printErrorAndExit() must not return. Or, if it does, the functions have to be revised:

HEADER *createHeader(void) 
{
    HEADER *yourHeader = malloc(sizeof(HEADER));
    if (yourHeader == NULL)
        printError();
    else
    {
        yourHeader->first = NULL;
        yourHeader->last = NULL;
        yourHeader->length = 0;
    }
    return yourHeader;
}

It might be better for the function not to print any error message — that limits its reusability, in general.

HEADER *createHeader(void) 
{
    HEADER *yourHeader = malloc(sizeof(HEADER));
    if (yourHeader != NULL)
    {
        yourHeader->first = NULL;
        yourHeader->last = NULL;
        yourHeader->length = 0;
    }
    return yourHeader;
}

The alternative design should probably return a status instead of nothing, so you can test the function result directly:

int createHeader(HEADER **yourHeader) 
{
    int rc = 0;
    *yourHeader = malloc(sizeof(HEADER));
    if (*yourHeader == NULL)
        rc = -1;   /* Or perhaps errno */
    else
    {
        (*yourHeader)->first = NULL;
        (*yourHeader)->last = NULL;
        (*yourHeader)->length = 0;
    }
    return rc;
}

Use:

if (createHeader(&header) != 0)
    …report error and bail out…

Using 0 for success and non-zero for failure is very common (many Unix system calls fall into this category). Very often, a negative return value indicates failure, especially if a positive value can be used for success — think open(). But the POSIX pthread functions frequently return a positive error number on failure and zero on success.

answered on Stack Overflow May 12, 2019 by Jonathan Leffler • edited May 12, 2019 by Jonathan Leffler

User contributions licensed under CC BY-SA 3.0