[Insight-users] use of std::vector with ITK typedefs

Arvind Rao arvind.sbia at gmail.com
Thu Apr 5 00:34:07 EDT 2012


Hi Matt,

Thanks for replying. *ReadStreamFile(...)* is defined below.
The "bad_alloc> caught: St9bad_alloc" error seems to originate from this
function. For clarity, *TPixelType = itk::Vector<float, 3*>

template< typename TPixelType>
void *ReadStreamFile*( std::string filename, std::vector<TPixelType>
&points )
{
   TPixelType dir;
   int num;
   std::vector<float> vec;
   std::ifstream indata( filename.c_str() ); // opens the file

   indata >> num;
   while ( !indata.eof() )  { // keep reading until end-of-file
      vec.push_back( num );
      indata >> num;}
   indata.close();
}

Best,
Arvind

On Wed, Apr 4, 2012 at 10:53 PM, Matt McCormick
<matt.mccormick at kitware.com>wrote:

> Hi Arvind,
>
> It looks like the error is happening inside ReadStreamFile, so please
> post how that is declared and defined.
>
> Thanks,
> Matt
>
> On Wed, Apr 4, 2012 at 8:57 PM, Arvind Rao <arvind.sbia at gmail.com> wrote:
> > Hi all,
> >
> > In order to read-in a variable number of points from a file I want to
> use a
> > dynamically sized container like STD::VECTOR. The points are stored like
> so:
> > ( points is the vector I use )
> >
> >       typedef itk::Image< itk::Vector<ComponentType, Dimension>,
> Dimension>
> >                                 PeakImageType;
> >       typedef PeakImageType::PixelType
> >                                                        DirectionType;
> >       std::vector<DirectionType>
> >                                                                points
> >
> > However, the following try-catch statement gives, at run-time, "bad_alloc
> > caught: St9bad_alloc"
> >
> >       try
> >       {
> >                 std::vector<DirectionType> points;
> >                 ReadStreamFile<DirectionType>( streamArg.getValue(),
> points
> > );
> >       }
> >      catch (std::bad_alloc& ba)
> >       {
> >                 std::cerr << "bad_alloc caught: " << ba.what() <<
> std::endl;
> >                 return 0;
> >       }
> >
> > So, what is going on? Its seems like POINTS is not being allocated
> properly,
> > but I'm sure how to fix this.
> >
> > Thanks for any help you can provide.
> >
> >
> > --
> > Arvind S. Rao, PhD
> > Section of Biomedical Image Analysis
> > Department of Radiology
> > University of Pennsylvania
> > 3600 Market Street, Suite 380
> > Philadelphia, PA 19104
> >
> >
> >
> > _____________________________________
> > Powered by www.kitware.com
> >
> > Visit other Kitware open-source projects at
> > http://www.kitware.com/opensource/opensource.html
> >
> > Kitware offers ITK Training Courses, for more information visit:
> > http://www.kitware.com/products/protraining.php
> >
> > Please keep messages on-topic and check the ITK FAQ at:
> > http://www.itk.org/Wiki/ITK_FAQ
> >
> > Follow this link to subscribe/unsubscribe:
> > http://www.itk.org/mailman/listinfo/insight-users
> >
>



-- 
*Arvind S. Rao, PhD*
Section of Biomedical Image Analysis
Department of Radiology
University of Pennsylvania
3600 Market Street, Suite 380
Philadelphia, PA 19104
tel: (267) 283-8959
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20120405/b968c8c5/attachment.htm>


More information about the Insight-users mailing list