Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #6976 > unrolled thread

what about my array type?

Started by"io_x" <a@b.c.invalid>
First post2011-06-26 19:08 +0200
Last post2011-06-27 06:04 -0700
Articles 12 — 4 participants

Back to article view | Back to comp.lang.c


Contents

  what about my array type? "io_x" <a@b.c.invalid> - 2011-06-26 19:08 +0200
    Re: what about my array type? Barry Schwarz <schwarzb@dqel.com> - 2011-06-26 18:38 -0700
      Re: what about my array type? Keith Thompson <kst-u@mib.org> - 2011-06-26 19:30 -0700
      Re: what about my array type? "io_x" <a@b.c.invalid> - 2011-06-27 07:11 +0200
        Re: what about my array type? pete <pfiland@mindspring.com> - 2011-06-27 01:32 -0400
        Re: what about my array type? Keith Thompson <kst-u@mib.org> - 2011-06-26 22:36 -0700
          Re: what about my array type? "io_x" <a@b.c.invalid> - 2011-06-27 10:04 +0200
            Re: what about my array type? Keith Thompson <kst-u@mib.org> - 2011-06-27 08:27 -0700
        Re: what about my array type? Barry Schwarz <schwarzb@dqel.com> - 2011-06-27 06:04 -0700
      Re: what about my array type? "io_x" <a@b.c.invalid> - 2011-06-27 11:11 +0200
    Re: what about my array type? "io_x" <a@b.c.invalid> - 2011-06-27 09:36 +0200
      Re: what about my array type? Barry Schwarz <schwarzb@dqel.com> - 2011-06-27 06:04 -0700

#6976 — what about my array type?

From"io_x" <a@b.c.invalid>
Date2011-06-26 19:08 +0200
Subjectwhat about my array type?
Message-ID<4e076731$0$44197$4fafbaef@reader1.news.tin.it>
As always when post here i not do much debugging
some function did run 0 times
so should be some error

in the exercise of stack... it is not a stack
but one array that has the function push and pop

What about this array type?
Does someone see some error?
Does someone see some portability iussue?
There is some criticize to show?

if one have "unsigned  a=-1;"
is true that a==UINT_MAX?
is true that ++a == 0?

when someone has "a=3"
than is possible to have &a, but why not &3?
where &3 means the place in the memory [address]
where there is stored the value 3

standard allow CHAR_BIT != 8?
standard allow there is no unsigned X type with UXINT_MAX=0xFFFFFFFF?

thanks

#include <limits.h>
// CHAR_BIT
#include <stdlib.h>
#include <stdio.h>

// macro for types
#if  CHAR_BIT != 8
#error  "Non posso compilare con questo sistema"
#endif

#define  u8  unsigned char

#if   ULONG_MAX == 0xFFFFFFFF
#define  u32 unsigned long
#elif UINT_MAX  == 0xFFFFFFFF
#define  u32 unsigned int
#elif USHRT_MAX == 0xFFFFFFFF
#define  u32 unsigned short
#else
#error  "Non posso compilare con questo sistema"
#endif

#define  uns  unsigned

// macro for function
#define  P  printf

// macro for keyWords
#define G  goto
#define R  return
#define W  while
#define F  for
#define T  template
#define TN typename
#define S  sizeof

typedef struct ArrObj_{
u32       top;
u32        sz;
u32     ObjSz;
u8*       arr;
}ArrObj;

u32  IniArrObj(ArrObj**  v, u32  osz)
{u32        t;
 ArrObj   *vv;
 u8      *arr;

 if(v==0||osz==0||osz>0xFFFFFF)
                      R  -1;
 vv = malloc(S(ArrObj));
 if(vv==0)            R  -1;
 t=osz; t*=10;  // spazio per 10 obj
 if(t > 0xFFFFFF)
     {err: free(vv); *v=0; R  -1;}
 arr=malloc(t); if(t==0) G err;
 *v=vv; vv->top=-1; vv->sz=10;
 vv->ObjSz=osz;   vv->arr=arr;
 R  0;
}

void freeArrObj(ArrObj**  v)
{u32        i;
 ArrObj   *vv;

 if(v==0)  R;
 vv=*v;
 if(vv==0) R;
 free(vv->arr); free(vv);
 *v=0;
}

u32  ApplyArrObj(ArrObj**  v, u32 (*f)(u8*))
{u32  i, r, sz;
 u8        *va;
 ArrObj    *vv;

 if(v==0||*v==0) R  -1;
 vv=*v;
 if(vv==0) {P("Array NULL\n"); R  0;}
 if(vv->top==(u32)-1)
           {P("Array void\n"); R  0;}
 va=vv->arr; sz=vv->ObjSz;
 if(vv->top>0xFFFFFF||va==0||sz==0||sz>0xFFFFF)
           {P("Array error\n"); R  0;}
 F(i=vv->top, r=0; i!=0; --i)
        r|=f(va+i*sz);
 r|=f(va);
 R  r;
}

ArrObj**   push(ArrObj** v, u8*  elm)
{u8           *va, *r;
 ArrObj           *vv;
 u32      i, j, elmsz;

 if(v==0||*v==0||elm==0)
                 R  0;
 vv=*v; elmsz=vv->ObjSz;
 if(vv->sz>0xFFFFFF||elmsz>0xFFFFF)
                 R  0;
 if(vv->sz==0||vv->top==(vv->sz-1))
   {if(vv->sz==0) vv->arr=0;
    i=2*vv->sz+4;
    j=i*elmsz;
    if(i>=j||j>0xFFFFFF)  R  0;
    va=realloc(vv->arr, j);
    if(va==0)             R  0;
    vv->arr=va;
    vv->sz = i;
   }
 ++vv->top;
 r=(vv->arr)+(vv->top)*elmsz;
 F(i=0; i<elmsz; ++i)
     r[i]=elm[i];
 R  v;
}

u32   Write(ArrObj** v, u32  index, u8*  elm)
{u8           *va, *r;
 ArrObj           *vv;
 u32      i, j, elmsz;

 if(v==0||*v==0||elm==0||index>0xFFFFFF)
                 R  -1;
 vv=*v; elmsz=vv->ObjSz;
 if(vv->sz>0xFFFFFF||elmsz>0xFFFFF)
                 R  -1;
 if(index> vv->top+1)     R  -1;

 if(vv->sz==0||index==(vv->sz-1))
   {if(vv->sz==0) vv->arr=0;
    i=2*vv->sz+4;
    j=i*elmsz;
    if(i>=j||j>0xFFFFFF)  R  -1;
    va=realloc(vv->arr, j);
    if(va==0)             R  -1;
    vv->arr=va;
    vv->sz = i;
   }

 if(index==vv->top+1) ++vv->top;

 r=(vv->arr)+index*elmsz;
 F(i=0; i<elmsz; ++i)
     r[i]=elm[i];
 R  0;
}


/*
Se elm!=0 fa il pop dell'elemento
in ogni caso ritorna un puntatore all'elemento popped
 0 significa errore
*/
ArrObj**  pop(u8*  elm, ArrObj** v)
{u8    *va, *r;
 ArrObj    *vv;
 u32  i, elmsz;

 if(v==0||*v==0) R  0;
 vv=*v; elmsz=vv->ObjSz;
 if(vv->sz>0xFFFFFF||vv->top==(u32)-1||
    elmsz==0||elmsz>0xFFFFF)
                 R  0;
 if(elm!=0)
   {r=(vv->arr)+(vv->top)*elmsz;
    F(i=0; i<elmsz; ++i)
        {elm[i]=r[i]; r[i]=0;}
   }
 --vv->top;
 R  v;
}

// 0 means error
ArrObj**  top(u8*  elm, ArrObj** v)
{u8         *r;
 ArrObj    *vv;
 u32  i, elmsz;

 if(v==0||*v==0||elm==0)
                 R  0;
 vv=*v; elmsz=vv->ObjSz;
 if( vv->sz > 0xFFFFFF
   ||vv->top==(u32)-1
   ||elmsz==0 || elmsz>0xFFFFF )
                 R  0;
 r=(vv->arr)+(vv->top)*elmsz;
 F(i=0; i<elmsz; ++i)
        elm[i]=r[i];
 R  v;
}

u32  get(u8*  ele, ArrObj** v, u32  index)
{u8         *r;
 ArrObj    *vv;
 u32  i, elmsz;

 if(ele==0||index>0xFFFFFF||v==0||*v==0)
                 R  -1;
 vv=*v; elmsz=vv->ObjSz;
 if(vv->sz>0xFFFFFF ||vv->top==(u32)-1||index>vv->top||
      elmsz==0||elmsz>0xFFFFF )
                 R  -1;
 r=(vv->arr)+(vv->top)*elmsz;
 F(i=0; i<elmsz; ++i)
        ele[i]=r[i];
 R  0;
}

