Local variable in plugin

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Local variable in plugin

Ang BeePeng
How to initialize a local variable in plugin?

Say I want to have something like this in c for my plugin.

int myfunction (){
   int* temp;

   for(int i=0; i< 4; i++){
   temp[i] = i;
   }
}


I use
self var: #temp type: 'int *temp'.  
for
int* temp

1 to: 3 do:[:i|
   temp at:i  put: i].

for the loop.

It seems like I need to initialize temp or something.

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Local variable in plugin

askoh
Administrator
It looks reasonable. Just ask vmmaker to compile the code and then look at the C output.
Aik-Siong Koh

Ang Beepeng wrote
How to initialize a local variable in plugin?

Say I want to have something like this in c for my plugin.

int myfunction (){
   int* temp;

   for(int i=0; i< 4; i++){
   temp[i] = i;
   }
}


I use
self var: #temp type: 'int *temp'.  
for
int* temp

1 to: 3 do:[:i|
   temp at:i  put: i].

for the loop.

It seems like I need to initialize temp or something.

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Local variable in plugin

Bert Freudenberg
In reply to this post by Ang BeePeng

On 02.11.2008, at 23:06, Ang Beepeng wrote:

>
> How to initialize a local variable in plugin?
>
> Say I want to have something like this in c for my plugin.
>
> int myfunction (){
>   int* temp;
>
>   for(int i=0; i< 4; i++){
>   temp[i] = i;
>   }
> }
>
> I use
> self var: #temp type: 'int *temp'.

Shouldn't this be

     self var: #temp type: 'int *'.

? Have a look at the generated C code.

- Bert -



>
> for
> int* temp
>
> 1 to: 3 do:[:i|
>   temp at:i  put: i].
> for the loop.
>
> It seems like I need to initialize temp or something.
>
> Thanks.
>


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Local variable in plugin

David T. Lewis
In reply to this post by Ang BeePeng
On Sun, Nov 02, 2008 at 07:34:18PM -0800, Ang Beepeng wrote:

>
> How to initialize a local variable in plugin?
>
> Say I want to have smething like this in c.
>
> int myfunction (){
>    int* temp;
>
>    for(int i=0; i< 4; i++){
>    temp[i] = i;
>    }
> }

Well, you would not want to do that in C, because your temp
variable is not pointing to any allocated memory. In this case
you would want to declare it is "int temp[4]".

To write it in Smalltalk, you could write this:

myFunction
    | temp i |
    self var: #temp declareC: 'int temp[4]'.
    i := 0.
    [i < 4] whileTrue:
            [temp at: i put: i.
            i := i + 1]

Which will be translated to C:

static sqInt myFunction(void) {
    sqInt i;
    int temp[4];
    i = 0;
    while (i < 4) {
        temp[i] = i;
        i += 1;
    }
}