|
From: sachin g. <g_s...@ya...> - 2011-11-18 16:32:48
|
Hi All,
I have a simple program
/*************************/
#include <iostream>
#include <string>
main()
{
std::string test;
test ="abc";
std::string *test2 = new std::string("abc");
delete test2;
}
/******************************************/
Now I am running valgrind over it
valgrind --tool=massif ./a.out
on ms_print
it is howing output as
/****************************************
n time(i) total(B) useful-heap(B) extra-heap(B) stacks(B)
--------------------------------------------------------------------------------
0 0 0 0 0 0
1 1,385,421 40 28 12 0
2 1,388,597 64 36 28 0
3 1,389,777 104 64 40 0
4 1,393,400 104 64 40 0
61.54% (64B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->53.85% (56B) 0x3D1709B85F: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.8)
| ->26.92% (28B) 0x3D1709D10F: std::string::_M_mutate(unsigned long, unsigned long, unsigned long) (in /usr/lib64/libstdc++.so.6.0.8)
| | ->26.92% (28B) 0x3D1709D28A: std::string::_M_replace_safe(unsigned long, unsigned long, char const*, unsigned long) (in /usr/lib64/libstdc++.so.6.0.8)
| | ->26.92% (28B) 0x4009B2: main (test1.c:6)
| |
| ->26.92% (28B) 0x3D1709C363: ??? (in /usr/lib64/libstdc++.so.6.0.8)
| ->26.92% (28B) 0x3D1709C510: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.8)
| ->26.92% (28B) 0x4009E3: main (test1.c:8)
|
->07.69% (8B) 0x4009CD: main (test1.c:8)
/*********************
Why valgrind is showing stack variables in heap. Any idea.
Regards
Sachn
|
|
From: Eric S. <mas...@gm...> - 2011-11-18 17:31:59
|
Hi
To the best of my knowledge:
std::string is an object.
When created it fetches some memory from the heap like vector, list or
whatever does via the standard allocator - if the allocator has not been
replaced.
So it can store some chars w/o immediately reallocating.
Well,
std::string test;
itself should reside on the stack, thus the administrative data.
However, the payload of string is stored on the heap.
Hope I am right and this helps.
Cheers
Eric
Am 18.11.2011 17:32, schrieb sachin gupta:
> Hi All,
>
> I have a simple program
>
> /*************************/
> #include <iostream>
> #include <string>
> main()
> {
> std::string test;
> test ="abc";
>
> std::string *test2 = new std::string("abc");
> delete test2;
> }
> /******************************************/
>
> Now I am running valgrind over it
>
> valgrind --tool=massif ./a.out
>
> on ms_print
>
> it is howing output as
> /****************************************
>
> n time(i) total(B) useful-heap(B) extra-heap(B)
> stacks(B)
> --------------------------------------------------------------------------------
> 0 0 0 0
> 0 0
> 1 1,385,421 40 28
> 12 0
> 2 1,388,597 64 36
> 28 0
> 3 1,389,777 104 64
> 40 0
> 4 1,393,400 104 64
> 40 0
> 61.54% (64B) (heap allocation functions) malloc/new/new[],
> --alloc-fns, etc.
> ->53.85% (56B) 0x3D1709B85F: std::string::_Rep::_S_create(unsigned
> long, unsigned long, std::allocator<char> const&) (in
> /usr/lib64/libstdc++.so.6.0.8)
> | ->26.92% (28B) 0x3D1709D10F: std::string::_M_mutate(unsigned long,
> unsigned long, unsigned long) (in /usr/lib64/libstdc++.so.6.0.8)
> | | ->26.92% (28B) 0x3D1709D28A: std::string::_M_replace_safe(unsigned
> long, unsigned long, char const*, unsigned long) (in
> /usr/lib64/libstdc++.so.6.0.8)
> | | ->26.92% (28B) 0x4009B2: main (test1.c:6)
> | |
> | ->26.92% (28B) 0x3D1709C363: ??? (in /usr/lib64/libstdc++.so.6.0.8)
> | ->26.92% (28B) 0x3D1709C510: std::basic_string<char,
> std::char_traits<char>, std::allocator<char> >::basic_string(char
> const*, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.8)
> | ->26.92% (28B) 0x4009E3: main (test1.c:8)
> |
> ->07.69% (8B) 0x4009CD: main (test1.c:8)
> /*********************
>
> Why valgrind is showing stack variables in heap. Any idea.
>
> Regards
> Sachn
>
>
> ------------------------------------------------------------------------------
> All the data continuously generated in your IT infrastructure
> contains a definitive record of customers, application performance,
> security threats, fraudulent activity, and more. Splunk takes this
> data and makes sense of it. IT sense. And common sense.
> http://p.sf.net/sfu/splunk-novd2d
>
>
> _______________________________________________
> Valgrind-users mailing list
> Val...@li...
> https://lists.sourceforge.net/lists/listinfo/valgrind-users
|
|
From: David C. <dcc...@ac...> - 2011-11-18 17:59:38
|
On 11/18/2011 8:32 AM, sachin gupta wrote:
> Hi All,
>
> I have a simple program
>
> /*************************/
> #include <iostream>
> #include <string>
> main()
> {
> std::string test;
> test ="abc";
>
> std::string *test2 = new std::string("abc");
> delete test2;
> }
> /******************************************/
>
> Now I am running valgrind over it
>
> valgrind --tool=massif ./a.out
>
> on ms_print
>
> it is howing output as
> /****************************************
>
> n time(i) total(B) useful-heap(B) extra-heap(B)
> stacks(B)
> --------------------------------------------------------------------------------
> 0 0 0 0
> 0 0
> 1 1,385,421 40 28
> 12 0
> 2 1,388,597 64 36
> 28 0
> 3 1,389,777 104 64
> 40 0
> 4 1,393,400 104 64
> 40 0
> 61.54% (64B) (heap allocation functions) malloc/new/new[],
> --alloc-fns, etc.
> ->53.85% (56B) 0x3D1709B85F: std::string::_Rep::_S_create(unsigned
> long, unsigned long, std::allocator<char> const&) (in
> /usr/lib64/libstdc++.so.6.0.8)
> | ->26.92% (28B) 0x3D1709D10F: std::string::_M_mutate(unsigned long,
> unsigned long, unsigned long) (in /usr/lib64/libstdc++.so.6.0.8)
> | | ->26.92% (28B) 0x3D1709D28A: std::string::_M_replace_safe(unsigned
> long, unsigned long, char const*, unsigned long) (in
> /usr/lib64/libstdc++.so.6.0.8)
> | | ->26.92% (28B) 0x4009B2: main (test1.c:6)
> | |
> | ->26.92% (28B) 0x3D1709C363: ??? (in /usr/lib64/libstdc++.so.6.0.8)
> | ->26.92% (28B) 0x3D1709C510: std::basic_string<char,
> std::char_traits<char>, std::allocator<char> >::basic_string(char
> const*, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.8)
> | ->26.92% (28B) 0x4009E3: main (test1.c:8)
> |
> ->07.69% (8B) 0x4009CD: main (test1.c:8)
> /*********************
>
> Why valgrind is showing stack variables in heap. Any idea.
>
>
std::string is a std::vector<char>. A vector is dynamically sized (data
can be added or removed ay any time), so it must allocate space from the
heap to store the data inside. There is no special optimization for a
stack variable.
All of this applies to every container object, so practically any
program that uses C++ libraries will allocate space from the heap.
--
David Chapman dcc...@ac...
Chapman Consulting -- San Jose, CA
|
|
From: sachin g. <g_s...@ya...> - 2011-11-19 04:02:07
|
Thanks David, Eric. Yes it helped.
________________________________
From: David Chapman <dcc...@ac...>
To: sachin gupta <g_s...@ya...>
Cc: "val...@li..." <val...@li...>
Sent: Friday, November 18, 2011 10:31 PM
Subject: Re: [Valgrind-users] [massif]>> string on stack is shown on heap
On 11/18/2011 8:32 AM, sachin gupta wrote:
Hi All,
>
>
>I have a simple program
>
>
>/*************************/
>
>#include <iostream>
>#include <string>
>main()
>{
>std::string test;
>test ="abc";
>
>std::string *test2 = new std::string("abc");
>delete test2;
>}
>/******************************************/
>
>
>Now I am running valgrind over it
>
>
>valgrind --tool=massif ./a.out
>
>
>
>on ms_print
>
>
>it is howing output as
>/****************************************
>
>
> n time(i) total(B) useful-heap(B) extra-heap(B) stacks(B)
>--------------------------------------------------------------------------------
> 0 0 0 0 0 0
> 1 1,385,421 40 28 12 0
> 2 1,388,597 64 36 28 0
> 3 1,389,777 104 64 40 0
> 4 1,393,400 104 64 40 0
>61.54% (64B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
>->53.85% (56B) 0x3D1709B85F: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.8)
>| ->26.92% (28B) 0x3D1709D10F: std::string::_M_mutate(unsigned long, unsigned long, unsigned long) (in /usr/lib64/libstdc++.so.6.0.8)
>| | ->26.92% (28B) 0x3D1709D28A: std::string::_M_replace_safe(unsigned long, unsigned long, char const*, unsigned long) (in /usr/lib64/libstdc++.so.6.0.8)
>| | ->26.92% (28B) 0x4009B2: main (test1.c:6)
>| |
>| ->26.92% (28B) 0x3D1709C363: ??? (in /usr/lib64/libstdc++.so.6.0.8)
>| ->26.92% (28B) 0x3D1709C510: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.8)
>| ->26.92% (28B) 0x4009E3: main (test1.c:8)
>|
>->07.69% (8B) 0x4009CD: main (test1.c:8)
>
>/*********************
>
>
>Why valgrind is showing stack variables in heap. Any idea.
>
>
>
std::string is a std::vector<char>. A vector is dynamically sized (data can be added or removed ay any time), so it must allocate space from the heap to store the data inside. There is no special optimization for a stack variable.
All of this applies to every container object, so practically any program that uses C++ libraries will allocate space from the heap.
--
David Chapman dcc...@ac... Chapman Consulting -- San Jose, CA |