u32  insert(ArrObj** v, u32  index, u8*  ele)
{u8  *r, *k, *h;
 ArrObj     *vv;
 u32  i,j,elmsz;

 if(ele==0||index>0xFFFFFF||v==0||*v==0)
                 R  -1;
 vv=*v; elmsz=vv->ObjSz;
 if(vv->sz>0xFFFFFF||vv->top==(u32)-1||index>(vv->top)+1||
      elmsz==0||elmsz>0xFFFFF )
                 R  -1;
 if(vv->sz==0||index>=(vv->sz-1))
   {if(vv->sz==0) vv->arr=0;
    i=2*vv->sz+4;
    j=i*elmsz;
    if(i>=j||j>0xFFFFFF)  R  -1;
    r=realloc(vv->arr, j);
    if(r ==0)             R  -1;
    vv->arr=r;
    vv->sz =i;
   }
// spostare gli elementi sopra l'indice di 1
 h=(vv->arr)+      index*elmsz;
 r=(vv->arr)+(vv->top+1)*elmsz;
 k=(vv->arr)+(vv->top  )*elmsz;
 F(; k>=h;--r,--k)
                *r=*k;
 F(i=0, ++k; i<elmsz; ++i)
        k[i]=ele[i];
 ++vv->top;
 R  0;
}

u32  printu(u8*  a)
{u32   v, r;
 v=*(u32*)a;
 r=P("%u ", (uns) v);
 if((int)r<=0) R  -1;
 R  0;
}

u32  printd(u8*  a)
{double   v;
 u32      r;

 v=*(double*)a;
 r=P("%f ", v);
 if((int)r<=0) R  -1;
 R  0;
}

int test(void)
{double        a;
 ArrObj *Adouble;
 ArrObj    *Au32;
 u32        b, r;

 r=IniArrObj(&Adouble, S(double));
 if(r==-1)  R  -1;
 IniArrObj(&Au32,   S(u32));
 if(r==-1){err:
            P("Errore\n");
            freeArrObj(&Adouble);
            R  -1;
          }

 if(push(&Adouble, (a=1.1, &a))==0)
    {err1: freeArrObj(&Au32); G err;}
 if(push(&Adouble, (a=1.2, &a))==0) G  err1;
 if(push(&Adouble, (a=1.3, &a))==0) G  err1;
 if(push(&Au32, (b=1, &b))==0) G  err1;
 if(push(&Au32, (b=2, &b))==0) G  err1;
 if(push(&Au32, (b=3, &b))==0) G  err1;

 P("Array di u    32: ");
 ApplyArrObj(&Au32, printu);

 P("\nArray di double: ");
 ApplyArrObj(&Adouble, printd);

 freeArrObj(&Au32);
 freeArrObj(&Adouble);
 R  0;
}

int main(void)
{test();
 R  0;
}

/*
Array di u    32: 3 2 1
Array di double: 1.300000 1.200000 1.100000
*/




[toc] | [next] | [standalone]


#7006

FromBarry Schwarz <schwarzb@dqel.com>
Date2011-06-26 18:38 -0700
Message-ID<t7mf07d716olfv14kqcem0dalkhksue6po@4ax.com>
In reply to#6976
On Sun, 26 Jun 2011 19:08:36 +0200, "io_x" <a@b.c.invalid> wrote:

>
>As always when post here i not do much debugging
>some function did run 0 times
>so should be some error

If you are not willing to put in the time and effort, why should
anyone else do so?

>
>in the exercise of stack... it is not a stack
>but one array that has the function push and pop
>
>What about this array type?
>Does someone see some error?
>Does someone see some portability iussue?
>There is some criticize to show?
>
>if one have "unsigned  a=-1;"
>is true that a==UINT_MAX?
>is true that ++a == 0?

For some reason you cannot be bothered to look at the standard (or the
easily downloadable free draft) yourself?  Or check any of the
hundreds of messages where it has been discussed before?

>
>when someone has "a=3"
>than is possible to have &a, but why not &3?
>where &3 means the place in the memory [address]
>where there is stored the value 3

What makes you think that the value 3 must be stored somewhere in
memory?

>
>standard allow CHAR_BIT != 8?

Also discussed many times.

>standard allow there is no unsigned X type with UXINT_MAX=0xFFFFFFFF?

What do you think is the most common value for UINT_MAX on a 32 bit
machine.

>
>thanks
>
>#include <limits.h>
>// CHAR_BIT
>#include <stdlib.h>
>#include <stdio.h>
>
>// macro for types
>#if  CHAR_BIT != 8
>#error  "Non posso compilare con questo sistema"
>#endif
>
>#define  u8  unsigned char

Why #define instead of typedef?

>
>#if   ULONG_MAX == 0xFFFFFFFF

Since the preprocessor does not perform arithmetic conversions, if
ULONG_MAX is defined as 4294967295UL, this #if will fail when it
should succeed.

>#define  u32 unsigned long
>#elif UINT_MAX  == 0xFFFFFFFF
>#define  u32 unsigned int
>#elif USHRT_MAX == 0xFFFFFFFF
>#define  u32 unsigned short
>#else
>#error  "Non posso compilare con questo sistema"
>#endif
>
>#define  uns  unsigned
>
>// macro for function
>#define  P  printf

Sorry but as soon as I see this kind of code, I stop reading

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#7012

