Re: global constant variables in mex



"Jon Erickson" <erickson@xxxxxxxxxxx> writes:

Hi Peter-
Thanks very much for the fast response!
All of it makes sense except the following.
You suggested trying

static int N;

I did this (both before my original post and retried after reading
your post).

Specifically I did

static int N;

N = (int)mxGetScalar(prhs[6]);

I still get the same kind of error:
Error newsalpamex.cc: 228 integer expression must be constant

I understand why this is happening , but
The thing I'm surprised about is that the compiler doesn't recognize
that I never try to update the value for N after the first call to
mxGetScalar. So it is in fact a constant once the first call to
mxGetScalar is finished. Maybe I'm expecting too much out of c
compilers...

It's not the fact that it is assigned to only once, but that fact that
the compiler doesn't know the size at compile time.

This restriction is not present in later C standards, C99, for
instance, and many compilers will probably handle it just fine,
treating it as some C/C99/C++ hybrid.


So my (probably very naive/stupid) question is:
can you please give a specific example how to pass a variable to a
mex function (from another matlab function, say) which is
subsequently declared as a global constant.

In ANSI C, global constants can only be assigned at declaration time,
and only with a value that the compiler can figure out at compile
time. So think of a different way :)


I'll probably just end up using your suggestion to do
#define N_MAX

but I'm curious to see how a pro would do it.

The #define N_MAX is how I would do it, unless my N_MAX were really
large, in which case I would use the mxMalloc suggestion.

-Peter
.



Relevant Pages

  • Re: Newbie question - displaying trivia questions at random
    ... and use some C++ compiler, ... static int num_items =0; ... Will not compile because syntax error. ... Visit http://www.ecomstation.de the home of german eComStation ...
    (comp.lang.c)
  • Re: Problem with a small "exercise" code using compiler xlf
    ... I couldn't tell from your original post that you understood that. ... it can vary from compiler ... and thus that roundoff error is indeed the correct explanation. ...
    (comp.lang.fortran)
  • Re: Pointers in C
    ... Two different platforms gives two ... "We know from the original post that the result is ... some other gcc system will produce. ... implementation, compiler, and platform. ...
    (comp.lang.c)
  • [PATCH][kprobe]replace magic numbers with enum (Re: Warning in kprobes.c)
    ... powerpc compiler is more unforgiving than most other arch compilers. ... Here is a patch which uses an enum to express the slot status. ... static int collect_garbage_slots; ...
    (Linux-Kernel)
  • Re: Pointers in C
    ... Two different platforms gives two ... "We know from the original post that the result is ... some other gcc system will produce. ... implementation, compiler, and platform. ...
    (comp.lang.c)