delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/08/10/05:35:14

X-Originating-IP: [208.160.246.197]
From: "Nimrod Abing" <n_abing AT hotmail DOT com>
To: djgpp AT delorie DOT com
Subject: Re: a bit about arrays...
Date: Thu, 10 Aug 2000 17:35:03 PHT
Mime-Version: 1.0
Message-ID: <F78JZ5sFmShG3ESvUC900000d14@hotmail.com>
X-OriginalArrivalTime: 10 Aug 2000 09:35:03.0875 (UTC) FILETIME=[430B4130:01C002AE]
Reply-To: djgpp AT delorie DOT com

>From: "Vermin" <ratspl AT hotmail DOT com>
>Reply-To: djgpp AT delorie DOT com
>To: djgpp AT delorie DOT com
>Subject: a bit about arrays...
>Date: Wed, 9 Aug 2000 22:00:00 +0200
>
>I've got two questions concerning arrays:
>
><1>
>What's the difference between
>
>char anArray[100];

If this is a global, storage for it is allocated on BSS (I think :-D).
If it is local, it is allocated on the stack.

>and
>
>char *anArray;
>anArray = new (char)[100];

Storage for this array (it's ``vector'' now) is allocated on the heap
or available free memory at run-time. You should free this memory
using `delete [] anArray'. Note the [] is _required_. Failure to free
dynamically allocated memory is the cause of memory leaks.

><2>
>How can I pass an array with undefined size to a procedure, and get the
>procedure to set the array size (C++)??
>
>something like this (in pseudo code):
>
>proc aProc(array[]){
>   array.size = 100;
>}
>
>int main(){
>   int anArray[];
>   aProc(anArray[]);
>   ...
>}
>
>Thanks!
>
>
If you're into C++, check out the STL template class ``vector'' which
frees you, the programmer, from the tedium of managing dynamically
memory.

#include <vector>


typedef vector<char> ch_vect;



void func(ch_vect &v)
{
  // Do your thing here with this vector.
  // You can change the size of v with v.resize(new_size)
}


int main(int argc, char *argv[])
{
  ch_vect array(1000); // Create an array of 1000 chars

  func(array);
}

Hope this helps.


----------------
_nimrod_a_abing_

------------------------------------------
Homepage: http://www.geocities.com/n_abing
________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com

- Raw text -


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