Re: Max size of data in C++ prog? Another comment
On Sun, Dec 19, 2004 at 11:31:13AM -0500, Marc Shapiro wrote:
> On Sun, 19 Dec 2004 12:32:12 +1100 Sam Watkins <swatkins@fastmail.fm> wrote:
> >
> >there are two different data segments for a C or C++ program,
> >the stack and the heap.
> >
> >stack space is (apparently) limited under Linux.
> >If you declare an array like this in a function:
> >
> > char c[10*1024*1024];
> >
> >and write to it all, this will cause a segfault on Linux.
>
> That was my problem.
>
> >The heap can get much bigger, you can malloc as much as you want on the
> >heap
> >(up to the limits of VM and process address space) and it won't segfault,
> >e.g.
> >
> > char *c = malloc(100*1024*1024);
> > int i;
> > for (i=0; i<size; ++i)
> > c[i] = 'x';
> > free(c);
>
> Thanks, allocating space with malloc worked great. I get my character
> array with 68000000 bytes (2000 x 34000) with no problems.
>
> >
> >you need to:
> >
> >#include <malloc.h>
> >
> >to use malloc.
>
> malloc.h must have been included by one of my other include files, as I
> did not include it directly.
>
> >C++ allocates data on the heap when you use "new", and the STL containers
> >allocate their elements on the heap. For example these are ok:
> >
> > char *c = new char[100*1024*1024];
> > // ...
> > delete[] c;
> >
> > vector<int> v;
> > int i;
> > for (i=0; i<10000000; ++i)
> > v.push_back(i);
>
> Vectors are single dimensional, I believe, so they wont work in this
> case, but I will remember them for the future
>
When one defines an STL vector with, for instance:
vector< int > v;
inside a routine, it has the appearance of being allocated on the stack.
This is true, but not really true. A small control block is on the stack,
but the data 'contained' in the vector is actually on the heap. C++
takes care of deallocating it when it runs its destructor on v. So C++
is more economical in use of stack space than C, and still safe against
mistakes that can happen when using globally allocated store.
--
Paul E Condon
pecondon@mesanetworks.net
Reply to: