Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problems allocating memory

Reply
Thread Tools

Problems allocating memory

 
 
Martijn van Buul
Guest
Posts: n/a
 
      03-09-2007
* Alf P. Steinbach:
> The std::vector code is more likely to be correct.


Care to elaborate on this hollow claim?

stl::vector is an array with all kinds of nasty strings attached - much
like the rest of this troublesome library. It's an array with an identity
crisis, it's an array with an attitude, but in the end it is still an array.
If you can't new that array yourself, then STL surely can't either. STL *will*
end up doing the new itself.

There's an enormous amount of uncalled for STL zealotry going on, with people
"suggesting" to use STL without even the *slightest* bit of validation to
their claims. The STL is *not* The One Answer. In fact, often, it's exactly
the WRONG answer.

--
Martijn van Buul - http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-09-2007
* Martijn van Buul:
> * Alf P. Steinbach:
>> The std::vector code is more likely to be correct.

>
> Care to elaborate


no.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-09-2007
Neclepsio wrote:
> John Harrison wrote:
>
>> std::vector does not prevent you from getting a raw pointer to the data.

>
>
> So, if new can't allocate the memory, why should std::vector be able to?
>
> Thank you,
> Ignazio


You are changing the subject, you said that you couldn't use std::vector
because you needed raw pointers. I pointed out that this wasn't true. I
didn't say std::vector would cure your problem.

john
 
Reply With Quote
 
Neclepsio
Guest
Posts: n/a
 
      03-09-2007
John Harrison wrote:
> You are changing the subject, you said that you couldn't use std::vector
> because you needed raw pointers. I pointed out that this wasn't true. I
> didn't say std::vector would cure your problem.


Ah, ok, I misunderstood your point. Thank you.
 
Reply With Quote
 
Neclepsio
Guest
Posts: n/a
 
      03-09-2007
Alf P. Steinbach wrote:
> The std::vector code is more likely to be correct.


This would not solve my problem: understand where I'm wrong.

Ignazio
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-09-2007
* Neclepsio:
> Alf P. Steinbach wrote:
>> The std::vector code is more likely to be correct.

>
> This would not solve my problem: understand where I'm wrong.


See Ivan Vecerina's reply earlier in the thread.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      03-09-2007
Neclepsio wrote:

> Alf P. Steinbach wrote:
>> The std::vector code is more likely to be correct.

>
> This would not solve my problem: understand where I'm wrong.


As long as we don't see the code that is giving you trouble, we won't be
able to help you solve _that_ problem. See the FAQ [5.8] for helpful hints
on how to post about code that does not work.


Best

Kai-Uwe Bux
 
Reply With Quote
 
bnonaj
Guest
Posts: n/a
 
      03-09-2007
Neclepsio wrote:
> Hi everyone.
> I've made a class Matrix, which contains a pointer to the data and some
> methods which all return a copy of the matrix modified in some way. The
> program works quite well for small matrices, but as matrix dimension
> grows up it crashes in deterministic locations depending on data,
> machine and OS.
> In particular, this happens when I test it with 3000x3000 matrices
> (which take up more than 30MB) under three different machines/OSes. The
> crash happens when much memory is still available (for example, under
> cygwin, more than 1GB of virtual memory available), and I cannot think
> there is no contiguous space for allocating the matrix.
> I always use references and never pointers to matrices, so Matrix
> objects are not dynamically allocated (the data, however, is allocated
> by the constructor). Could this be a problem with stack/heap limitations?
> Can someone help me with some idea on how to further investigate the
> problem?
>
> Thank you,
> Ignazio


Have you got exception handling so failed allocation will cause a throw?
Is the allocation and deallocation kept within each class? (reset
pointer to NULL and throw if access tried, and have a test)
Never keep more than one permanent pointer to the memory, (get the
pointer only within the function requiring it)
Try adding index checking via class operator[] overloading, and throw
exceptions if outside the range)
Always pass the classes that make up the matrix by reference, avoids a
copy constructor and subsequent destructor which may free memory
inappropriately
Preferably use an STL class like vector which handles memory well
already, (why reinvent the wheel)
Good luck.

JB

 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      03-10-2007
"Martijn van Buul" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
:* Alf P. Steinbach:
: > The std::vector code is more likely to be correct.
:
: Care to elaborate on this hollow claim?
:
: stl::vector is an array with all kinds of nasty strings attached -
much
: like the rest of this troublesome library. It's an array with an
identity
: crisis, it's an array with an attitude, but in the end it is still an
array.
: If you can't new that array yourself, then STL surely can't either.
: STL *will* end up doing the new itself.
:
: There's an enormous amount of uncalled for STL zealotry going on, with
people
: "suggesting" to use STL without even the *slightest* bit of validation
to
: their claims. The STL is *not* The One Answer. In fact, often, it's
exactly
: the WRONG answer.

One should start by using well-designed and well-tested libraries
before trying to reinvent the wheel. Especially when one is unable
to correctly manages memory.

This is just sound advice to first "get things working".
Where do you see zealotrly in there?

Until proven wrong ("show us the code..."), incorrect memory management
in the OP's code remains the most likely hypothesis.

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      03-10-2007
"Neclepsio" <(E-Mail Removed)> wrote in message
news:45f19f53$(E-Mail Removed)...
: Thank you for replying.
:
: Ivan Vecerina wrote:
: > The first thing to check is that you have provided correct
: > implementations of the destructor, the copy-constructor, and the
: > copy-assignment operators.
:
: Everything is ok.
How do we know?
[ your final comment does nothing to make me trust a bare claim ]

: The second?
The second is: another source of Undefined Behavior.
Third: you are actually allocating more memory than is available.

: > But to make your life easier in the first place, you should consider
: > using std::vector instead of a raw pointer to store the matrix data.
:
: I needed raw pointers to use the BLAS library.

Sure: &vec.front() or &vec[i]


And of course, there are C++ equivalents to BLAS, as others pointed out.

So there are several options to solve the problem.
But if you want a diagnosis, please post the source code.


Kind regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Allocating vector of strings seem to crash. Allocating array ofstrings seems to be ok . Rakesh Kumar C++ 5 12-21-2007 10:42 AM
segmenatation fault while allocating memory Sameer C++ 2 11-03-2003 09:24 AM
allocating memory for std::string? Axel C++ 1 10-27-2003 12:35 PM
basic allocating memory question soni29 C++ 6 09-05-2003 05:45 PM
Pushing memory allocating objects into a vector. hall C++ 4 08-20-2003 09:24 PM



Advertisments