memset o/p not correct

  • Follow


Hi,

the following is actually a part of the pattern matching program which
i tried ,memset is not setting the entire integer array with
-1

 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>

 int main(void)
 {

   int maxpat[80];
   memset(maxpat,-1,80);

   for(i=0;i<80;i++)
   printf("%d\t",maxpat[i]);

   return EXIT_SUCCESS;
 }

but i am getting o/p as

-1      -1      -1      -1      -1      -1      -1      -1
-1      -1
-1      -1      -1      -1      -1      -1      -1      -1
-1      -1
-370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
-370086 -370086
-370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
-370086 -370086
-370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
-370086 -370086
-370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
-370086 -370086
-370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
-370086 -370086
-370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
-370086 -370086

"D:\siju\lcc6\noisedisp.exe"
Return code 0
Execution time 0.048 seconds
Press any key to continue...

why is this so..????
0
Reply aarklon (253) 4/19/2008 9:28:14 PM

sorry I forgot to declare i , but still the o/p is same

 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>


 int main(void)
 {


   int maxpat[80],i;
   memset(maxpat,-1,80);


   for(i=0;i<80;i++)
   printf("%d\t",maxpat[i]);


   return EXIT_SUCCESS;
 }


0
Reply aarklon (253) 4/19/2008 9:31:23 PM


On Apr 20, 2:28=A0am, aark...@gmail.com wrote:
> Hi,
>
> the following is actually a part of the pattern matching program which
> i tried ,memset is not setting the entire integer array with
> -1
>
> =A0#include <string.h>
> =A0#include <stdio.h>
> =A0#include <stdlib.h>
>
> =A0int main(void)
> =A0{
>
> =A0 =A0int maxpat[80];
> =A0 =A0memset(maxpat,-1,80);
>
> =A0 =A0for(i=3D0;i<80;i++)
> =A0 =A0printf("%d\t",maxpat[i]);
>
> =A0 =A0return EXIT_SUCCESS;
> =A0}
>
> but i am getting o/p as
>
> -1 =A0 =A0 =A0-1 =A0 =A0 =A0-1 =A0 =A0 =A0-1 =A0 =A0 =A0-1 =A0 =A0 =A0-1 =
=A0 =A0 =A0-1 =A0 =A0 =A0-1
> -1 =A0 =A0 =A0-1
> -1 =A0 =A0 =A0-1 =A0 =A0 =A0-1 =A0 =A0 =A0-1 =A0 =A0 =A0-1 =A0 =A0 =A0-1 =
=A0 =A0 =A0-1 =A0 =A0 =A0-1
> -1 =A0 =A0 =A0-1
> -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
> -370086 -370086
> -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
> -370086 -370086
> -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
> -370086 -370086
> -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
> -370086 -370086
> -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
> -370086 -370086
> -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
> -370086 -370086
>
> "D:\siju\lcc6\noisedisp.exe"
> Return code 0
> Execution time 0.048 seconds
> Press any key to continue...
>
> why is this so..????

sorry I forgot to declare i, but still the o/p is same
0
Reply aarklon (253) 4/19/2008 9:32:38 PM

aarklon@gmail.com wrote, On 19/04/08 22:31:
> sorry I forgot to declare i , but still the o/p is same
> 
>  #include <string.h>
>  #include <stdio.h>
>  #include <stdlib.h>
> 
>  int main(void)
>  {
>    int maxpat[80],i;
>    memset(maxpat,-1,80);

<snip>

Check the definition of memset in your reference book, it sets a 
specified number of *bytes*. This also means that having corrected the 
error (sizeof *maxpat is your friend) it won't be portable to 
implementations using 1s complement or sign-magnitude if you can find 
such an implementation.
-- 
Flash Gordon
0
Reply spam331 (4024) 4/19/2008 9:36:29 PM

aarklon@gmail.com wrote:
> Hi,
> 
> the following is actually a part of the pattern matching program which
> i tried ,memset is not setting the entire integer array with
> -1
> 
>  #include <string.h>
>  #include <stdio.h>
>  #include <stdlib.h>
> 
>  int main(void)
>  {
> 
>    int maxpat[80];
>    memset(maxpat,-1,80);
> 
You are only setting the first 80 bytes of the the array, not 80 ints.

-- 
Ian Collins.
0
Reply ian-news (9881) 4/19/2008 9:38:51 PM

<aarklon@gmail.com> wrote in message 
news:c7633ee9-954f-4ad8-a0c0-646fbe8e8e81@b5g2000pri.googlegroups.com...
> Hi,
>
> the following is actually a part of the pattern matching program which
> i tried ,memset is not setting the entire integer array with
> -1
>
> #include <string.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
>
>   int maxpat[80];
>   memset(maxpat,-1,80);

You're setting 80 bytes. The array is bigger than that (80*sizeof(int)). 
Try:

 memset(maxpat,-1,sizeof maxpat);

-- 
Bart



0
Reply bc (2211) 4/19/2008 9:45:16 PM

aarklon@gmail.com wrote:

> Hi,
> 
> the following is actually a part of the pattern matching program which
> i tried ,memset is not setting the entire integer array with
> -1

>    int maxpat[80];
>    memset(maxpat,-1,80);

As the others have pointed out, it sets each byte to the value. There's
almost no way to do what you want with memset(), unless you happen to
be on a machine that had one-byte ints.

To set each int in the array to -1, you need a loop. Better yet,
explain WHY you want to do this. There may be a better construct.



Brian
0
Reply defaultuserbr (3657) 4/19/2008 11:43:40 PM

In article <66vedbF2m6h5lU1@mid.individual.net>,
Default User <defaultuserbr@yahoo.com> wrote:

>>    int maxpat[80];
>>    memset(maxpat,-1,80);

>As the others have pointed out, it sets each byte to the value. There's
>almost no way to do what you want with memset(), unless you happen to
>be on a machine that had one-byte ints.

Unless you really on the machines being 2s-complement, which is true
of all general-purpose computers today.

-- Richard
-- 
:wq
0
Reply richard91 (3683) 4/20/2008 12:25:27 AM

"Default User" <defaultuserbr@yahoo.com> writes:

> aarklon@gmail.com wrote:
>
>> Hi,
>> 
>> the following is actually a part of the pattern matching program which
>> i tried ,memset is not setting the entire integer array with
>> -1
>
>>    int maxpat[80];
>>    memset(maxpat,-1,80);
>
> As the others have pointed out, it sets each byte to the value. There's
> almost no way to do what you want with memset(), unless you happen to
> be on a machine that had one-byte ints.
>
> To set each int in the array to -1, you need a loop. Better yet,
> explain WHY you want to do this. There may be a better construct.

I would be interested to hear how you knowing WHY he needs to set a
sequence of ints to a single value would in any way "optimise" your
advice.


>
>
>
> Brian
0
Reply devr_ (448) 4/20/2008 3:29:37 AM

Richard Tobin wrote:
> Default User <defaultuserbr@yahoo.com> wrote:
> 
>>>    int maxpat[80];
>>>    memset(maxpat,-1,80);
> 
>> As the others have pointed out, it sets each byte to the value.
>> There's almost no way to do what you want with memset(), unless
>> you happen to be on a machine that had one-byte ints.
> 
> Unless you really on the machines being 2s-complement, which is
> true of all general-purpose computers today.

What's the point of the foolishness, when:

  #define MPSIZE 80
  #define VALUE  -1
  int     maxpat[MPSIZE], i;

  for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;

handles it accurately and portably, doesn't care about what VALUE
is, and preserves the value of MPSIZE for any other uses.  Note
that the bit of code above doesn't have to be in the scope of
maxpat, rather it can receive it as a parameter.

-- 
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.


** Posted from http://www.teranews.com **
0
Reply cbfalconer (19183) 4/20/2008 4:06:41 AM

Richard wrote:
) In article <66vedbF2m6h5lU1@mid.individual.net>,
) Default User <defaultuserbr@yahoo.com> wrote:
)
)>>    int maxpat[80];
)>>    memset(maxpat,-1,80);
)
)>As the others have pointed out, it sets each byte to the value. There's
)>almost no way to do what you want with memset(), unless you happen to
)>be on a machine that had one-byte ints.
)
) Unless you really on the machines being 2s-complement, which is true
) of all general-purpose computers today.

