# A little question on pointers

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

## Recommended Posts

Hi, if i create a new object like this:
for(i = 0; i< 10; ++i)
{
ServerSystem *server = new ServerSystem();
}

why this isn't an error?

##### Share on other sites
Quote:
 Original post by Tano_ITAwhy this isn't an error?

Because it's not? That's perfectly valid code.

##### Share on other sites
It is an error in that you aren't going to be able to delete any of those pointers you created. It's not a syntax error though so it will still build. Depending on what ServerSystem does it will probably run also.

##### Share on other sites
Quote:
 Original post by Tano_ITAHi,if i create a new object like this:for(i = 0; i< 10; ++i){ ServerSystem *server = new ServerSystem();}why this isn't an error?

What kind of error were you expecting?

you will get a memory leak at run time, but it is a perfectly valid piece of code.

##### Share on other sites
Ok, thanks for the replyes. but if i want to access a particolary server object? For example, i want to delete 2 of 10 objects. Can i do that?

Thanks a lot. I know that's a stupid question, but i'm curious.. And sorry for my bad english! :)

##### Share on other sites
Quote:
 Original post by neomechyou will get a memory leak at run time, but it is a perfectly valid piece of code.
Not necessarily. The object's constructor may well be doing some voodoo magic to destroy itself when some event happens (such as the application shutting down).

##### Share on other sites
Quote:
 Original post by Tano_ITAHi,if i create a new object like this:for(i = 0; i< 10; ++i){ ServerSystem *server = new ServerSystem();}why this isn't an error?

This is stuff for what a compiler would yield a warning message. Warning (as opposed to error), because (as many people already pointed out) it is valid code, but still you are (probably) doing something wrong here. But as far as I know, no current compiler would print a warning for that (but e.g. gcc prints warning for every call to gets(), which is a valid function, but dangerous).

edit:typo

##### Share on other sites
Quote:
 Original post by Tano_ITAOk, thanks for the replyes. but if i want to access a particolary server object? For example, i want to delete 2 of 10 objects. Can i do that?Thanks a lot. I know that's a stupid question, but i'm curious.. And sorry for my bad english! :)

And for this question?

##### Share on other sites
Quote:
 Original post by monkey8751It is an error in that you aren't going to be able to delete any of those pointers you created.

C++ is built on premise that "user knows better". In this case, user wants to allocate several objects.

Valid, and sane, yet somewhat advanced example (see boost's asio TCP connection examples):
class ServerSystem {  ServerSystem() {    registerEvent(this, onReceive, &ServerSystem::onReceive);    registerEvent(this, onShutdown, &ServerSystem::onShutdown);  }  void onShutdown() {    delete this;  }};

onShutdown gets called by some external handler.

While purists will start killing kittens when seeing such code, there is nothing wrong with it, and sometimes makes perfect sense.

This is also idiomatic approach for memory managed languages. There, references are tracked implicitly. So even if user loses a reference to an object, memory manager still has it. But the by far most important point: someone, somewhere, somehow needs to track what a certain byte of memory represents. It could even be DRAM itself. Or memory manager. Or meta-bits in memory. Or someone. In above example, event manager (in asio's example it's the IOCP) keeps track of this reference.

General advice for C++ is to de-allocate in same context as you allocate. But this is not even remotely required, and sometimes not even desired. Code such as this:
void concatStrings(string * s1, string * s2) {  return new string(s1, s2);}
is undesirable due to design/implementation coupling. Since C++ makes no distinction of how pointee was allocated, such function will implicitly need to use consistent allocation scheme.

All that said, general advice still holds. Manage what you allocate. Almost without exception, it makes code cleaner and simpler to follow. But if using C++, look into other options. After all, manual memory management is the defining trait of C++.

Quote:
 And for this question?

You somehow need to find them. Unless you remember where you put something, you will never find it again.

And don't do that. Just because of above options, this isn't a legit example of such practices. Allocate, remember, de-allocate. It will be a long time before you find a legit example where it may be desirable to forget what you allocated.

##### Share on other sites
You probably want an array (or std::vector, or some usage of smart pointers) of pointers.
ServerSystem *server[10];for(int i = 0; i < 10; ++i){    // chance for memory leak if an exception occurs here    server = new ServerSystem();}delete server[2];server[2] = NULL;int x = rand() % 10;if(server){  server->do_something();}// chance for memory leak if an exception occurs hereserver[2] = new ServerSystem();for(int i = 0; i < 10; ++i){    delete server;}

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632915
• Total Posts
3009197
• ### Who's Online (See full list)

There are no registered users currently online

×