Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

Mindeveloped's blog

By Mindeveloped, history, 6 months ago, In English

Hello, everyone. I'd like to share an issue I met recently of which passing an STL container as a parameter through a function call caused MLE.

Look at this submission: 244198211. I was debugging it when I realized such line is causing MLE.

void insert (int p, string str, int len, int i, int id) {

I thought passing a string as a parameter might be the problem, so I changed my code and passed the index instead, and it got AC (there are other bugs but they don't matter): 244220216

So why is the problem?

The problem is, passing a parameter in non-reference calls the copy constructor and the destructor of the object each once, which means the object is being copied once every function call until it's finished. In normal situations it doesn't seems to be a big problem, but when you try to pass the object multiple times (especially if you're calling the function recursively) the memory usage is getting stacked up.

Note that this situation is the most common that people prefer to pass STL containers, especially string and vector in function parameters.

To make it clear I did some experiment, shown below:

#include<iostream>
using namespace std;

struct node {
	node () {
		cout << "Constructor called" << endl;
	}
	node (node &obj) {
		cout << "Copy constructor called" << endl;
	}
	~node () {
		cout << "Destructor called" << endl;
	}
};
node x;
void normal (node y) {
	cout << "Normal function body" << endl;
}
void reference (node &y) {
	cout << "Reference function body" << endl;
}
int main () {
	cout << "Calling normal function" << endl;
	normal (x);
	cout << "Finished calling normal function. Calling reference function" << endl;
	reference (x);
	cout << "Finished calling reference function" << endl;
	return 0;
}

And the result is:

Constructor called
Calling normal function
Copy constructor called
Normal function body
Destructor called
Finished calling normal function. Calling reference function
Reference function body
Finished calling reference function
Destructor called

From upon you can see that passing object as normal parameter calls copy constructor and destructor each once, and passing as reference does not do anything to the object.

So here are some suggestions to avoid this:

  1. Pass indices or pointers instead of the original object. The straightforward way, though it might makes the code look bad (imo).
  2. Use arrays instead of strings or vectors. Arrays are passed as pointers of the initial address (in other ways, they are passed as references automatically).
  3. Pass reference if you can. It makes the code more readable compared to 1.

EDIT: The idea was originally wrong, but thanks to the guy in comment I've corrected. The point is still valid, but the description needs a bit changing.

Thanks for reading.

Full text and comments »

  • Vote: I like it
  • +8
  • Vote: I do not like it

By Mindeveloped, history, 9 months ago, In English

I'm trying to improve by solving problems Codeforces, but I'm quite unsure about whether it really helps me improve or not. The question is, the problems I meet are either problems that I can solve in my first sight, or else problems I can't solve in an hour. How to fix this? I'm using the problem difficulty filter but it doesn't work because the actual difficulty for me still varies a lot even in a fixed difficulty number.

Full text and comments »

  • Vote: I like it
  • +9
  • Vote: I do not like it

By Mindeveloped, history, 15 months ago, In English

For example, if a successful hacking attempt on Div.2 is added to the system test data, will that case be added to Div.1 too?

Full text and comments »

  • Vote: I like it
  • +11
  • Vote: I do not like it

By Mindeveloped, history, 16 months ago, In English

Only ICPC scoring is available in virtual participation now. So please make Codeforces Scoring an option for virtual participation too.

Several advantages:

  1. It will be easier to see one's speed of doing a contest, as speed is far more effective in Codeforces scoring than ICPC.

  2. It will be even closer to a real contest.

Full text and comments »

  • Vote: I like it
  • +13
  • Vote: I do not like it

By Mindeveloped, history, 2 years ago, In English

When I participate Div.2 contests, I always solve the easy problems(such as A, B or C) quickly, but then I can't even move on other problems. What should I do in that case?

Full text and comments »

  • Vote: I like it
  • +5
  • Vote: I do not like it