And then he wants to set the entire array to -2 instead.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
0
Reply willem (1478) 4/20/2008 7:04:03 AM

CBFalconer <cbfalconer@yahoo.com> writes:
[...]
>   #define MPSIZE 80
>   #define VALUE  -1
>   int     maxpat[MPSIZE], i;
>
>   for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
[...]

Quibble:
    #define VALUE (-1)
It doesn't matter in this case, but it's a good habit.

-- 
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21474) 4/20/2008 7:34:40 AM

Keith Thompson wrote:
> CBFalconer <cbfalconer@yahoo.com> writes:
> [...]
>>   #define MPSIZE 80
>>   #define VALUE  -1
>>   int     maxpat[MPSIZE], i;
>>
>>   for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
> [...]
> 
> Quibble:
>     #define VALUE (-1)
> It doesn't matter in this case, but it's a good habit.
> 
Better still

const int value = -1;

-- 
Ian Collins.
0
Reply ian-news (9881) 4/20/2008 8:14:31 AM

Ian Collins wrote:
> Keith Thompson wrote:
>> CBFalconer <cbfalconer@yahoo.com> writes:
>>
>> [...]
>>
>>>   #define MPSIZE 80
>>>   #define VALUE  -1
>>>   int     maxpat[MPSIZE], i;
>>>
>>>   for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
>> [...]
>>
>> Quibble:
>>     #define VALUE (-1)
>>
>> It doesn't matter in this case, but it's a good habit.
>
> Better still
> 
> const int value = -1;

The quibble is fine, but this last is a horror.  We want a
constant, not a pre-initialized int.  If you want c++, use the
appropriate newsgroup.

-- 
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.


** Posted from http://www.teranews.com **
0
Reply cbfalconer (19183) 4/20/2008 9:17:04 AM

CBFalconer wrote:
> Ian Collins wrote:
>> Keith Thompson wrote:
>>> CBFalconer <cbfalconer@yahoo.com> writes:
>>>
>>> [...]
>>>
>>>>   #define MPSIZE 80
>>>>   #define VALUE  -1
>>>>   int     maxpat[MPSIZE], i;
>>>>
>>>>   for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
>>> [...]
>>>
>>> Quibble:
>>>     #define VALUE (-1)
>>>
>>> It doesn't matter in this case, but it's a good habit.
>> Better still
>>
>> const int value = -1;
> 
> The quibble is fine, but this last is a horror.  We want a
> constant, not a pre-initialized int.  If you want c++, use the
> appropriate newsgroup.
> 
I think you've topped your own stupidity this time.

-- 
Ian Collins.
0
Reply ian-news (9881) 4/20/2008 11:15:51 AM

On Apr 20, 4:43=A0am, "Default User" <defaultuse...@yahoo.com> wrote:

> To set each int in the array to -1, you need a loop. Better yet,
> explain WHY you want to do this. There may be a better construct.


actually it was for doing the following program


/*program to find the maximum matching pattern in the string*/

#include < string.h >
#include < stdio.h >
#include < stdlib.h >

void findmaxpat(char*,char*,int*);
void printmaxpat(char*,int*);

int main(void)
{

int i;

char s[80];
char pat[80];
int maxpat[80];


memset(maxpat,-1,80 * sizeof(int));

printf("\n Enter the main string:: ");
gets(s);

while(*s)
{
printf("\n Enter the pattern to be searched:: ");
gets(pat);

findmaxpat(s,pat,maxpat);
printmaxpat(s,maxpat);

printf("\n Enter the main string:: ");
gets(s);
}

return EXIT_SUCCESS;
}

void findmaxpat(char* s,char* pat,int* maxpat)
{

int i;

for(i=3D0;*s && *pat;i++,s++)

if(*s =3D=3D *pat)
*maxpat++ =3D i,pat++;

if(!*pat)
puts("whole pattern found...");

else
printf("pattern not found in the string ");

*maxpat =3D -1;

}


void printmaxpat(char* s,int* maxpat)
{

char *temp =3D s;

(*maxpat =3D=3D -1)? printf("\"%s\"\n",s) :puts(s);

for(;*temp && *maxpat !=3D -1;++temp)
{

if(temp-s =3D=3D *maxpat)
{
printf("^");
maxpat++;
}
else
printf("%c",' ');
}

puts("");
}

0
Reply aarklon (253) 4/20/2008 3:06:30 PM

"Flash Gordon" <spam@flash-gordon.me.uk> schreef in bericht 
news:085qd5x7k9.ln2@news.flash-gordon.me.uk...
> aarklon@gmail.com wrote, On 19/04/08 22:31:
>> sorry I forgot to declare i , but still the o/p is same
>>
>>  #include <string.h>
>>  #include <stdio.h>
>>  #include <stdlib.h>
>>
>>  int main(void)
>>  {
>>    int maxpat[80],i;
>>    memset(maxpat,-1,80);
>
> <snip>
>
> Check the definition of memset in your reference book, it sets a specified 
> number of *bytes*. This also means that having corrected the error (sizeof 
> *maxpat is your friend) it won't be portable to implementations using 1s 
> complement or sign-magnitude if you can find such an implementation.

Why not? The program will fill maxpat with whatever -1 represents and print 
that.
Perfectly portable 

0
Reply ni7369 (72) 4/20/2008 3:44:54 PM

"Serve Laurijssen" <ni@hao.com> writes:

> "Flash Gordon" <spam@flash-gordon.me.uk> schreef in bericht
> news:085qd5x7k9.ln2@news.flash-gordon.me.uk...
>> aarklon@gmail.com wrote, On 19/04/08 22:31:
>>> sorry I forgot to declare i , but still the o/p is same
>>>
>>>  #include <string.h>
>>>  #include <stdio.h>
>>>  #include <stdlib.h>
>>>
>>>  int main(void)
>>>  {
>>>    int maxpat[80],i;
>>>    memset(maxpat,-1,80);
>>
>> <snip>
>>
>> Check the definition of memset in your reference book, it sets a
>> specified number of *bytes*. This also means that having corrected
>> the error (sizeof *maxpat is your friend) it won't be portable to
>> implementations using 1s complement or sign-magnitude if you can
>> find such an implementation.
>
> Why not? The program will fill maxpat with whatever -1 represents and
> print that.
> Perfectly portable 

The OP expressed his desire to set all *integers* to -1. Which it will
do if he changed the 80 in the memset to 80*sizeof(int) and the
architecture was 2's complement.

0
Reply devr_ (448) 4/20/2008 5:08:09 PM

On Sun, 20 Apr 2008 17:44:54 +0200, "Serve Laurijssen" <ni@hao.com>
wrote:

>
>"Flash Gordon" <spam@flash-gordon.me.uk> schreef in bericht 
>news:085qd5x7k9.ln2@news.flash-gordon.me.uk...
>> aarklon@gmail.com wrote, On 19/04/08 22:31:
>>> sorry I forgot to declare i , but still the o/p is same
>>>
>>>  #include <string.h>
>>>  #include <stdio.h>
>>>  #include <stdlib.h>
>>>
>>>  int main(void)
>>>  {
>>>    int maxpat[80],i;
>>>    memset(maxpat,-1,80);
>>
>> <snip>
>>
>> Check the definition of memset in your reference book, it sets a specified 
>> number of *bytes*. This also means that having corrected the error (sizeof 
>> *maxpat is your friend) it won't be portable to implementations using 1s 
>> complement or sign-magnitude if you can find such an implementation.
>
>Why not? The program will fill maxpat with whatever -1 represents and print 
>that.
>Perfectly portable 

What is the representation of -1 in a sign magnitude system with a
32-bit int and an 8 bit char?  How much of that representation will
memset use?  What will be the resulting bit pattern in one of the
elements of maxpat?  What value will this represent?  Will this
achieve the intended objective as posed in the original post?  How is
this value different from a 2s-complement machine?  From a
1s-complement machine?


Remove del for email
0
Reply schwarzb3978 (1358) 4/20/2008 6:28:45 PM

On Sun, 20 Apr 2008 08:06:30 -0700 (PDT), aarklon@gmail.com wrote:

>On Apr 20, 4:43�am, "Default User" <defaultuse...@yahoo.com> wrote:
>
>> To set each int in the array to -1, you need a loop. Better yet,
>> explain WHY you want to do this. There may be a better construct.
>
>
>actually it was for doing the following program
>
>
>/*program to find the maximum matching pattern in the string*/
>
>#include < string.h >
>#include < stdio.h >
>#include < stdlib.h >
>
>void findmaxpat(char*,char*,int*);
>void printmaxpat(char*,int*);
>
>int main(void)
>{
>
>int i;
>
>char s[80];
>char pat[80];
>int maxpat[80];
>
>
>memset(maxpat,-1,80 * sizeof(int));
>
>printf("\n Enter the main string:: ");
>gets(s);
>
>while(*s)
>{
>printf("\n Enter the pattern to be searched:: ");
>gets(pat);
>
>findmaxpat(s,pat,maxpat);
>printmaxpat(s,maxpat);
>
>printf("\n Enter the main string:: ");
>gets(s);
>}
>
>return EXIT_SUCCESS;
>}
>
>void findmaxpat(char* s,char* pat,int* maxpat)
>{
>
>int i;
>
>for(i=0;*s && *pat;i++,s++)
>
>if(*s == *pat)
>*maxpat++ = i,pat++;

Let s contain "abcde" and pat contain "ade".

>
>if(!*pat)
>puts("whole pattern found...");

This is what you print and may actually reflect what you want but it
is not the normal meaning of the term "pattern matching".  For a
pattern match to occur, s would have to contain the letters a-d-e
consecutively in sequence.

In terms of c functions, pattern matching is strstr.  What you have is
similar to multiple calls to strpbrk or strcspn

>
>else
>printf("pattern not found in the string ");
>
>*maxpat = -1;
>
>}
>
>
>void printmaxpat(char* s,int* maxpat)
>{
>
>char *temp = s;
>
>(*maxpat == -1)? printf("\"%s\"\n",s) :puts(s);
>
>for(;*temp && *maxpat != -1;++temp)
>{
>
>if(temp-s == *maxpat)

You need to learn to indent your code.  It will save you a ton of
trouble later on.

>{
>printf("^");
>maxpat++;
>}
>else
>printf("%c",' ');

Strange that above you put the constant character in the format string
while hear you printed it with %c.  In both cases, putc would be the
function of choice.

>}
>
>puts("");
>}


Remove del for email
0
Reply schwarzb3978 (1358) 4/20/2008 6:28:46 PM

Serve Laurijssen wrote:
> ...
> Why not? The program will fill maxpat with whatever -1 represents and 
> print that.
> Perfectly portable

The program will re-interpret the memory occupied by 'maxpat' as an array of 
characters, all of which will be filled with the value of '(unsigned char) -1'. 
There's nothing portable there.

-- 
Best regards,
Andrey Tarasevich
0
Reply andreytarasevich (1531) 4/20/2008 9:04:45 PM

Richard Tobin wrote:
> ...
> Unless you really on the machines being 2s-complement, which is true
> of all general-purpose computers today.
> ...

Being 2's-complement is not enough. You also need the machine to have either no 
padding bits in 'int' or accepting the resultant configuration of padding bits 
as valid. It is also true for all general-purpose computers today, but 
nevertheless...

-- 
Best regards,
Andrey Tarasevich
0
Reply andreytarasevich (1531) 4/20/2008 9:08:35 PM

CBFalconer <cbfalconer@yahoo.com> writes:
> Ian Collins wrote:
>> Keith Thompson wrote:
>>> CBFalconer <cbfalconer@yahoo.com> writes:
>>>
>>> [...]
>>>
>>>>   #define MPSIZE 80
>>>>   #define VALUE  -1
>>>>   int     maxpat[MPSIZE], i;
>>>>
>>>>   for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
>>> [...]
>>>
>>> Quibble:
>>>     #define VALUE (-1)
>>>
>>> It doesn't matter in this case, but it's a good habit.
>>
>> Better still
>> 
>> const int value = -1;

Yes, that's fine in this case, but only because it's not used in a
context that requires a constant expression.  If it were, you'd need
to use either a macro or the enum trick:
    enum { VALUE = -1 };
which works only for values of type int.

> The quibble is fine, but this last is a horror.  We want a
> constant, not a pre-initialized int.  If you want c++, use the
> appropriate newsgroup.

It's perfect valid C.  (The fact that it's also valid C++, and that
C++'s "const" is more flexible that C's, hardly seems relevant.)

Consider the following version of the OP's program:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    #define MPSIZE 80
    const int value = -1;
    int maxpat[MPSIZE];
    int i;

    for (i = 0; i < MPSIZE; i++) {
        maxpat[i] = value;
    }

    for(i = 0; i < MPSIZE; i++) {
        if (i > 0 && i % 8 == 0) {
            putchar('\n');
        }
        printf("%6d  ", maxpat[i]);
    }
    putchar('\n');

    return EXIT_SUCCESS;
}

It works correctly, and IMHO the use of a const int object isn't even
a style problem.  And I would expect any decent compiler to generate
the same code as for a similar program using "#define VALUE (-1)"
<OT>; gcc does so at "-O1" or higher</OT>.

-- 
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21474) 4/20/2008 9:31:39 PM