FromKeith Thompson <kst-u@mib.org>
Date2011-06-26 19:30 -0700
Message-ID<lnsjqw9fag.fsf@nuthaus.mib.org>
In reply to#7006
Barry Schwarz <schwarzb@dqel.com> writes:
> On Sun, 26 Jun 2011 19:08:36 +0200, "io_x" <a@b.c.invalid> wrote:
[...]
>>standard allow there is no unsigned X type with UXINT_MAX=0xFFFFFFFF?
>
> What do you think is the most common value for UINT_MAX on a 32 bit
> machine.

Sure, but the standard does permit implementations for which no unsigned
type has a maximum value of 0xFFFFFFFF (though at least unsigned long
and unsigned long long must have maximum values at least that large).

[...]
>>#define  uns  unsigned
>>
>>// macro for function
>>#define  P  printf
>
> Sorry but as soon as I see this kind of code, I stop reading

As do I.  io_x, if you want anyone to read your code, stop using those
silly macros.  If you don't want anyone to read your code, you might as
well not bother posting it here.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#7019

From"io_x" <a@b.c.invalid>
Date2011-06-27 07:11 +0200
Message-ID<4e0810a9$0$15669$4fafbaef@reader2.news.tin.it>
In reply to#7006
"Barry Schwarz" <schwarzb@dqel.com> ha scritto nel messaggio 
news:t7mf07d716olfv14kqcem0dalkhksue6po@4ax.com...
> On Sun, 26 Jun 2011 19:08:36 +0200, "io_x" <a@b.c.invalid> wrote:
>
>>
>>As always when post here i not do much debugging
>>some function did run 0 times
>>so should be some error
>
> If you are not willing to put in the time and effort, why should
> anyone else do so?

because someone has nothing to do?

>>in the exercise of stack... it is not a stack
>>but one array that has the function push and pop
>>
>>What about this array type?
>>Does someone see some error?
>>Does someone see some portability iussue?
>>There is some criticize to show?
>>
>>if one have "unsigned  a=-1;"
>>is true that a==UINT_MAX?
>>is true that ++a == 0?
>
> For some reason you cannot be bothered to look at the standard (or the
> easily downloadable free draft) yourself?  Or check any of the
> hundreds of messages where it has been discussed before?

i'm lazy

>>when someone has "a=3"
>>than is possible to have &a, but why not &3?
>>where &3 means the place in the memory [address]
>>where there is stored the value 3
>
> What makes you think that the value 3 must be stored somewhere in
> memory?

because here is in the memory, it could be in section data
can be in section text, for example the instruction
mov eax, 3
has in the code section, in the space reserved for store the memory
for this instruction, has the number 3
there is one addres in the executable that contai 3

i have to say it is not true for instruction for FPU
that has the instruction "store 1 in the FPU stack"
but were there is no float 1.0 embedded["immerso"] in the
instruction code...

>>standard allow CHAR_BIT != 8?
>
> Also discussed many times.

yes this i know: No CHAR_BIT can be != 8

>>standard allow there is no unsigned X type with UXINT_MAX=0xFFFFFFFF?

yes this too i know too, yes there will be one unsigned X type where
UXINT_MAX=0xFFFFFFFF

> What do you think is the most common value for UINT_MAX on a 32 bit
> machine.

>>thanks
>>
>>#include <limits.h>
>>// CHAR_BIT
>>#include <stdlib.h>
>>#include <stdio.h>
>>
>>// macro for types
>>#if  CHAR_BIT != 8
>>#error  "Non posso compilare con questo sistema"
>>#endif
>>
>>#define  u8  unsigned char
>
> Why #define instead of typedef?
>
>>
>>#if   ULONG_MAX == 0xFFFFFFFF
>
> Since the preprocessor does not perform arithmetic conversions, if
> ULONG_MAX is defined as 4294967295UL, this #if will fail when it
> should succeed.
>
>>#define  u32 unsigned long
>>#elif UINT_MAX  == 0xFFFFFFFF
>>#define  u32 unsigned int
>>#elif USHRT_MAX == 0xFFFFFFFF
>>#define  u32 unsigned short
>>#else
>>#error  "Non posso compilare con questo sistema"
>>#endif
>>
>>#define  uns  unsigned
>>
>>// macro for function
>>#define  P  printf
>
> Sorry but as soon as I see this kind of code, I stop reading
 

[toc] | [prev] | [next] | [standalone]


#7022

Frompete <pfiland@mindspring.com>
Date2011-06-27 01:32 -0400
Message-ID<4E081602.7842@mindspring.com>
In reply to#7019
io_x wrote:
> 
> "Barry Schwarz" <schwarzb@dqel.com> ha scritto nel messaggio
> news:t7mf07d716olfv14kqcem0dalkhksue6po@4ax.com...
> > On Sun, 26 Jun 2011 19:08:36 +0200, "io_x" <a@b.c.invalid> wrote:

