Hi guys
I have a small problem. It could be not enough information to obtain the right answer but I will try. So I have a problem regarding liked lists. It works perfectly fine when is compliled under Windows using Dev-C++ compileer. But when I try to complile under Unix system using gcc compiler it gives me bunch of errors. It is C program. I provide you than one particular fuction, so it will be easy to locate the problem. In that case when I compile it says that tehre is 'parse error before struct'. When I will comment if statement, the one just before the struct it will work, but I need that statement. Why I receive the parse error, how may I fix it.
Is it a C syntax?
Thanks

int allocate_mem(int size)
{
	// check ranges
	if (size < 0 || size > 10000)
		return -1;
	struct free_block *best_fit_block = NULL;
	struct free_block *prev_to_best_fit_block = NULL;
	struct free_block *current_block = memory->head;
	struct free_block *previous_block = NULL;
	// system is out of memory
	if (current_block == NULL) return -1;
	// otherwise look over free blocks to find first, which fits
	while (current_block != NULL)
	{
		if (current_block->size >= size)
			break;
		else
		{
			previous_block = current_block;		
			current_block = current_block->next;
		}
	}
	if (current_block == NULL)
	{
		// system is out of blocks, big enough
		return -1;
	}
	prev_to_best_fit_block = previous_block;
	best_fit_block = current_block;
	previous_block = current_block;
	current_block = current_block->next;
	// look over rest and try to find the one, which fits better
	while (current_block != NULL && best_fit_block->size != size)
	{
		if (current_block->size >= size && current_block->size < best_fit_block->size)
		{
			prev_to_best_fit_block = previous_block;
			best_fit_block = current_block;
		}		
		previous_block = current_block;
		current_block = current_block->next;
	}

>It is C program.
No, it's a C++ program. How do I know? Because GCC doesn't compile it and the error sounds very much like an out of order variable declaration, I seriously doubt you're compiling with the "-std=c99" switch that enables mixed declarations. Since it works on Windows, I can only assume that you failed to compile properly as C and those compilers are defaulting to C++.

Prior to C99 (which isn't very well implemented), all variable declarations must be at the beginning of a block. Meaning your if statement at the top of the function is invalid C (prior to C99).

gcc allows the // comments. The only compiler I have come across which doesn't allow // on C code is the AIX C compiler.

This article has been dead for over six months. Start a new discussion instead.