"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote in message 
news:J7WdnSXEeIX8LZbVnZ2dnUVZ_uidnZ2d@comcast.com...
> Richard Tobin wrote:
>> ...
>> Unless you really on the machines being 2s-complement, which is true
>> of all general-purpose computers today.
>> ...
>
> Being 2's-complement is not enough. You also need the machine to have 
> either no padding bits in 'int' or accepting the resultant configuration 
> of padding bits as valid. It is also true for all general-purpose 
> computers today, but nevertheless...

If I worried about all this stuff years ago I would never have got anything 
done, or it would have been too slow to be useful.

It's possible the OP knows what his target architecture is using for number 
representation.

The OP's initial problem was the wrong N parameter to memset, he said 
nothing about the rest of it, and in fact it looked like the 80 bytes of the 
array he did manage to set were properly initialised to -1.

But no-one has (yet) mentioned the use of gets() in his later message which 
usually is picked up quickly here.

-- 
Bart



0
Reply bc (2211) 4/20/2008 9:52:24 PM

aarklon@gmail.com writes:
> On Apr 20, 4:43�am, "Default User" <defaultuse...@yahoo.com> wrote:
>> To set each int in the array to -1, you need a loop. Better yet,
>> explain WHY you want to do this. There may be a better construct.
>
> actually it was for doing the following program
>
> /*program to find the maximum matching pattern in the string*/
>
> #include < string.h >
> #include < stdio.h >
> #include < stdlib.h >

You can't have spaces after the '<' or before the '>' in a #include
directive.  My compiler complains that it can't find headers
" string.h ", " stdio.h ", or " stdlib.h ".  I'm surprised yours
accepts it, but since the interpretation of header names is
implementation-specific, it's possible.  But the correct directives
are:

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>

Your article was encoded as "quoted-printable", which means that '='
characters are encoded as "=3D".  If you can persuade your newsreaqder
to post plain text rather than quoted-printable, it would be helpful.
Possibly my own newsreader should have done the translation when
saving the file, but there's nothing in what you posted that really
requires anything more elaborate than a plain-text encoding.

> void findmaxpat(char*,char*,int*);
> void printmaxpat(char*,int*);
>
> int main(void)
> {
>
> int i;

You declare ``i'', but you never use it.

Please, please, *PLEASE* indent your code properly.  It's much more
difficult to read when everything is shoved over against the left
margin.

It's possible you did indent your code, and your newsreader or
something else killed it.  If so, please try to solve that problem.
If you're using tabs for indentation, try using just spaces.  And make
sure you're using an editor that's designed to work with source code.

[...]
> gets(s);

Never use gets().  Never.  See question 12.23 in the comp.lang.c FAQ,
<http://c-faq.com/>.  (The FAQ is an excellent resource; reading the
whole thing would not be a waste of your time.)

[...]
> (*maxpat == -1)? printf("\"%s\"\n",s) :puts(s);

This is an entirely gratuitous use of the conditional operator; it
does nothing but make your code harder to read.  Just use an if
statement:

    if (*maxpat == -1)
        printf("\"%s\"\n",s); 
    else
        puts(s);

Personally, I always use braces, even for a single statement:

    if (*maxpat == -1) {
        printf("\"%s\"\n",s); 
    }
    else {
        puts(s);
    }

but that's a fairly minor style point; I recommend it, but feel free
to make your own choice.  Using "?:" rather than if/else in a
statement context, on the other hand, is just silly.


> if(temp-s == *maxpat)
> {
> printf("^");
> maxpat++;
> }
> else
> printf("%c",' ');
> }
>
> puts("");
> }

There are two main approaches to writing information to stdout.  You
can use printf for everything, which is a simpler approach (as long as
you're careful about '%' characters with the format string).  Or you
can make use of the make use of the wider variety of output functions,
such as puts, fputs, putchar, and so forth.  Always using printf had
the advantage that there's only one function to learn.  But here,
since you're using puts anyway, I'll suggest that the above 3 output
calls could be written as:

    putchar('^');
    ...
    putchar(' ');
    ...
    putchar('\n');

-- 
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21474) 4/20/2008 9:54:36 PM

On Sun, 20 Apr 2008 17:08:09 UTC, Richard <devr_@gmail.com> wrote:


> 
> The OP expressed his desire to set all *integers* to -1. Which it will
> do if he changed the 80 in the memset to 80*sizeof(int) and the
> architecture was 2's complement.

Which will never do! Because
- the array is defined as an array of int.

memset does not handle ints,
- it gets a pointer to char which can not be an array of int
- it gets an single int that gets shorten to an single char to be 
stored into a single char
- it gets a number of char that is not a  number of ints when sizeof 
char is < sizeof int.
 


-- 
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
0
Reply os2guy1 (1071) 4/20/2008 9:55:38 PM

On Sat, 19 Apr 2008 21:45:16 UTC, "Bartc" <bc@freeuk.com> wrote:

> 
> <aarklon@gmail.com> wrote in message 
> news:c7633ee9-954f-4ad8-a0c0-646fbe8e8e81@b5g2000pri.googlegroups.com...
> > Hi,
> >
> > the following is actually a part of the pattern matching program which
> > i tried ,memset is not setting the entire integer array with
> > -1
> >
> > #include <string.h>
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > int main(void)
> > {
> >
> >   int maxpat[80];
> >   memset(maxpat,-1,80);
> 
> You're setting 80 bytes. The array is bigger than that (80*sizeof(int)). 
> Try:
> 
>  memset(maxpat,-1,sizeof maxpat);
> 
Does even fail on any mashine that uses padding bits because a 
sequence of bytes of (char) (int) -1 mut not give the same bitpattern 
as (int) -1 as the C standard forbids explicity to store a value of 
one type and read of another one:

union {
  char c[2];
  int  i;
} s;

s.c[0] = -1;
s.c[1] = -1;

if (s.i != -1) 
  /* trap/segfault or whatever the iplementation does on illegal 
memory access */

memset handles bytes, not ints. A value of int is not identical with a
sequence of char on some mashines.

-- 
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
0
Reply os2guy1 (1071) 4/20/2008 9:55:38 PM

On Sun, 20 Apr 2008 15:44:54 UTC, "Serve Laurijssen" <ni@hao.com> 
wrote:

> 
> "Flash Gordon" <spam@flash-gordon.me.uk> schreef in bericht 
> news:085qd5x7k9.ln2@news.flash-gordon.me.uk...
> > aarklon@gmail.com wrote, On 19/04/08 22:31:
> >> sorry I forgot to declare i , but still the o/p is same
> >>
> >>  #include <string.h>
> >>  #include <stdio.h>
> >>  #include <stdlib.h>
> >>
> >>  int main(void)
> >>  {
> >>    int maxpat[80],i;
> >>    memset(maxpat,-1,80);
> >
> > <snip>
> >
> > Check the definition of memset in your reference book, it sets a specified 
> > number of *bytes*. This also means that having corrected the error (sizeof 
> > *maxpat is your friend) it won't be portable to implementations using 1s 
> > complement or sign-magnitude if you can find such an implementation.
> 
> Why not? The program will fill maxpat with whatever -1 represents and print 
> that.

No, it will not - except sizeof char is equal sizeo fint on his 
mashine and then it would work perfectly. But the OP shows that on his
mashine is sizeof char < sizeof int, so the program fails miserably as
it shoud.

> Perfectly portable 

No, perfectly importable as the program requires that sizeof char == 
sizeof int, what is not the case on his implementation wand will not 
on mamy other ones too and will occure simply undefined behavior on 
others, because the standard allows padding bits in ints and between 
bytes, so storing a sequence of (char) (int) -1 is not identical as 
storing (int) -i in an array of ints.

memset strores a number of chars into an array of chars and noit as 
the OP thinks a number of ints into an array of its. So the program is
completely absolute importable anyway.

-- 
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
0
Reply os2guy1 (1071) 4/20/2008 9:55:38 PM

On Sun, 20 Apr 2008 11:15:51 UTC, Ian Collins <ian-news@hotmail.com> 
wrote:

> CBFalconer wrote:
> > Ian Collins wrote:
> >> Keith Thompson wrote:
> >>> CBFalconer <cbfalconer@yahoo.com> writes:
> >>>
> >>> [...]
> >>>
> >>>>   #define MPSIZE 80
> >>>>   #define VALUE  -1
> >>>>   int     maxpat[MPSIZE], i;
> >>>>
> >>>>   for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
> >>> [...]
> >>>
> >>> Quibble:
> >>>     #define VALUE (-1)
> >>>
> >>> It doesn't matter in this case, but it's a good habit.
> >> Better still
> >>
> >> const int value = -1;
> > 
> > The quibble is fine, but this last is a horror.  We want a
> > constant, not a pre-initialized int.  If you want c++, use the
> > appropriate newsgroup.
> > 
> I think you've topped your own stupidity this time.
> 
You are faulty. You have to learn the difference between a readonly 
variable (const) and a const (letter, number).

When a variable classified as const were really a const and not only a
variable classified so then it wre legal to have a readonly variable 
on the place a label requires a const.

const int i;

i: is illegal becaue i is not a const.

-- 
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
0
Reply os2guy1 (1071) 4/20/2008 9:55:38 PM

Keith Thompson wrote:
> CBFalconer <cbfalconer@yahoo.com> writes:
>> Ian Collins wrote:
>>> Keith Thompson wrote:
>>>> CBFalconer <cbfalconer@yahoo.com> writes:
>>>>
>>>> [...]
>>>>
>>>>>   #define MPSIZE 80
>>>>>   #define VALUE  -1
>>>>>   int     maxpat[MPSIZE], i;
>>>>>
>>>>>   for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
>>>> [...]
>>>>
>>>> Quibble:
>>>>     #define VALUE (-1)
>>>>
>>>> It doesn't matter in this case, but it's a good habit.
>>>
>>> Better still
>>>
>>> const int value = -1;
> 
> Yes, that's fine in this case, but only because it's not used in
> a context that requires a constant expression.  If it were, you'd
> need to use either a macro or the enum trick:
>     enum { VALUE = -1 };
> which works only for values of type int.
> 
>> The quibble is fine, but this last is a horror.  We want a
>> constant, not a pre-initialized int.  If you want c++, use the
>> appropriate newsgroup.
> 
> It's perfect valid C.  (The fact that it's also valid C++, and that
> C++'s "const" is more flexible that C's, hardly seems relevant.)

So what?  Among other things, it can tend to generate poor code. 
The real constant (from the #define) can be loaded as an immediate
value.  The improper const requires loading an address and then
loading a value from that address.  And, once you have the constant
defined, it is usable elsewhere and is guaranteed to match values. 
All I am saying is that the use of const here is a horrible
misuse.  However the enum is a valid means of generating that
constant.

-- 
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.


** Posted from http://www.teranews.com **
0
Reply cbfalconer (19183) 4/20/2008 10:56:52 PM

CBFalconer wrote:
> Keith Thompson wrote:
>> CBFalconer <cbfalconer@yahoo.com> writes:
>>> Ian Collins wrote:
>>>> Keith Thompson wrote:
>>>>> CBFalconer <cbfalconer@yahoo.com> writes:
>>>>>
>>>>> [...]
>>>>>
>>>>>>   #define MPSIZE 80
>>>>>>   #define VALUE  -1
>>>>>>   int     maxpat[MPSIZE], i;
>>>>>>
>>>>>>   for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
>>>>> [...]
>>>>>
>>>>> Quibble:
>>>>>     #define VALUE (-1)
>>>>>
>>>>> It doesn't matter in this case, but it's a good habit.
>>>> Better still
>>>>
>>>> const int value = -1;
>> Yes, that's fine in this case, but only because it's not used in
>> a context that requires a constant expression.  If it were, you'd
>> need to use either a macro or the enum trick:
>>     enum { VALUE = -1 };
>> which works only for values of type int.
>>
>>> The quibble is fine, but this last is a horror.  We want a
>>> constant, not a pre-initialized int.  If you want c++, use the
>>> appropriate newsgroup.
>> It's perfect valid C.  (The fact that it's also valid C++, and that
>> C++'s "const" is more flexible that C's, hardly seems relevant.)
> 
> So what?  Among other things, it can tend to generate poor code. 

Not on any compiler I've used recently.  It generates the exact same
code as a literal constant.

> The real constant (from the #define) can be loaded as an immediate
> value.  The improper const requires loading an address and then
> loading a value from that address.  

Nope.

> And, once you have the constant
> defined, it is usable elsewhere and is guaranteed to match values. 

It also pollutes any scope where the #define is visible.

> All I am saying is that the use of const here is a horrible
> misuse.  

It isn't, it's perfectly valid.  It's the only way if you want to manage
the scope of the constant.

> However the enum is a valid means of generating that
> constant.
> 
For integer types.

-- 
Ian Collins.
0
Reply ian-news (9881) 4/20/2008 11:22:50 PM

Keith Thompson wrote:

> Your article was encoded as "quoted-printable", which means that '='
> characters are encoded as "=3D".  If you can persuade your newsreaqder
> to post plain text rather than quoted-printable, it would be helpful.
> Possibly my own newsreader should have done the translation when
> saving the file, but there's nothing in what you posted that really
> requires anything more elaborate than a plain-text encoding.

He posts from Google Groups, and that's one of the "features" of that
system. I started displaying the User-Agent header line so that I could
tell when a message was from GG (it will be set to G2/1.0).

> It's possible you did indent your code, and your newsreader or
> something else killed it.  If so, please try to solve that problem.
> If you're using tabs for indentation, try using just spaces.  And make
> sure you're using an editor that's designed to work with source code.

GG might eat tabs, or he might have neglected to indent. Spaces work
ok.




Brian
0
Reply defaultuserbr (3657) 4/21/2008 12:54:33 AM

CBFalconer <cbfalconer@yahoo.com> writes:

> Keith Thompson wrote:
>> CBFalconer <cbfalconer@yahoo.com> writes:
>>> Ian Collins wrote:
>>>> Keith Thompson wrote:
>>>>> CBFalconer <cbfalconer@yahoo.com> writes:
>>>>>
>>>>> [...]
>>>>>
>>>>>>   #define MPSIZE 80
>>>>>>   #define VALUE  -1
>>>>>>   int     maxpat[MPSIZE], i;
>>>>>>
>>>>>>   for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
>>>>> [...]
>>>>>
>>>>> Quibble:
>>>>>     #define VALUE (-1)
>>>>>
>>>>> It doesn't matter in this case, but it's a good habit.
>>>>
>>>> Better still
>>>>
>>>> const int value = -1;
>> 
>> Yes, that's fine in this case, but only because it's not used in
>> a context that requires a constant expression.  If it were, you'd
>> need to use either a macro or the enum trick:
>>     enum { VALUE = -1 };
>> which works only for values of type int.
>> 
>>> The quibble is fine, but this last is a horror.  We want a
>>> constant, not a pre-initialized int.  If you want c++, use the
>>> appropriate newsgroup.
>> 
>> It's perfect valid C.  (The fact that it's also valid C++, and that
>> C++'s "const" is more flexible that C's, hardly seems relevant.)
>
> So what?  Among other things, it can tend to generate poor code. 

Your views (incorrect at that) on compiler specifics are clearly off
topic. I am sure you would be the first to point that out to others who
were to post such rubbish. 

> The real constant (from the #define) can be loaded as an immediate
> value.  The improper const requires loading an address and then
> loading a value from that address.  And, once you have the constant
> defined, it is usable elsewhere and is guaranteed to match values. 
> All I am saying is that the use of const here is a horrible
> misuse.  However the enum is a valid means of generating that
> constant.


0
Reply devr_ (448) 4/21/2008 12:57:43 AM

Richard wrote:

> CBFalconer <cbfalconer@yahoo.com> writes:
> 
>> Keith Thompson wrote:
>>> CBFalconer <cbfalconer@yahoo.com> writes:
>>>> Ian Collins wrote:
>>>>> Keith Thompson wrote:
>>>>>> CBFalconer <cbfalconer@yahoo.com> writes:
>>>>>>
>>>>>> [...]
>>>>>>
>>>>>>>   #define MPSIZE 80
>>>>>>>   #define VALUE  -1
>>>>>>>   int     maxpat[MPSIZE], i;
>>>>>>>
>>>>>>>   for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
>>>>>> [...]
>>>>>>
>>>>>> Quibble:
>>>>>>     #define VALUE (-1)
>>>>>>
>>>>>> It doesn't matter in this case, but it's a good habit.
>>>>>
>>>>> Better still
>>>>>
>>>>> const int value = -1;
>>> 
>>> Yes, that's fine in this case, but only because it's not used in
>>> a context that requires a constant expression.  If it were, you'd
>>> need to use either a macro or the enum trick:
>>>     enum { VALUE = -1 };
>>> which works only for values of type int.
>>> 
>>>> The quibble is fine, but this last is a horror.  We want a
>>>> constant, not a pre-initialized int.  If you want c++, use the
>>>> appropriate newsgroup.
>>> 
>>> It's perfect valid C.  (The fact that it's also valid C++, and that
>>> C++'s "const" is more flexible that C's, hardly seems relevant.)
>>
>> So what?  Among other things, it can tend to generate poor code.
> 
> Your views (incorrect at that) on compiler specifics are clearly off
> topic.

What compiler specifics?

<snip>

0
Reply santosh.k83 (3969) 4/21/2008 1:38:37 AM

santosh <santosh.k83@gmail.com> writes:

> Richard wrote:
>
>> CBFalconer <cbfalconer@yahoo.com> writes:
>> 
>>> Keith Thompson wrote:
>>>> CBFalconer <cbfalconer@yahoo.com> writes:
>>>>> Ian Collins wrote:
>>>>>> Keith Thompson wrote:
>>>>>>> CBFalconer <cbfalconer@yahoo.com> writes:
>>>>>>>
>>>>>>> [...]
>>>>>>>
>>>>>>>>   #define MPSIZE 80
>>>>>>>>   #define VALUE  -1
>>>>>>>>   int     maxpat[MPSIZE], i;
>>>>>>>>
>>>>>>>>   for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
>>>>>>> [...]
>>>>>>>
>>>>>>> Quibble:
>>>>>>>     #define VALUE (-1)
>>>>>>>
>>>>>>> It doesn't matter in this case, but it's a good habit.
>>>>>>
>>>>>> Better still
>>>>>>
>>>>>> const int value = -1;
>>>> 
>>>> Yes, that's fine in this case, but only because it's not used in
>>>> a context that requires a constant expression.  If it were, you'd
>>>> need to use either a macro or the enum trick:
>>>>     enum { VALUE = -1 };
>>>> which works only for values of type int.
>>>> 
>>>>> The quibble is fine, but this last is a horror.  We want a
>>>>> constant, not a pre-initialized int.  If you want c++, use the
>>>>> appropriate newsgroup.
>>>> 
>>>> It's perfect valid C.  (The fact that it's also valid C++, and that
>>>> C++'s "const" is more flexible that C's, hardly seems relevant.)
>>>
>>> So what?  Among other things, it can tend to generate poor code.
>> 
>> Your views (incorrect at that) on compiler specifics are clearly off
>> topic.
>
> What compiler specifics?