> >>when someone has "a=3"
> >>than is possible to have &a, but why not &3?
> >>where &3 means the place in the memory [address]
> >>where there is stored the value 3
> >
> > What makes you think that the value 3 must be stored somewhere in
> > memory?
> 
> because here is in the memory, it could be in section data
> can be in section text, for example the instruction
> mov eax, 3
> has in the code section, in the space reserved for store the memory
> for this instruction, has the number 3
> there is one addres in the executable that contai 3

That just happens to be that way on the C implementation
that you are using right now.

There's no rule in C which specifies how
   a = 3;
must be translated.

   a = 3;
only means that after the statement is executed,
that (a) will have a value of 3.

If the statement were
   a = 0;
instead, then it might be translated as a (clear) 
instruction, rather than as a (mov) instuction.


-- 
pete

[toc] | [prev] | [next] | [standalone]


#7023

FromKeith Thompson <kst-u@mib.org>
Date2011-06-26 22:36 -0700
Message-ID<lnoc1jal9u.fsf@nuthaus.mib.org>
In reply to#7019
"io_x" <a@b.c.invalid> writes:
[...]
>> On Sun, 26 Jun 2011 19:08:36 +0200, "io_x" <a@b.c.invalid> wrote:
[...]
>>>standard allow there is no unsigned X type with UXINT_MAX=0xFFFFFFFF?
>
> yes this too i know too, yes there will be one unsigned X type where
> UXINT_MAX=0xFFFFFFFF
[...]

Incorrect; see my other followup.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#7032

From"io_x" <a@b.c.invalid>
Date2011-06-27 10:04 +0200
Message-ID<4e083942$0$44207$4fafbaef@reader1.news.tin.it>
In reply to#7023
"Keith Thompson" <kst-u@mib.org> ha scritto nel messaggio
news:lnoc1jal9u.fsf@nuthaus.mib.org...
> "io_x" <a@b.c.invalid> writes:
> [...]
>>> On Sun, 26 Jun 2011 19:08:36 +0200, "io_x" <a@b.c.invalid> wrote:
> [...]
>>>>standard allow there is no unsigned X type with UXINT_MAX=0xFFFFFFFF?
>>
>> yes this too i know too, yes there will be one unsigned X type where
>> UXINT_MAX=0xFFFFFFFF
> [...]
>
> Incorrect; see my other followup.

so is possible something as
unsigned long   64 bits
unsigned int    64 bits
unsigned short  64 bits
unsigned char    8 bits

because unsigned log as to be at last 32 bits
and sizeof(unsigned char)<=sizeof(unsigned short)<=
    <=sizeof(unsigned int)<=sizeof(unsigned long) all as 2^n



[toc] | [prev] | [next] | [standalone]


#7071

FromKeith Thompson <kst-u@mib.org>
Date2011-06-27 08:27 -0700
Message-ID<lnk4c79twc.fsf@nuthaus.mib.org>
In reply to#7032
"io_x" <a@b.c.invalid> writes:
> "Keith Thompson" <kst-u@mib.org> ha scritto nel messaggio
> news:lnoc1jal9u.fsf@nuthaus.mib.org...
>> "io_x" <a@b.c.invalid> writes:
>> [...]
>>>> On Sun, 26 Jun 2011 19:08:36 +0200, "io_x" <a@b.c.invalid> wrote:
>> [...]
>>>>>standard allow there is no unsigned X type with UXINT_MAX=0xFFFFFFFF?
>>>
>>> yes this too i know too, yes there will be one unsigned X type where
>>> UXINT_MAX=0xFFFFFFFF
>> [...]
>>
>> Incorrect; see my other followup.
>
> so is possible something as
> unsigned long   64 bits
> unsigned int    64 bits
> unsigned short  64 bits
> unsigned char    8 bits
>
> because unsigned log as to be at last 32 bits
> and sizeof(unsigned char)<=sizeof(unsigned short)<=
>     <=sizeof(unsigned int)<=sizeof(unsigned long) all as 2^n

