[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How can I trust in Objective-C?
From: |
Gary Polhill |
Subject: |
Re: How can I trust in Objective-C? |
Date: |
Wed, 10 Mar 1999 11:44:24 +0000 |
Fabio Mascelloni wrote:
>
> In the interface of one of my program's classes , I define an array of
> float values called local_y.After that I initialize it and print its
> content with a function called
> "printfRow" implemented in this way:
>
> void printfRow(float *vect,unsigned dim)
> {
> unsigned idx;
> for (idx=0;idx<dim;idx++) printf ("%f ",*(vect+idx));
> printf ("\n");
> }
>
> Here's the output for an instance of dim 2 when I try to use its
> elements in a method defined int the same class
>
> printfRow(local_y,2); // the output is 4.061603 3.941553
>
> // now I try to write these values whithout using printfRow
>
> printf ("First value : %f \n",*(local_y)); //the output is
> 452.150452
> printf ("Second value : %f \n",*(local_y+1)); //the output is
> 0.000000
>
I thought this might be an array/pointer thing. Arrays are not the same
as pointers, but I must confess I am not 100% clear about the difference.
I have tried to reproduce your problem declaring local_y to be an array
and a pointer, but can't. Here's the output of my program
% pointer -b
Array: 4.0616 3.94155
Pointer: 4.0616 3.94155
Array Pointer
===== =======
4.0616 4.0616
3.94155 3.94155
Here's the code I used:
/* main.m */
#import <simtools.h>
#import "pointer.h"
int main (int argc, const char **argv) {
Pointer *p;
initSwarm(argc, argv);
p = [Pointer create: globalZone];
[p mallocPtr];
[p setArrElement: 0 toValue: 4.061603];
[p setArrElement: 1 toValue: 3.941553];
[p setPtrElement: 0 toValue: 4.061603];
[p setPtrElement: 1 toValue: 3.941553];
[p print];
exit(0);
}
/* pointer.h */
#include <objectbase/SwarmObject.h>
#include <stdio.h>
#include <errno.h>
#define ARRSZ 2
@interface Pointer: SwarmObject {
float arr[ARRSZ];
unsigned arrSz;
float *ptr;
unsigned ptrSz;
}
-(void)setArrElement: (unsigned)i toValue: (float)v ;
-(void)setPtrElement: (unsigned)i toValue: (float)v ;
-(void)setNumPtrElements: (unsigned)n ;
+(id)create: (id)aZone ;
-(void)mallocPtr ;
-(void)print ;
@end
/* pointer.m */
#include "pointer.h"
void printfRow(float *v, unsigned d) {
unsigned i;
for(i = 0; i < d; i++) {
printf("%g ", *(v + i));
}
printf("\n");
}
@implementation Pointer
-(void)setArrElement: (unsigned)i toValue: (float)v {
if(i >= 0 && i < arrSz) {
arr[i] = v;
}
else {
fprintf(stderr, "Array element out of bounds (0-%u): %u\n", arrSz, i);
}
}
-(void)setPtrElement: (unsigned)i toValue: (float)v {
if(i >= 0 && i < ptrSz) {
*(ptr + i) = v;
}
else {
fprintf(stderr, "Pointer element out of bounds (0-%u): %u\n", ptrSz, i);
}
}
-(void)setNumPtrElements: (unsigned)n {
ptrSz = n;
}
+(id)create: (id)aZone {
Pointer *obj;
obj = [super create: aZone];
obj->ptrSz = 2;
obj->arrSz = ARRSZ;
return obj;
}
-(void)mallocPtr {
ptr = (float *)malloc(ptrSz * sizeof(float));
if(ptr == NULL) {
perror("Memory allocation of ptr");
exit(1);
}
}
-(void)print {
int i;
printf("Array: ");
printfRow(arr, arrSz);
printf("Pointer: ");
printfRow(ptr, ptrSz);
printf("Array\tPointer\n");
printf("=====\t=======\n");
for(i = 0; (i < arrSz) && (i < ptrSz); i++) {
printf("%g\t%g\n", *(arr + i), *(ptr + i));
}
}
@end
--
Macaulay Land Use Research Institute, Craigiebuckler, Aberdeen. AB15 8QH
Tel: +44 (0) 1224 318611 Email: address@hidden
==================================
Swarm-Support is for discussion of the technical details of the day
to day usage of Swarm. For list administration needs (esp.
[un]subscribing), please send a message to <address@hidden>
with "help" in the body of the message.