| |

VerySource

 Forgot password?
 Register
Search
View: 1316|Reply: 10

How much stack can a function use in C ++

[Copy link]

3

Threads

10

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-1-21 17:20:01
| Show all posts |Read mode
Just
void foo ()
{
   char * p = new char [count]
}

So what is the maximum of this count? ? Is it 640k or 64k?
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-31 15:36:01
| Show all posts
This is the heap ...
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-31 17:46:05
| Show all posts
The size of the stack depends on the implementation.
Reply

Use magic Report

3

Threads

10

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-1-31 20:09:01
| Show all posts
Oh sorry the habit is wrong
should be
void foo ()
{
char p [count]
}
Reply

Use magic Report

0

Threads

15

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-2-1 01:27:01
| Show all posts
...

Impression is only 64K

In general, if the array does not exceed 0.1k, I will choose to use the stack
But if I have a slightly larger array, I would choose to use the heap,
In case recursion is used, the stack is probably not enough
Reply

Use magic Report

3

Threads

10

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-2-1 21:00:01
| Show all posts
err ....

Under dev-cpp
int main (void)
{
    char a [(1024 + 512 + 256 + 128 + 64 + 32 + 16 + 2) * 1024];
}
Not wrong

int main (void)
{
    char a [(1024 + 512 + 256 + 128 + 64 + 32 + 16 + 2 + 1) * 1024];
}
This way it's wrong
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-2-1 22:54:01
| Show all posts
Compiler related
Reply

Use magic Report

0

Threads

24

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-2-2 09:45:01
| Show all posts
char * p = new char [count]
-------------------------------------------------- ----------------------------
Your question should be changed to "how much heap a function can use".
Reply

Use magic Report

0

Threads

57

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-2-2 12:54:02
| Show all posts
Look at the options when you compile, you can choose the size of the stack space
At the same time, the stack of the same thread is used together, there is no concept of the maximum value that a function can use
It also depends on how much stack space is used before calling this function
Reply

Use magic Report

3

Threads

10

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-2-4 16:00:01
| Show all posts
int main (void)
{
char a [(1024 + 512 + 256 + 128 + 64 + 32 + 16 + 2 + 1) * 1024];
}

Well, such a sentence, the other parts occupy about 14k of memory, which is a bit scary.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list