ANSI C : test return from calloc API - differ from windows and ibm cics [duplicate]

StackOverflow https://stackoverflow.com/questions/16179255

  •  11-04-2022
  •  | 
  •  

Question

The ANSI C code below returns a valid pointer when compiled with Viasual Studio 2010 (Compile as C Code (/TC)) :

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

void main()
{
    void * p = NULL;
    int i=0;

    p = calloc(0, 100 );

    if ( p != NULL )
    {
        printf ( "Success.\n" );

        free ( p ); 
        p = NULL;
    }
    else
    {
        printf ( "Fail.\n" );
    }
}

The same code using the C compiler from IBM CICS has different behavior, which returns NULL on API call calloc.
The behavior for Visual Studio 2010 is correct?

Since that test the callback has different effects in both cases.

Was it helpful?

Solution

As the documentation from cppreference says:

If size is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be returned that may not be used to access storage)

Just don't do that.

http://en.cppreference.com/w/cpp/memory/c/calloc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top