# Ccall, NULL pointers to arrays and sizeof

**URL:** <https://discourse.julialang.org/t/ccall-null-pointers-to-arrays-and-sizeof/984>\
**Category:** General Usage\
**Tags:** ccall\
**Created:** [December 15, 2016, 10:54pm UTC](https://discourse.julialang.org/t/ccall-null-pointers-to-arrays-and-sizeof/984 "2016-12-15T22:54:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtravs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtravs/32/2010_2.png) [@jtravs](https://discourse.julialang.org/u/jtravs)\
**Post date:** [December 15, 2016, 10:54pm UTC](https://discourse.julialang.org/t/ccall-null-pointers-to-arrays-and-sizeof/984/1 "2016-12-15T22:54:23Z")

</div>

I am trying to wrap the Vimba C API (for grabbing data from machine vision cameras). I’m struggling with one aspect (it may be that it is my lack of C knowledge).

The function I’m currently wrapping is declared roughly as:

```nohighlight
VmbError_t VmbCamerasList ( VmbCameraInfo_t* pCameraInfo,
                            VmbUint32_t listLength,
                            VmbUint32_t* pNumFound,
                            VmbUint32_t sizeofCameraInfo );

```

First we are supposed to call this function with a NULL pointer for `pCameraInfo` to retreive the numer of cameras in the 3rd argument. i.e.

```julia
ncameras = Ref{VmbUint32_t}(0)
err = ccall((:VmbCamerasList,vmblib), VmbError_t,
            (Ptr{VmbCameraInfo_t}, VmbUint32_t, Ref{VmbUint32_t}, VmbUint32_t),
            C_NULL, 0, ncameras, sizeof(Ptr{VmbCameraInfo_t}))

```

My first question is what I should be doing with the last argument? The example in the API docs uses:

```nohighlight
VmbCameraInfo_t *pCameras = NULL; // A list of camera details
VmbUint32_t nCount // Number of found cameras
err = VmbCamerasList( NULL, 0, &nCount, sizeof *pCameras );

```

What is the Julia equivalent of `sizeof *pCameras` ? As far as I know, in C this just returns the size of the pointer type, not the size of `VmbCameraInfo_t ` let alone the array it points to.

Subsequently I am supposed to call with a correctly allocated array. The C example is:

```nohighlight
pCameras = (VmbCameraInfo_t*) malloc( nCount * sizeof( *pCameras ));
if ( NULL != pCameras ) {
    err = VmbCamerasList( pCameras, nCount, &nFoundCount, sizeof *pCameras ); }

```

which I translated as

```julia
cameras = Array{VmbCameraInfo_t}(ncameras[])
err = ccall((:VmbCamerasList,vmblib), VmbError_t,
            (Ref{VmbCameraInfo_t}, VmbUint32_t, Ref{VmbUint32_t}, VmbUint32_t),
            cameras, ncameras[], ncameras, sizeof(Ptr{VmbCameraInfo_t}))

```

does that make sense? I’m still confused about the last argument.

Also note that in the first call I declared the first argument to be `Ptr{VmbCameraInfo_t}` becasue I wanted to pass `C_NULL`, whereas in the second I declared it to be `Ref{VmbHandle_t}` so I can directly pass `Array{VmbCameraInfo_t}(ncameras[])`. Do I need to worry about the `C_NULL` or does `Ref` do the right thing here?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 15, 2016, 11:03pm UTC](https://discourse.julialang.org/t/ccall-null-pointers-to-arrays-and-sizeof/984/2 "2016-12-15T23:03:02Z")

</div>

> [@jtravs](#):
>
> As far as I know, in C this just returns the size of the pointer type, not the size of VmbCameraInfo\_t let alone the array it points to.

It should return the size of the object pointed to by `pCameras` so assuming `pCameras` is of type `VmbCameraInfo_t*` it should be `sizeof(VmbCameraInfo_t)`. Without reading the doc a directly translation should be passing `sizeof(VmbCameraInfo_t)`

> [@jtravs](#):
>
> does that make sense? I’m still confused about the last argument.

Looks right.

> [@jtravs](#):
>
> Do I need to worry about the C\_NULL or does Ref do the right thing here?

What you are doing should be fine.

---

<div class="post-metadata">

**Author:** ![jtravs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtravs/32/2010_2.png) [@jtravs](https://discourse.julialang.org/u/jtravs)\
**Post date:** [December 16, 2016, 12:48am UTC](https://discourse.julialang.org/t/ccall-null-pointers-to-arrays-and-sizeof/984/3 "2016-12-16T00:48:40Z")

</div>

Thanks!

I have another question (on the same API). I need to pass the C struct:

```nohighlight
typedef struct
{
    const char* cameraIdString; # Unique identifier for each camera
    const char* cameraName; # Name of the camera
    const char* modelName; # Model name
    const char* serialString; # Serial number
    VmbAccessMode_t permittedAccess; # Used access mode, see VmbAccessModeType
    const char* interfaceIdString; # Unique value for each interface or bus
} VmbCameraInfo_t;

```

So I made a Julia type

```julia
immutable CameraInfo
    cameraIdString::Ptr{Uint8} # Unique identifier for each camera
    cameraName::Ptr{Uint8} # Name of the camera
    modelName::Ptr{Uint8} # Model name
    serialString::Ptr{Uint8} # Serial number
    permittedAccessVmbAccessMode_t # Used access mode, see VmbAccessModeType
    interfaceIdString::Ptr{Uint8} # Unique value for each interface or bus
end

```

Now I can load these with the function on my previous post. But as far as I understand I need to `unsafe_load` all of these `Ptr{Uint8}` fields after the function call right? As although the structure itself is allocated in Julia (as a member of an Array) the `const char*` fields are allocated in the C function.

Is there a simple and clean way to load all of these fields safely? Or do I need to make another type with String fields and `unsafe_load` them across?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 16, 2016, 4:53am UTC](https://discourse.julialang.org/t/ccall-null-pointers-to-arrays-and-sizeof/984/4 "2016-12-16T04:53:13Z")

</div>

> [@jtravs](#):
>
> But as far as I understand I need to unsafe\_load all of these Ptr{Uint8} fields after the function call right?

`unsafe_load` is for loading a single element. We do have functions to convert a pointer into a string/array. `unsafe_string` should copy the NUL-terminated C string into a julia string. `unsafe_wrap` will create a string or array that shares the pointer. You should pick one of these depending on the memory management of the C library. The doc for these functions should help you pick.

> [@jtravs](#):
>
> Is there a simple and clean way to load all of these fields safely?

It cannot be done “safely” as the operation of accessing bare memory is unsafe, i.e. we cannot guarantee the safeness.

> [@jtravs](#):
>
> Or do I need to make another type with String fields and unsafe\_load them across?

Also depend on the usecase, If you want to access them repeatedly as julia String object then yes you need to convert it into a different object. You can also lazy convert them with access functions if that’s more efficient for your usecase. [Here](https://github.com/yuyichao/LibALPM.jl/blob/master/src/ctypes.jl)’re some examples using the first approach from my package.
