496 - Simply Subsets

The forum to report every bug you find or tell us what you'd like to find in UVa OJ

Moderator: Board moderators

Locked
jbernadas
New poster
Posts: 16
Joined: Tue Apr 24, 2007 11:23 pm
Location: Caracas, Venezuela

496 - Simply Subsets

Post by jbernadas »

Hi everybody. Can someone check the input for this problem. I submitted this code to the new server:

Code: Select all

#include "stdio.h"

int main() {
  static char buffer[1000000];
  int cnt = 0;
  while (fgets(buffer, 1000000, stdin) != NULL)
    +cnt;
  if (cnt % 2 != 0) {
    char* c = NULL;
    *c = '\0';
  }
  return 0;
}
and the server returned Runtime Error. Unless I made a mistake, I think that the input file have an odd number of lines, while the problem statement says that it should have an even number of lines.

Thanks.

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

Hi,

Check this part of your code:

Code: Select all

    char* c = NULL; 
    *c = '\0'; 
You declared a char pointer c pointing to NULL. Then the next line deferences c (pointing to NULL) and this will give a runtime error.

Hope this helps.

hi!man!
New poster
Posts: 48
Joined: Fri Dec 29, 2006 1:26 pm

Post by hi!man! »

Code: Select all

    char* c = NULL; 
    *c = '\0'; 
No, that is not the reason.
*c is not pointer, it is a variable.

Change the code

Code: Select all

#include "stdio.h"
int main() {
  static char buffer[1000000];
  int cnt = 0;
  while (fgets(buffer, 1000000, stdin) != NULL)
    +cnt;
  if (cnt % 2 != 0) {
    char* c = NULL;
    *c = '\0';
  }
  return 0;
}
to this:

Code: Select all

#include "stdio.h"
static char buffer[1000000];
int main() {
  int cnt = 0;
  while (fgets(buffer, 1000000, stdin) != NULL)
    +cnt;
  if (cnt % 2 != 0) {
    char* c = NULL;
    *c = '\0';
  }
  return 0;
} 
should get rid of RE.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

hi!man! wrote:

Code: Select all

char* c = NULL;
*c = '\0';
No, that is not the reason.
*c is not pointer, it is a variable.
It's not a variable. it's an lvalue. It's something at address 0, which in almost all OSes you usually can neither read nor write, so that code causes a runtime error.

But you both miss the main point of jbernadas' code - his program crashes whenever the input contains an odd number of lines. And according to the problem statement, "Your program should accept an even number of lines of text."
So there must something wrong with the input file, or new server. Or maybe his buffer sizes are just too small.

jbernadas
New poster
Posts: 16
Joined: Tue Apr 24, 2007 11:23 pm
Location: Caracas, Venezuela

Post by jbernadas »

mf wrote:So there must something wrong with the input file, or new server. Or maybe his buffer sizes are just too small.
I doubt it is my buffer size, I use the same size in my AC program.

Carlos
System administrator
Posts: 1286
Joined: Sat Oct 13, 2001 2:00 am
Location: Valladolid, Spain
Contact:

Re: 496 - Simply Subsets

Post by Carlos »

shouldn't it be

Code: Select all

++cnt;
instead of

Code: Select all

+cnt;
?
DON'T PM ME --> For any doubt, suggestion or error reporting, please use the "Contact us" form in the web.

Carlos
System administrator
Posts: 1286
Joined: Sat Oct 13, 2001 2:00 am
Location: Valladolid, Spain
Contact:

Re: 496 - Simply Subsets

Post by Carlos »

Input contains an even number of lines, so the topic is closed.
DON'T PM ME --> For any doubt, suggestion or error reporting, please use the "Contact us" form in the web.

Locked

Return to “Bugs and suggestions”