Right.  I've worked on a system (Cray T90) with exactly those
characteristics.  (And it was mostly for interoperability reasons
that CHAR_BIT was made 8 rather than 64.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#7049

FromBarry Schwarz <schwarzb@dqel.com>
Date2011-06-27 06:04 -0700
Message-ID<6irg071psh1n83at2c2c13kue421t1c347@4ax.com>
In reply to#7019
On Mon, 27 Jun 2011 07:11:39 +0200, "io_x" <a@b.c.invalid> wrote:

>
>"Barry Schwarz" <schwarzb@dqel.com> ha scritto nel messaggio 
>news:t7mf07d716olfv14kqcem0dalkhksue6po@4ax.com...
>> On Sun, 26 Jun 2011 19:08:36 +0200, "io_x" <a@b.c.invalid> wrote:
>>
>>>
snip

>
>>>when someone has "a=3"
>>>than is possible to have &a, but why not &3?
>>>where &3 means the place in the memory [address]
>>>where there is stored the value 3
>>
>> What makes you think that the value 3 must be stored somewhere in
>> memory?
>
>because here is in the memory, it could be in section data
>can be in section text, for example the instruction
>mov eax, 3
>has in the code section, in the space reserved for store the memory
>for this instruction, has the number 3
>there is one addres in the executable that contai 3

The following is a perfectly acceptable code sequence for 
   a = 3;
      CLR
      INC
      INC
      INC
      ST

There is no 3 anywhere in memory.

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#7037

From"io_x" <a@b.c.invalid>
Date2011-06-27 11:11 +0200
Message-ID<4e0848cc$0$44197$4fafbaef@reader1.news.tin.it>
In reply to#7006
"Barry Schwarz" <schwarzb@dqel.com> ha scritto nel messaggio
news:t7mf07d716olfv14kqcem0dalkhksue6po@4ax.com...
> On Sun, 26 Jun 2011 19:08:36 +0200, "io_x" <a@b.c.invalid> wrote:
>>#define  u8  unsigned char
>
> Why #define instead of typedef?

because these macro are familar to me;
but it is not familiar typedef,
and not i want to full my little
mind with too much standars;
i search the minimize standard that
make goes all well
without rememeber too much

>>#if   ULONG_MAX == 0xFFFFFFFF
>
> Since the preprocessor does not perform arithmetic conversions, if
> ULONG_MAX is defined as 4294967295UL, this #if will fail when it
> should succeed.

in this system i found in the header file the line

#define ULONG_MAX 4294967295UL /* maximum unsigned long value */

but the prog run the same, this means that some #if return true
i think u32 here is unsigned long and the conversion is ok

do you say that for compare 2 numbers sys use strcmp? [for preprocessors
directives]
and not it gets the 2 number and make the compare

>>#define  u32 unsigned long
>>#elif UINT_MAX  == 0xFFFFFFFF
>>#define  u32 unsigned int
>>#elif USHRT_MAX == 0xFFFFFFFF
>>#define  u32 unsigned short
>>#else
>>#error  "Non posso compilare con questo sistema"
>>#endif
>>
>>#define  uns  unsigned
>>
>>// macro for function
>>#define  P  printf
>
> Sorry but as soon as I see this kind of code, I stop reading


[toc] | [prev] | [next] | [standalone]


#7028

From"io_x" <a@b.c.invalid>
Date2011-06-27 09:36 +0200
Message-ID<4e0832a3$0$44201$4fafbaef@reader1.news.tin.it>
In reply to#6976
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4e076731$0$44197$4fafbaef@reader1.news.tin.it...
> when someone has "a=3"
> than is possible to have &a, but why not &3?
> where &3 means the place in the memory [address]
> where there is stored the value 3

yes i can do something that return the copy of
the costant in an address
somthing as:
// return the address of one region of mem
// that contain the costant number
#define  A(a)  RitornaPuntatoreAmem(sizeof a, a)

but i have the dubit that:
 pc=(u8*)&(va_arg(ap, u8*));
is right...
but how to write such function, that get one costant
and return one pointer to space of mem that contain
that costant...
if the costant is a number double as 1.1, int -1,
or unsigned int  99
as in A(99) or A(1.1) or A(-1)

#include <stdarg.h>
#include <limits.h>
// CHAR_BIT
#include <stdlib.h>
#include <stdio.h>

// macro for types

#define  u8  unsigned char

#if   ULONG_MAX == 0xFFFFFFFF
#define  u32 unsigned long
#elif UINT_MAX  == 0xFFFFFFFF
#define  u32 unsigned int
#elif USHRT_MAX == 0xFFFFFFFF
#define  u32 unsigned short
#else
#error  "Non posso compilare con questo sistema"
#endif

#define  uns  unsigned

// macro for function
#define  P  printf

// macro for keyWords
#define G  goto
#define R  return
#define W  while
#define F  for
#define T  template
#define TN typename
#define S  sizeof

typedef struct ArrObj_{
u32       top;
u32        sz;
u32     ObjSz;
u8*       arr;
}ArrObj;

u32  IniArrObj(ArrObj**  v, u32  osz)
{u32        t;
 ArrObj   *vv;
 u8      *arr;

 if(v==0||osz==0||osz>0xFFFFFF)
                      R  -1;
 vv = malloc(S(ArrObj));
 if(vv==0)            R  -1;
 t=osz; t*=10;  // spazio per 10 obj
 if(t > 0xFFFFFF)
     {err: free(vv); *v=0; R  -1;}
 arr=malloc(t); if(t==0) G err;
 *v=vv; vv->top=-1; vv->sz=10;
 vv->ObjSz=osz;   vv->arr=arr;
 R  0;
}

void freeArrObj(ArrObj**  v)
{u32        i;
 ArrObj   *vv;

 if(v==0)  R;
 vv=*v;
 if(vv==0) R;
 free(vv->arr); free(vv);
 *v=0;
}

u32  ApplyArrObj(ArrObj**  v, u32 (*f)(u8*))
{u32  i, r, sz;
 u8        *va;
 ArrObj    *vv;

 if(v==0||*v==0) R  -1;
 vv=*v;
 if(vv==0) {P("Array NULL\n"); R  0;}
 if(vv->top==(u32)-1)
           {P("Array void\n"); R  0;}
 va=vv->arr; sz=vv->ObjSz;
 if(vv->top>0xFFFFFF||va==0||sz==0||sz>0xFFFFF)
           {P("Array error\n"); R  0;}
 F(i=vv->top, r=0; i!=0; --i)
        r|=f(va+i*sz);
 r|=f(va);
 R  r;
}

ArrObj**   push(ArrObj** v, u8*  elm)
{u8           *va, *r;
 ArrObj           *vv;
 u32      i, j, elmsz;

 if(v==0||*v==0||elm==0)
                 R  0;
 vv=*v; elmsz=vv->ObjSz;
 if(vv->sz>0xFFFFFF||elmsz>0xFFFFF)
                 R  0;
 if(vv->sz==0||vv->top==(vv->sz-1))
   {if(vv->sz==0) vv->arr=0;
    i=2*vv->sz+4;
    j=i*elmsz;
    if(i>=j||j>0xFFFFFF)  R  0;
    va=realloc(vv->arr, j);
    if(va==0)             R  0;
    vv->arr=va;
    vv->sz = i;
   }
 ++vv->top;
 r=(vv->arr)+(vv->top)*elmsz;
 F(i=0; i<elmsz; ++i)
     r[i]=elm[i];
 R  v;
}

u32   Write(ArrObj** v, u32  index, u8*  elm)
{u8           *va, *r;
 ArrObj           *vv;
 u32      i, j, elmsz;

 if(v==0||*v==0||elm==0||index>0xFFFFFF)
                 R  -1;
 vv=*v; elmsz=vv->ObjSz;
 if(vv->sz>0xFFFFFF||elmsz>0xFFFFF)
                 R  -1;
 if(index> vv->top+1)     R  -1;

 if(vv->sz==0||index==(vv->sz-1))
   {if(vv->sz==0) vv->arr=0;
    i=2*vv->sz+4;
    j=i*elmsz;
    if(i>=j||j>0xFFFFFF)  R  -1;
    va=realloc(vv->arr, j);
    if(va==0)             R  -1;
    vv->arr=va;
    vv->sz = i;
   }

 if(index==vv->top+1) ++vv->top;

 r=(vv->arr)+index*elmsz;
 F(i=0; i<elmsz; ++i)
     r[i]=elm[i];
 R  0;
}


/*
Se elm!=0 fa il pop dell'elemento
in ogni caso ritorna un puntatore all'elemento popped
 0 significa errore
*/
ArrObj**  pop(u8*  elm, ArrObj** v)
{u8    *va, *r;
 ArrObj    *vv;
 u32  i, elmsz;

 if(v==0||*v==0) R  0;
 vv=*v; elmsz=vv->ObjSz;
 if(vv->sz>0xFFFFFF||vv->top==(u32)-1||
    elmsz==0||elmsz>0xFFFFF)
                 R  0;
 if(elm!=0)
   {r=(vv->arr)+(vv->top)*elmsz;
    F(i=0; i<elmsz; ++i)
        {elm[i]=r[i]; r[i]=0;}
   }
 --vv->top;
 R  v;
}

// 0 means error
ArrObj**  top(u8*  elm, ArrObj** v)
{u8         *r;
 ArrObj    *vv;
 u32  i, elmsz;

 if(v==0||*v==0||elm==0)
                 R  0;
 vv=*v; elmsz=vv->ObjSz;
 if( vv->sz > 0xFFFFFF
   ||vv->top==(u32)-1
   ||elmsz==0 || elmsz>0xFFFFF )
                 R  0;
 r=(vv->arr)+(vv->top)*elmsz;
 F(i=0; i<elmsz; ++i)
        elm[i]=r[i];
 R  v;
}

u32  get(u8*  ele, ArrObj** v, u32  index)
{u8         *r;
 ArrObj    *vv;
 u32  i, elmsz;

 if(ele==0||index>0xFFFFFF||v==0||*v==0)
                 R  -1;
 vv=*v; elmsz=vv->ObjSz;
 if(vv->sz>0xFFFFFF ||vv->top==(u32)-1||index>vv->top||
      elmsz==0||elmsz>0xFFFFF )
                 R  -1;
 r=(vv->arr)+(vv->top)*elmsz;
 F(i=0; i<elmsz; ++i)
        ele[i]=r[i];
 R  0;
}

u32  insert(ArrObj** v, u32  index, u8*  ele)
{u8  *r, *k, *h;
 ArrObj     *vv;
 u32  i,j,elmsz;

 if(ele==0||index>0xFFFFFF||v==0||*v==0)
                 R  -1;
 vv=*v; elmsz=vv->ObjSz;
 if(vv->sz>0xFFFFFF||vv->top==(u32)-1||index>(vv->top)+1||
      elmsz==0||elmsz>0xFFFFF )
                 R  -1;
 if(vv->sz==0||index>=(vv->sz-1))
   {if(vv->sz==0) vv->arr=0;
    i=2*vv->sz+4;
    j=i*elmsz;
    if(i>=j||j>0xFFFFFF)  R  -1;
    r=realloc(vv->arr, j);
    if(r ==0)             R  -1;
    vv->arr=r;
    vv->sz =i;
   }
// spostare gli elementi sopra l'indice di 1
 h=(vv->arr)+      index*elmsz;
 r=(vv->arr)+(vv->top+1)*elmsz;
 k=(vv->arr)+(vv->top  )*elmsz;
 F(; k>=h;--r,--k)
                *r=*k;
 F(i=0, ++k; i<elmsz; ++i)
        k[i]=ele[i];
 ++vv->top;
 R  0;
}

u32  printu(u8*  a)
{u32   v, r;
 v=*(u32*)a;
 r=P("%u ", (uns) v);
 if((int)r<=0) R  -1;
 R  0;
}

u32  printd(u8*  a)
{double   v;
 u32      r;

 v=*(double*)a;
 r=P("%f ", v);
 if((int)r<=0) R  -1;
 R  0;
}

// return the address of one region of mem
// that contain the costant number
#define  A(a)  RitornaPuntatoreAmem(sizeof a, a)

u8*  RitornaPuntatoreAmem(u32  sz, ...)
{static u8  *val=0;
 va_list        ap;
 u8            *pc;
 u32             i;

 if(sz>512)             R  0;
 if(val==0){/* the memory for this vector of u8*
               is a leak and never be free until
               this program run
            */
            val=malloc(1000);
            if(val==0)  R  0;
           }

 va_start(ap, sz);
 pc=(u8*)&(va_arg(ap, u8*));
 F(i=0; i<sz; ++i)
       val[i]=pc[i];
 va_end(ap);

 R   val;
}

int test(void)
{double        a;
 ArrObj *Adouble;
 ArrObj    *Au32;
 u32        b, r;

 r=IniArrObj(&Adouble, S(double));
 if(r!=0)  R  -1;
 IniArrObj(&Au32,   S(u32));
 if(r!=0){err:
          P("Errore\n");
          freeArrObj(&Adouble);
          R  -1;
         }

 if(push(&Adouble, A(1.1))==0)
    {err1: freeArrObj(&Au32);  G  err;}
 if(push(&Adouble, A(1.2))==0) G  err1;
 if(push(&Adouble, A(1.3))==0) G  err1;
 if(push(&Au32,    A(1))==0)   G  err1;
 if(push(&Au32,    A(2))==0)   G  err1;
 if(push(&Au32,    A(3))==0)   G  err1;

 P("Array di u   32: ")  ;ApplyArrObj(&Au32,    printu);
 P("\nArray di double: ");ApplyArrObj(&Adouble, printd);

 freeArrObj(&Au32);
 freeArrObj(&Adouble);
 R  0;
}

int main(void)
{//P("sizeof 1.1=%u\n", (uns) sizeof 1.1);
 test();
 R  0;
}



[toc] | [prev] | [next] | [standalone]


#7050

FromBarry Schwarz <schwarzb@dqel.com>
Date2011-06-27 06:04 -0700
Message-ID<vrrg07h98p234n0oelijeqt0eljtbcqa2k@4ax.com>
In reply to#7028
On Mon, 27 Jun 2011 09:36:39 +0200, "io_x" <a@b.c.invalid> wrote:

>
>"io_x" <a@b.c.invalid> ha scritto nel messaggio
>news:4e076731$0$44197$4fafbaef@reader1.news.tin.it...
>> when someone has "a=3"
>> than is possible to have &a, but why not &3?

snip ~350 lines of code mostly repeated from previous messages

For a self proclaimed lazy person you do like to talk to yourself a
lot.

-- 
Remove del for email

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web