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         [email protected]
    Chapman Consulting -- San Jose, CA

------------------------------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to