Visual Studio very strange "hello world" error

Pages: 12
To lastchance: mm get it, for a "variable lenght array" i use lists.. ..?

PS forgive my beginnerness! :D
LakySimi1 wrote:
for a "variable length array" i use lists.. ..?

If you need to access the ... 10042nd ... element of a list are you prepared to follow the pointer path through the preceding 10041 elements?

A vector stores its data in contiguous memory, so you can go immediately to the element that you want.

But all the STL containers (vector, list, set, map, stack, queue, ... ) have their particular uses, so you can choose the one that you want for a particular task. They have also been set up to integrate with the routines in <algorithm>.

If I am absolutely sure that my array is only ever going to be of one fixed size then I will use a C-style array. Likewise, if I'm doing mixed-language programming. But the STL containers do often make tasks easier.



PS forgive my beginnerness! :D

We're all learners.
Last edited on
are not arrays static by "standard"


static creates the array on the heap - not the stack. By default arrays are created on the stack.

https://stackoverflow.com/questions/5836309/stack-memory-vs-heap-memory
static creates the array on the heap


Not convinced. I thought "static" was a separate area of memory from both stack and heap? For variables that persist for the lifetime of the program. The heap is dynamic.
To lastchance: Mm yes, in fact i've never encoutered a situation where i need 10042th element of a list BUT if it happens it's just a pointless decrease of efficiency.. get it.. i'll do my homework and check out vectors and others, thank you!

(The story is that when i decided to study coding i chosed to stop learning at the moment that my "tools" are just enough for my game projects, i really liked the idea of doing thing almost from scratch.. maybe somone else feels like me?)

PS i will mark thread as solved when there will be no more new posts
Last edited on
i chosed to stop learning at the moment that my "tools" are just enough for my game projects

So you use a hammer because that is the only tool you have, everything you encounter you see as a nail.

Learning all you can about what C++ offers would make writing code MUCH easier. And efficient.

And give you more tools to solve problems.
1
2
3
4
5
6
7
8
9
10
11
12
13
include <iostream>

using namespace std;

int TEST1[1'000];

int main()
{
	int TEST2[1'000];
	static int TEST3[1'000];

	cout << TEST1 << "  " << TEST2 << "  " << TEST3 << '\n';
} 


Which on my computer displays:


000000013F278A20  000000000023EE40  000000013F279BC0


TEST1 and TEST3 are located on the heap, TEST2 is located on the stack.
> static creates the array on the heap - not the stack

For the C++ standard, storage duration is an abstract concept: it only deals with the lifetime of an object, and the duration for which the storage for an object would be available. For instance, a hypothetical conforming implementation could provide storage for objects with a static storage duration on the implementation's heap.

The total amount of storage required for all the objects with static storage duration in a program can be determined at build time; in almost all implementations, this storage is in one or more data sections.
> TEST1 and TEST3 are located on the heap, TEST2 is located on the stack.

Not necessarily. All that the output tells us is that TEST1 and TEST3 are located close to each other; and that they are quite far away from TEST2.
To Furry Guy: mm no, at the time i was quite a bit obsessed with the concept of "from scratch", expecially in informatics which to me is quite ethereal and still made of sand and copper, so i liked the idea of hammer a nail not with an hammer but with a branch that i detach from a short tree, and as soon as possible make myself a improvised hammer.. i was interested in "prime tools" and building other tools by myself.
Last edited on
@seeplus, in your code both TEST1 and TEST3 have static storage. TEST1 is a global variable whilst TEST3 has been deliberately put in the static storage. If you create another array TEST4 with new (so it is definitely on the heap) then its memory address is miles away.
Last edited on
Hmm...
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

int TEST1[1'000'000]{};

int main()
{
	//int TEST2[1'000'000];			// Fails
	static int TEST3[1'000'000]{};
	auto TEST4{ new int[1'000'000]{} };
}


Only trying to create TEST2 (on the stack?) fails. The other definitions all succeed. I suppose all we can really say is that static doesn't create on the stack and that using it doesn't have the same size limitations as creating on the stack.

And that this sort of thing is likely to be compiler specific...

TEST1 (as a global variable known at compile time) and TEST3 (by mandate) are in static memory.
TEST2 (if it had succeeded) would be on the stack.
TEST4 (as it is dynamic) is on the heap.

The implementation is free to locate the memory where it likes, but, nevertheless,
static - stack - heap
are distinct areas of memory.
Topic archived. No new replies allowed.
Pages: 12