• Advertisement

Archived

This topic is now archived and is closed to further replies.

convoluted code

This topic is 5311 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

While studying c++ and directX, I come across some code that is just unbelieveably contorted and twisted. If the code were a person it would be one of those 8 year old russian contortionists that can put their feet on their head by bending backwards. Im just currious if anyone can make any simple, short, and utterly pointless convolutions of their own. Just for fun. Time for an example : (brace yourself for an array of function pointers that clears out pointer arrays, and the functions to do it. Dont even get me started on recursion.)
#include <stdio.h>

int (*pFnState)(float num);
char *text = NULL;

int GSstart(float num);
int GScredits(float num);
int GSclose(float num);

int GSstart(float num)
{
	if(!text) text = new char;

	sprintf(text, "num = %f", num);
	MessageBox(0, "GSstart() entered", text, 0); 

	if(text) {text = NULL; delete text;}
	pFnState = GScredits;
	return num + 1;
}

int GScredits(float num)
{
	if(!text) text = new char;

	sprintf(text, "num = %f", num);
	MessageBox(0, "GScredits() entered", text, 0); 

	if(text) {text = NULL; delete text;}
	pFnState = GSclose;
	return num + 1;
}

int GSclose(float num)
{
	PostQuitMessage(0);
	return 1;
}

void Fn()
{

/*Assigning the return value of a function that keeps changing what its poing to to a member of an array pointed at by x. This is the twisted part.*/

	const SIZE_OF_ARRAY = 5;
	int *x[SIZE_OF_ARRAY];

	pFnState = GSstart;

	for(int a = 0; a < SIZE_OF_ARRAY; a++)
	{
		xLink 

			
		

Share this post


Link to post
Share on other sites
Advertisement
quote:
if(text) {text = NULL; delete text;}

Ah, good. A conditional to ensure that the memory is leaked correctly.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
quote:
if(text) {text = NULL; delete text;}

Ah, good. A conditional to ensure that the memory is leaked correctly.


How appropriate. You fight like a cow.


Am I missing something or did it just set a pointer to NULL and then delete it?? Have you gotten this code to actually run? I would think that would cause a run-time error.

Share this post


Link to post
Share on other sites
The correct way would be:
delete text; text = NULL;
Of course, I''m not even going into your misuse of new a few lines earlier... I''ll leave that to other posters.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Hey, Im self taught. Maybe if I werent a bus driver and lived in kansas Id know what you meant, (Im looking it up now). Tell me whats wrong with if(text) {text = NULL; delete text;} and why it makes memory leaks. Plus, post some convoluted code.

bored,
-Jason

Share this post


Link to post
Share on other sites
quote:
Original post by nosajghoul
Hey, Im self taught. Maybe if I werent a bus driver and lived in kansas Id know what you meant, (Im looking it up now). Tell me whats wrong with if(text) {text = NULL; delete text;} and why it makes memory leaks. Plus, post some convoluted code.

bored,
-Jason


Don't take offense. The pointer should hold the memory adress of, I imagine, an array of chars. So if you first set it to NULL (0) and then delete it, you are attempting to free memory at the memory adress 0.

edit:
And the main problem is that you no longer have anything holding the memory adress of the memory you allocated, so it is just taking up room, and you have no way to delete it.



[edited by - Rayno on October 8, 2003 2:39:11 AM]

Share this post


Link to post
Share on other sites
Oh, and BTW, the code there is using a "finite state machine". It''s an odd-looking beast, but useful. It''s the first time I''ve actually seen it done with function pointers directly, tho; virtual classes and switch statements are more common.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Well... it's like this.

delete text

will release the memory referred to by the pointer named 'text'. However, before that line, you set text = NULL; Thus, your code:
1) Tries to delete memory it doesn't own (the memory at NULL)
1) Calls delete on a null pointer.
2) Fails to delete the memory that text originally pointed to.

edit: ok, ok... delete null is a no-op. so it's not really trying to delete memory it doesn't own... just doing nothing.

[edited by - tibre on October 8, 2003 2:43:29 AM]

Share this post


Link to post
Share on other sites

  • Advertisement