memset o/p not correct

A

aarklon

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);

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..????
 
A

aarklon

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);


return EXIT_SUCCESS;
}
 
A

aarklon

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);

   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..????


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

Flash Gordon

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.
 
I

Ian Collins

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.
 
B

Bartc

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);
 
D

Default User

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
 
R

Richard Tobin

int maxpat[80];
memset(maxpat,-1,80);
[/QUOTE]
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
 
R

Richard

Default User said:
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.
 
W

Willem

Richard wrote:
) In article <[email protected]>,
)
)>> 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
 
K

Keith Thompson

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

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

[...]

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

Ian Collins

Keith said:
CBFalconer said:
#define MPSIZE 80
#define VALUE -1
int maxpat[MPSIZE], i;

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

[...]

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

Better still

const int value = -1;
 
I

Ian Collins

CBFalconer said:
Ian said:
Keith said:
[...]

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

for (i = 0; i < MPSIZE; i++) maxpat = 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.
 
A

aarklon

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++;

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

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)
{
printf("^");
maxpat++;
}
else
printf("%c",' ');
}

puts("");
}
 
S

Serve Laurijssen

Flash Gordon said:
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
 
R

Richard

Serve Laurijssen said:
Flash Gordon said:
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.
 
B

Barry Schwarz

Flash Gordon said:
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
 
B

Barry Schwarz

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
 
K

Keith Thompson

CBFalconer said:
Ian said:
Keith said:
[...]

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

for (i = 0; i < MPSIZE; i++) maxpat = 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 = value;
}

for(i = 0; i < MPSIZE; i++) {
if (i > 0 && i % 8 == 0) {
putchar('\n');
}
printf("%6d ", maxpat);
}
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>.
 
B

Bartc

Andrey Tarasevich said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top