help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Casting from an Octave int value to a pointer.


From: mpender
Subject: Casting from an Octave int value to a pointer.
Date: Tue, 21 Jan 2014 18:18:51 -0800 (PST)

I'm still trying to work out how to reserve memory with a handle.  I've
figured out how to reserve memory with an OCT file and pass back a pointer,
but I'm stuck trying to figure out the syntax for releasing the memory later
with the free() function.  I understand the general problem is one that the
compiler does not accept the implicit cast of an Octave integer to an
unsigned long, but I think I've tried every variation I can think of and
can't seem to find the correct syntax to set up the variable for the free()
function.

The line "tmp2 = tmp1" in freetest.cc produces these errors in
makeoctfile.cc:

mkoctfile freetest.cc
freetest.cc: In function ‘octave_value_list Ffreetest(const
octave_value_list&, int)’:
freetest.cc:19:24: error: invalid cast from type ‘const octave_uint32 {aka
const octave_int<unsigned int>}’ to type ‘void*’

Here's my freetest.cc file:

#include <octave/oct.h>
#include <stdio.h>
#include <stdlib.h>

DEFUN_DLD (freetest, args, nargout,  "Free memory for a previously reserved
array.")
{
    octave_value retval;
    int nargin = args.length();

    if (nargin != 1)
        print_usage ();
    else
    {
        const octave_uint32 (tmp1) = args(0).int_value();
        unsigned long tmp2;

        octave_stdout << "tmp1\t" << tmp1 << "\n";

        tmp2 = tmp1;

        octave_stdout << "tmp2\t" << tmp2 << "\n";

//        free(tmp2);
    }
    return octave_value_list();
}

malloctest.cc:

#include <octave/oct.h>
#include <stdio.h>
#include <stdlib.h>

DEFUN_DLD (malloctest, args, nargout,  "Reserve Memory for an array of size
POPULATIONSIZE x GENELENGTH.")
{
    octave_value retval;
    int nargin = args.length();

    if (nargin != 2)
        print_usage ();
    else
    {
        const int popsize = args(0).int_value();
        const int genelen = args(1).int_value();

        int indx, jndx;
        float *population;
        unsigned long tmp1;

        population = (float *) malloc (popsize * genelen * sizeof(float));

        if (population==NULL) return (octave_value_list());

        for (indx = 0; indx < popsize; indx++)
            for (jndx = 0; jndx < genelen; jndx++)
                population[indx*genelen + jndx] = indx * genelen + jndx;

    for (indx = 0; indx < popsize * genelen; indx++)
        printf("%03.0f\t", population [indx]);
    printf("\n");

// print the number representing the memory pointer
        printf("%ld\n", (unsigned long)population);
        tmp1 = (unsigned long)population;

//        set_global_value ("POPULATION", (octave_uint32) tmp1);

//        free(population);

        return octave_value((octave_uint32) tmp1);     
    }
    return octave_value_list();
}

test19.m:

clear
global POPSIZE
POPSIZE = 10;

global GENELEN
GENELEN = 20;

global POPULATION
global NEXTGEN
POPULATION = malloctest(POPSIZE, GENELEN)





--
View this message in context: 
http://octave.1599824.n4.nabble.com/Casting-from-an-Octave-int-value-to-a-pointer-tp4661233.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]