convoluted code

Started by
18 comments, last by nosajghoul 20 years, 6 months ago
I get it, makes sense.

(This was sposed to be a twisted code example post, but hey, I learned something.)
normal_toes@hotmail.com
Advertisement
What they mean is that you are making text point at NULL instead of the previously allocated memory. Then, you are trying to delete a NULL pointer. This is bad for 2 reasons (maybe only one). First, you're trying to delete memory that you didn't allocate. Second, you're trying to delete a NULL pointer. This normally causes a runtime error (such as a crash).

The correct way to delete memory would be

if (text) { delete text; text = NULL; }

And that will not leak memory.

Now, about the misuse of the new operator...

There's really no point in allocating just 1 char so text shouldn't be a char *. it should just be of type char. Then you don't have to worry about deleting it either.

I can understand that you used new to do that since you are trying to make horribly convoluted code. But at least fix the delete problems.

I'm learning, just like the best of us...

Ok, now assume a spherical cow...

Apparently it took me 3 minutes to write that, so people got to you before me

[edited by - Soulkeeper on October 8, 2003 2:44:03 AM]
I'm learning, just like the best of us...Ok, now assume a spherical cow... :)
Crap, now I have questions.

So if I NULL first, delete isnt necessary, is it? Null makes it point to memory address 0 (zero), while delete returns memory to the heap. Hence my thinking: make it nothing, then free it.

Rayno : no offence taken, actually, thanks.

Sneftel : I fight like a cow on hell difficulty with 8 players going after a necro.

Soulkeeper : (This normally causes a runtime error (such as a crash). ) ran fine, closed fine, no errors. Weird huh.




normal_toes@hotmail.com
quote:Original post by nosajghoul
Crap, now I have questions.

So if I NULL first, delete isnt necessary, is it? Null makes it point to memory address 0 (zero), while delete returns memory to the heap. Hence my thinking: make it nothing, then free it.

Rayno : no offence taken, actually, thanks.

Sneftel : I fight like a cow on hell difficulty with 8 players going after a necro.

Soulkeeper : (This normally causes a runtime error (such as a crash). ) ran fine, closed fine, no errors. Weird huh.




Well not quite.

Lets say you have some memory allocated at memory adress 55. So your pointer literly holds the value 55. If you set your pointer to 0, then it doesn''t free the block of memory you allocated, it just makes you pointer, point to the address 0.

The reason pointers are set to NULL after is so if you accidentally accesss the memory pointed to by your pointer, it is safer trying to read/write to 0, than let''s say 55.
to clarify the earlier posts, regarding dynamic allocation and deallocation.

since you wanted to write a string to your allocated memory you need to allocate an array of characters

if(!text) text = new char[someArbitraryNumberOfCharacters];

and once you are done with it

delete[] text;
text = NULL;

its not entirely neccesary to call that version of delete[] on an array of characters,you could just use the normal delete, but if you have an array of classes then delete[] will call the destructors of those classes correctly.

And as allready stated

char *text = NULL;
delete[] text;
text = NULL;

is a safe operation, it won't attempt to delete system memory.


EDIT: good practice to set back to NULL

[edited by - gleno on October 8, 2003 3:07:49 AM]
If you don''t delete it, the memory never gets returned to the heap. Thus, your program has a memory leak.

Technically, you never have to set it to NULL. Some people just do that for cleanliness/safe coding purposes.

The super safe way is:
if (text) {  delete text;  text = NULL;} 


Some people do:
if (text)  delete text; 


And apparently you can also do:
delete text; 

Since delete on a null is apparently no-op.
quote:Original post by nosajghoul
Sneftel : I fight like a cow on hell difficulty with 8 players going after a necro.

Actually, that''s my signature. Still, I''m glad to hear it. The world needs more badass cows.

How appropriate. You fight like a cow.
quote:Original post by Tibre
The super safe way is:
if (text) {  delete text;  text = NULL;}  



Nope, the if(text) call is completely unnecessary.
delete [] text; // (or delete text, depending if text is an array or not)text = NULL; 

is just fine.
Of course, one could ask why you aren''t using std::string, and std::stringstream, since you''re using c++
#include <string>#include <sstream>int GSstart(float num){   std::ostringstream textStream;     //use an ostringstream like cout   textStream << "num = " << num;   //use .str() to get a std::string out of a std::stringstream   std::string text = textStream.str();   //use .c_str() to get a const char* out of a std::string   MessageBox(0, "GSstart() entered", text.c_str(), 0); 	   pFnState = GScredits;	   return num + 1;}
If youre getting bored of function pointers allready try it using poymorphism.

like this.

class State;class StartState;class CreditsState;class CloseState;State *currentState = NULL;StartState *startState = NULL;CreditsState *creditsState = NULL;CloseState *closeState = NULL;class State{public:    virtual int run(float value) = 0;}class StartState : public State{public:    int run(float value)        {        //add stuff        currentState = creditsState;        }}class CreditsState : public State{public:    int run(float value)        {        //add stuff        currentState = closeState;        }}class CloseState : public State{public:    int run(float value)        {        //add stuff        }}startState = new StartState;creditsState = new CreditsState;closeState = new CloseState;currentState = startState;//do whatever else you do, calling the current state likecurrentState->run(num);


EDIT: some big mistakes, yes definitely convoluted.

[edited by - gleno on October 8, 2003 3:31:35 AM]

This topic is closed to new replies.

Advertisement