memory allocation for array

How do I make a million ints? I can't do it with subscripted arrays. I can with dynamic memory allocation and then pointers. Is there another (easier) way that I am not seeing?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include<iostream>


int main()
{
                //ok
		int * A = new int[1000000];
		std::cout<<sizeof(A); // size of pointer  to first array element

                //errors with 1000000, not with 100000
		int array[1000000];//100000 fine, 1000000: run time error
		std::cout<<"Array Size :"<<sizeof(array);
		int length = (sizeof(array)/sizeof(array[0]));
		std::cout<<std::endl;
		std::cout<<"Length :"<<length<<std::endl;

		delete []A;

	return 0;
}
Last edited on
1
2
3
4
std::vector<int> large(1000000);
std::cout << large.size() << std::endl;
large[999999] = 42;
std::cout << large[999999] << std::endl;
Last edited on
A million int values will occupy 4 or 8 million bytes.

Typical stack size is less than this (on windows, I recall typical stack size is 1MB). So to use that much memory, it's going to have to be on the heap.

Doing it yourself is error prone. Just use a vector.

vector<int> A(1000000);
Yes, the default stack size on Windows is 1 MiB. Executables can be built to request larger stacks from the OS, but this is meant to accommodate complex programs with deep call stacks. It's not meant just to allocate large data structures on the stack. Large allocations should go on the heap.
Hi,

As the others have said, the best thing to do is use an STL container, it puts it's data on the heap, does it's own memory management, knows it's own size etcetera. The STL is designed to be easy and safe to use. One can achieve a lot by taking advantage of all it's facilities.

Avoid using new and delete, they are for low level memory management, even then smart pointers can often be used. The other problem with new is that if an exception happens, delete is never reached resulting in a memory leak.

Some other things to avoid: Raw pointers and arrays. If you want to play with those, then consider writing C programs. Although some have a go at writing their own C++ containers, which might use those things. But you have to be careful, part of the reason why the STL is there, is to protect coders from all these details and problems.

Your technique of using sizeof is a C methodology, you shouldn't need it for C++. Realise that C and C++ are quite different paradigms.

I know your background is in assembler, so it's reasonably easy to transition to C, I am just saying try to avoid bringing low level techniques into C++, unless you are doing something like your own container.
Hi, I wanted to follow up on this thread. Thx to everyone.

I used vector container without a problem, and am able to pass it to a function (below). My question now is that when I pass the vector container/array to a function, is a copy of the array made or do I use & operator to avoid copying?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<vector>

void makeArray(std::vector<int>array)
{
			int length = array.size();
			std::cout<<"Array Size :"<<length<<std::endl;
			for(int i(0); i<1000000; ++i){array[i] = i + 2;}
			for(int i(0); i<1000000; ++i){std::cout<<array[i]<<std::endl;}
}


int main()
{
		std::vector<int> array(1000000);
		makeArray(array);

	return 0;
}
Last edited on
My question now is that when I pass the vector container/array to a function, is a copy of the array made implicitly or should I use the & operator to avoid copying?

Yes since you passed the vector by value into the function a copy is being made. IMO, yes you should pass a vector into functions by reference when you wish to change the contents of the vector, and by const reference when you don't want to change the values of the vector.

ok, thx
1
2
3
4
void makeArray(std::vector<int>&array)
{
 //do stuff
}
Hi,

Note that the pass by reference should be used for any of the STL containers, or any of your own classes - in other words anything that might potentially be big :+) Also, when returning a container from a function, one doesn't have to do this by reference, C++ has copy elision (NRVO and RVO) and move semantics. And one can't return a local reference - it goes out of scope when the function ends.

Use const wherever you can get away with it - it's a big advantage in making more robust code.
Topic archived. No new replies allowed.