5 Replies - 451 Views - Last Post: 09 February 2012 - 05:37 PM Rate Topic: -----

#1 Douggler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-August 11

Question About 'new' And Dynamic Objects

Posted 09 February 2012 - 12:26 PM

Hi All

I've been messing around with pointers, objects and vectors so I can get some practice and I'm making a little program that simulates the stock market. In it I have a "Share" class which simulates the behavior of a real share or share index.
I had a static function that push_back()ed new Share's into a static Share* vector, which was something like this:

void loadShares()
{
    shares.clear();
    shares.push_back(new Share(...));
    shares.push_back(new Share(...));
    shares.push_back(new Share(...));
    shares.push_back(new Share(...));



This worked fine for the first 'push_back()' but caused a memory access violation for the second or third similar statment. After a few days of researching what could have caused the error and the nature of the 'new' operator, I eventually tried making a static Share* function, called 'newShare(...)', which was something like this:
Share* Share::newShare(...)
{
    Share* returnShare = NULL;
    returnShare = new Share(...); //'passing on' the arguments into the constructor
    return returnShare;



I then used it like this:

...//in the loadShare() method
shares.push_back(newShare(...));


This function worked perfectly, and this must be because I'm initializing the poiner to NULL. What my question is though, is if a pointer is not NULL, and it holds some rubbish value and 'new' is called onto it, does the allocation of memory happen to the address it holds, while if it were NULL an unused block of memory is found first?

NOTE: sorry about the title, I know not to use things like "PLZ HELP" etc, but i literally saw the "question about" as I pressed enter and I couldn't stop my laggy internet connection from doing what I'd asked. :oops:

Is This A Good Question/Topic? 0
  • +

Replies To: Question About 'new' And Dynamic Objects

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Question About 'new' And Dynamic Objects

Posted 09 February 2012 - 12:37 PM

You're going to a great deal of trouble to create a memory leak. From what you have described, you shouldn't be using new at all.

You can push new Share objects into the vector by writing:
shares.push_back(Share( [args] ));

Was This Post Helpful? 1
  • +
  • -

#3 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 451
  • View blog
  • Posts: 855
  • Joined: 17-March 11

Re: Question About 'new' And Dynamic Objects

Posted 09 February 2012 - 12:37 PM

There is nothing essentially wrong with:

shares.clear();
shares.push_back(new Share(...));



shares.clear(); will leak memory though if shares contained any Share allocated the way you describe. Removing a pointer from the container does not free the memory.

Memory errors are the most tricky ones to track down and often manifest in a different place. Your error is probably somewhere else. Did you use a debugger to track the error. And if that fails a memory debugger such as valgrind can often be useful to track memory errors.

You are specifically looking for things like reading past the end of an array, double deletes/frees, doing something with pointers that are already freed, using uninitialized value.

This post has been edited by Karel-Lodewijk: 09 February 2012 - 12:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 The Adrian  Icon User is offline

  • New D.I.C Head

Reputation: 16
  • View blog
  • Posts: 47
  • Joined: 09-January 12

Re: Question About 'new' And Dynamic Objects

Posted 09 February 2012 - 12:42 PM

When you call new, it allocates memory and returns a pointer to that memory. So what you're doing is your taking a pointer, a null pointer or a garbage pointer, and you're assigning a new value to it. And the new value is the address to the memory that new just allocated for you

There is a difference between assigning a pointer to newly allocated memory and allocating memory on a pointer. You were assigning.

But glad it works!

And the two before me bring up a good point. If you don't need to allocate new memory, then don't. c/c++ doesn't manage memory for you
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Question About 'new' And Dynamic Objects

Posted 09 February 2012 - 12:49 PM

The trouble with using new is that no part of your program is responsible for freeing the memory that's being dynamically allocated each time you create a Share object that way.

If, instead, you simply use a call to the constructor as the argument to push_back, it creates a temporary Share object which is passed by value to the vector. The vector makes its own copy in memory that's managed by the vector itself, and then the temporary object is immediately and "automatically" destroyed by the compiler because it goes out of scope right after the call to push_back, so there's no memory leak.

But as Karel-Lodewijk said, the cause of your memory access violation is not apparent from the code that you posted.
Was This Post Helpful? 0
  • +
  • -

#6 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 691
  • Joined: 16-February 09

Re: Question About 'new' And Dynamic Objects

Posted 09 February 2012 - 05:37 PM

This would be a great opportunity for you to practice debugging.

I would start off by stepping through loadShares to make sure that the container is being filled properly. Then step through the code to see how you are accessing the container's elements. If you have any other functions that touch the container, step through that function, too.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1