delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1998/11/25/05:35:59

From: benny AT crocodial DOT de (Benjamin Riefenstahl)
Subject: Re:
25 Nov 1998 05:35:59 -0800 :
Message-ID: <365B2255.53AA7E02.cygnus.gnu-win32@crocodial.de>
References: <199811232132 DOT WAA00238 AT dedalus DOT com>
Mime-Version: 1.0
To: gnu-win32 AT cygnus DOT com

Hi Ugo,


Ugo Matrangolo wrote:
> This program causes a segment violation :
> ...
>    vector<vector<int> > mymatrix(n);
> 
>    for (int i = 0;i < n;i++)
>       for (int j = 0;j < n;j++)
>          mymatrix[i][j] = 0;
> ...
> Can anyone tell me if it is a bug or my fault ?

Your fault ;-)

The declaration allocates a vector of n *empty* vector<int>s and those
inner vectors are never expanded to make room for any elements (vectors
do not expand automatically). Try this one instead:

    vector<vector<int> > mymatrix(n);

    for (int i = 0;i < n;i++)
       for (int j = 0;j < n;j++)
          mymatrix[i].push_back(0); // expand the vector<int> by one


so long, benny  
======================================
Benjamin Riefenstahl (benny AT crocodial DOT de)
Crocodial Communications EntwicklungsGmbH
Ruhrstr. 61, D-22761 Hamburg, Germany
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019