Please read the post I replied to. Hint : Falconer would not tolerate
the word "can" since code generation is alien to the standard.

For some reason known only to yourself you then continued to snip all
the "possibles" Falconer posted which are clearly guesswork and liable
to be compiler specific.


0
Reply devr_ (448) 4/21/2008 2:00:18 AM

CBFalconer wrote:
> Keith Thompson wrote:
 >> ...
>> It's perfect valid C.  (The fact that it's also valid C++, and that
>> C++'s "const" is more flexible that C's, hardly seems relevant.)
> 
> So what?  Among other things, it can tend to generate poor code. 
> The real constant (from the #define) can be loaded as an immediate
> value.  The improper const requires loading an address and then
> loading a value from that address.  

No, actually. From the point of view of low-level semantics (i.e. code 
generation) there's absolutely no difference between const-qualified 'int' 
object and a manifest constant (from the #define) in C. A 'const int' object can 
easily be loaded as an immediate value. There's no need to generate a memory 
read. What made you to conclude than a memory read would be necessary?

-- 
Best regards,
Andrey Tarasevich
0
Reply andreytarasevich (1531) 4/21/2008 4:19:50 AM

Ian Collins wrote:
> CBFalconer wrote:
> > However the enum is a valid means of generating that
> > constant.
>
> For integer types.

....in the range of int.

--
Peter
0
Reply airia (1802) 4/21/2008 4:38:56 AM

Richard wrote:

> "Default User" <defaultuserbr@yahoo.com> writes:
> 
>> aarklon@gmail.com wrote:
>>
>>> Hi,
>>> 
>>> the following is actually a part of the pattern matching program which
>>> i tried ,memset is not setting the entire integer array with
>>> -1
>>
>>>    int maxpat[80];
>>>    memset(maxpat,-1,80);
>>
>> As the others have pointed out, it sets each byte to the value. There's
>> almost no way to do what you want with memset(), unless you happen to
>> be on a machine that had one-byte ints.
>>
>> To set each int in the array to -1, you need a loop. Better yet,
>> explain WHY you want to do this. There may be a better construct.
> 
> I would be interested to hear how you knowing WHY he needs to set a
> sequence of ints to a single value would in any way "optimise" your
> advice.

Because it sometimes turns out that the /actual/ problem can be
solved in a different, perhaps better, way. Have you not met 
problems that can be easily solved one higher level of
abstraction than their symptoms?

-- 
"What would that matter, if it made a good book?"                 /Gaudy Night/

Hewlett-Packard Limited registered office:                Cain Road, Bracknell,
registered no: 690597 England                                    Berks RG12 1HN

0
Reply chris.dollin (1683) 4/21/2008 8:11:21 AM

"Herbert Rosenau" <os2guy@pc-rosenau.de> writes:
> On Sun, 20 Apr 2008 11:15:51 UTC, Ian Collins <ian-news@hotmail.com> 
> wrote:
>> CBFalconer wrote:
>> > Ian Collins wrote:
>> >> Keith Thompson wrote:
>> >>> CBFalconer <cbfalconer@yahoo.com> writes:
>> >>>
>> >>> [...]
>> >>>
>> >>>>   #define MPSIZE 80
>> >>>>   #define VALUE  -1
>> >>>>   int     maxpat[MPSIZE], i;
>> >>>>
>> >>>>   for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
>> >>> [...]
>> >>>
>> >>> Quibble:
>> >>>     #define VALUE (-1)
>> >>>
>> >>> It doesn't matter in this case, but it's a good habit.
>> >> Better still
>> >>
>> >> const int value = -1;
>> > 
>> > The quibble is fine, but this last is a horror.  We want a
>> > constant, not a pre-initialized int.  If you want c++, use the
>> > appropriate newsgroup.
>> > 
>> I think you've topped your own stupidity this time.
>> 
> You are faulty. You have to learn the difference between a readonly 
> variable (const) and a const (letter, number).

You mean between a readonly variable (const) and a *constant*.
(Though I prefer to refer to the former as a read-only *object*, just
to avoid the confusion of a "variable" that isn't allowed to vary.)

But the point is that in the above code a const-qualified object will
work; the context is not one that requires a constant expression.

Even though the word "const" is derived from "constant", IMHO it's
best to pretend that "const" is *not* an abbreviation for "constant",
and particularly to avoid using the two words interchangeably.

> When a variable classified as const were really a const and not only a
> variable classified so then it wre legal to have a readonly variable 
> on the place a label requires a const.
> 
> const int i;
>
> i: is illegal becaue i is not a const.

Um, do you think you could rephrase that?

Oddly enough, the declaration
    const int i;
is legal, though not at all useful (at least at block scope).

-- 
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21474) 4/21/2008 8:21:53 AM

Ian Collins <ian-news@hotmail.com> writes:
> CBFalconer wrote:
[...]
>> However the enum is a valid means of generating that
>> constant.
>> 
> For integer types.

Only for type int, actually.  This:

    enum { too_big = INT_MAX + 1 };

is invalid (though I'm not sure which kind of invalid it is).

-- 
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21474) 4/21/2008 8:24:35 AM

Herbert Rosenau wrote:
> On Sun, 20 Apr 2008 11:15:51 UTC, Ian Collins <ian-news@hotmail.com> 
> wrote:
> 
>> CBFalconer wrote:
>>> The quibble is fine, but this last is a horror.  We want a
>>> constant, not a pre-initialized int.  If you want c++, use the
>>> appropriate newsgroup.
>>>
>> I think you've topped your own stupidity this time.
>>
> You are faulty. You have to learn the difference between a readonly 
> variable (const) and a const (letter, number).
> 
Nonsense.  Use of const int was correct in this situation.

> When a variable classified as const were really a const and not only a
> variable classified so then it wre legal to have a readonly variable 
> on the place a label requires a const.
> 
I don't really understand that sentence, care to try again?

> const int i;
> 
> i: is illegal becaue i is not a const.
> 
It's legal by C's daft const rules, but useless.

-- 
Ian Collins.
0
Reply ian-news (9881) 4/21/2008 9:00:26 AM

Keith Thompson wrote:
> Ian Collins <ian-news@hotmail.com> writes:
>> CBFalconer wrote:
> [...]
>>> However the enum is a valid means of generating that
>>> constant.
>>>
>> For integer types.
> 
> Only for type int, actually.  This:
> 
>     enum { too_big = INT_MAX + 1 };
> 
> is invalid (though I'm not sure which kind of invalid it is).
> 
Probably the invalid type...

Which all goes to strengthen my case that an initialised, const
qualified variable is the best choice for anything other than a compile
time constant.

-- 
Ian Collins.
0
Reply ian-news (9881) 4/21/2008 9:03:22 AM

On Mon, 21 Apr 2008 01:24:35 -0700, Keith Thompson wrote:
> This:
> 
>     enum { too_big = INT_MAX + 1 };
> 
> is invalid (though I'm not sure which kind of invalid it is).

It's a constraint violation, according to the interpretation at
  <http://open-std.org/JTC1/SC22/WG14/www/docs/dr_031.html>
0
Reply truedfx (1926) 4/21/2008 4:17:18 PM

"Barry Schwarz" <schwarzb@dqel.com> schreef in bericht 
news:b6um045ib8jv5b4onih8420as9n24a1fqa@4ax.com...
> What is the representation of -1 in a sign magnitude system with a
> 32-bit int and an 8 bit char?  How much of that representation will
> memset use?  What will be the resulting bit pattern in one of the
> elements of maxpat?  What value will this represent?  Will this
> achieve the intended objective as posed in the original post?  How is
> this value different from a 2s-complement machine?  From a
> 1s-complement machine?

yes yes I dont think you all understand me, but never mind




>
>
> Remove del for email 

0
Reply ni7369 (72) 4/21/2008 5:11:55 PM

Andrey Tarasevich wrote:
> CBFalconer wrote:
>> Keith Thompson wrote:
>>> ...
>>> It's perfect valid C.  (The fact that it's also valid C++, and that
>>> C++'s "const" is more flexible that C's, hardly seems relevant.)
>>
>> So what?  Among other things, it can tend to generate poor code.
>> The real constant (from the #define) can be loaded as an immediate
>> value.  The improper const requires loading an address and then
>> loading a value from that address.
> 
> No, actually. From the point of view of low-level semantics (i.e.
> code generation) there's absolutely no difference between const-
> qualified 'int' object and a manifest constant (from the #define)
> in C. A 'const int' object can easily be loaded as an immediate
> value. There's no need to generate a memory read. What made you to
> conclude than a memory read would be necessary?

Because a const qualified object is simply marked as read-only. 
There are various ways of getting around this and arranging to
change what that object is holding.  At which point any code that
treats its value as being a constant is WRONG.

-- 
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.


** Posted from http://www.teranews.com **
0
Reply cbfalconer (19183) 4/21/2008 11:45:40 PM

CBFalconer wrote:
> Andrey Tarasevich wrote:
>> CBFalconer wrote:
>>> Keith Thompson wrote:
>>>> ...
>>>> It's perfect valid C.  (The fact that it's also valid C++, and that
>>>> C++'s "const" is more flexible that C's, hardly seems relevant.)
>>> So what?  Among other things, it can tend to generate poor code.
>>> The real constant (from the #define) can be loaded as an immediate
>>> value.  The improper const requires loading an address and then
>>> loading a value from that address.
>> No, actually. From the point of view of low-level semantics (i.e.
>> code generation) there's absolutely no difference between const-
>> qualified 'int' object and a manifest constant (from the #define)
>> in C. A 'const int' object can easily be loaded as an immediate
>> value. There's no need to generate a memory read. What made you to
>> conclude than a memory read would be necessary?
> 
> Because a const qualified object is simply marked as read-only. 
> There are various ways of getting around this and arranging to
> change what that object is holding.  At which point any code that
> treats its value as being a constant is WRONG.
> 
No, the code that's "getting around this" is wrong.  Once you mess about
with the value of a const qualified variable, you unleash the demons of
Undefined Behaviour.

-- 
Ian Collins.
0
Reply ian-news (9881) 4/22/2008 1:30:31 AM

CBFalconer <cbfalconer@yahoo.com> writes:
> Andrey Tarasevich wrote:
>> CBFalconer wrote:
>>> Keith Thompson wrote:
>>>> ...
>>>> It's perfect valid C.  (The fact that it's also valid C++, and that
>>>> C++'s "const" is more flexible that C's, hardly seems relevant.)
>>>
>>> So what?  Among other things, it can tend to generate poor code.
>>> The real constant (from the #define) can be loaded as an immediate
>>> value.  The improper const requires loading an address and then
>>> loading a value from that address.
>> 
>> No, actually. From the point of view of low-level semantics (i.e.
>> code generation) there's absolutely no difference between const-
>> qualified 'int' object and a manifest constant (from the #define)
>> in C. A 'const int' object can easily be loaded as an immediate
>> value. There's no need to generate a memory read. What made you to
>> conclude than a memory read would be necessary?
>
> Because a const qualified object is simply marked as read-only. 
> There are various ways of getting around this and arranging to
> change what that object is holding.  At which point any code that
> treats its value as being a constant is WRONG.

Ok, consider this:

#include <stdio.h>
int main(void)
{
    const int x = 42;

    /* ... */

    printf("x = %d\n", x);
    return 0;
}

Can you think of something you could insert in place of the comment
that would change the value of x *without invoking undefined
behavior*?  (Hint: No.)

Both the compiler and the programmer are entitled to assume that the
value of x is 42 at the point of the printf call.  The compiler could
legally replace the call with puts("x = 42").  If you insert code to
change the value of x, then you've invoked undefined behavior, and
printing "x = 42" is merely one possible consequence.

The compiler is not required to replace references to x with the
constant 42, but it's certainly allowed to do so.  (Note that this
might not even be an optimization; depending on the CPU, you might get
faster code by loading the value of x, particularly if it happens to
be stored in a register, than by loading a literal value.)

-- 
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21474) 4/22/2008 8:25:10 AM

46 Replies
40 Views

(page loaded in 0.459 seconds)


Reply: