| |

VerySource

 Forgot password?
 Register
Search
View: 828|Reply: 7

Ask: dynamic allocation of objects

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-13 11:40:01
| Show all posts |Read mode
1. Does the vector store data in stack or heep? If it is in heap, will the operation speed be slower than ordinary arrays when there are many elements?
2. There is a class with a private array variable, whose value is initialized by the constructor. If a function statically creates an object of the class, the array variable should be stored in the stack. If dynamic allocation new is used, is the array variable stored in the heap?
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-1-17 16:18:01
| Show all posts
In the 1 heap, it depends on what operation you want to do. I think the speed is the same.
2 don't quite understand what you mean
class test
{
char sz [100];
}; If it's such a class
Then in voidfunc ()
{
test t; the object is in the stack, and the member variables are also in the stack
}

If test * p = new test then all variables of the object in the heap are also in the heap
Reply

Use magic Report

0

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-1-19 10:45:01
| Show all posts
agree to the upstairs.
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-19 11:27:01
| Show all posts
Thank you! Because I saw some articles suggesting that the speed of running in the heap is relatively slow, the storage speed seems to be slower after changing the code written to dynamic allocation today because the amount of data is very large (about 2 million long-element two-dimensional arrays) ) So I hope to optimize the stored code, what is your opinion?
Reply

Use magic Report

0

Threads

5

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-20 14:36:02
| Show all posts
A vector is just a wrapper for a template type array. "Is the space where the vector stores data is stack or deep?" I don't know what it means. It's like asking whether the stack is an array or a linked list. They are a logical layer and a physical layer . . Vector can be stored randomly. It can be understood as either heap or stack. Only the operation is different, stack is push_back, pop_back, heap is push_back, pop_front /
STL is more efficient than arrays because we do not have to be responsible for memory management, such as application and release, and there are probably applications for memory pools. Big data certainly recommends STL.
Reply

Use magic Report

0

Threads

24

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-25 19:00:02
| Show all posts
Is the space where the vector stores data stack or heep?
-------------------------------------------------- --------------------------------
heap
Reply

Use magic Report

0

Threads

24

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-25 19:36:01
| Show all posts
2. There is a class with a private array variable, whose value is initialized by the constructor. If a function statically creates an object of the class, the array variable should be stored in the stack. If dynamic allocation new is used, is the array variable stored in the heap?
-------------------------------------------------- -----------------------------------
If it is dynamically allocated, your entire object is on the heap, so the embedded array is naturally on the heap.
Reply

Use magic Report

0

Threads

6

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-13 20:45:02
| Show all posts
heap
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