Line data Source code
1 : // This file (ptr_address.cc) was created by Ron Rechenmacher <ron@fnal.gov> on
2 : // Feb 28, 2019. "TERMS AND CONDITIONS" governing this file are in the README
3 : // or COPYING file. If you do not have such a file, one can be obtained by
4 : // contacting Ron or Fermi Lab in Batavia IL, 60510, phone: 630-840-3000.
5 : // $RCSfile: .emacs.gnu,v $
6 : // rev="$Revision: 1784 $$Date: 2026-10-09 13:51:46 -0500 (Fri, 09 Oct 2026) $";
7 : #if 0
8 : rm -f /tmp/trace_buffer_`whoami`
9 :
10 : /home/ron/work/tracePrj/trace
11 : ron@mu2edaq01 :^) treset; ptr_address_string
12 : 02-28 16:42:54.035092 ptr_address_string nfo TRACE_NAME is ptr_address_string
13 : 02-28 16:42:54.035184 ptr_address_string nfo the real message is hello
14 : 02-28 16:42:54.035190 ptr_address_string nfo the copy message is hello
15 : 02-28 16:42:54.035197 ptr_address_string nfo the address of message string - (void*)real_msg=0x40872e
16 : 02-28 16:42:54.035202 ptr_address_string nfo ip=0xc4ac20
17 : 02-28 16:42:54.035207 ptr_address_string nfo ullp=0xc4acb0 *ullp=555
18 : 02-28 16:42:54.035211 ptr_address_string nfo std::string("hello")=hello
19 : 02-28 16:42:54.035215 ptr_address_string nfo unique_ptr up_string=0xc4acd0 which is ptr to std::string (not the actual string within)
20 : 02-28 16:42:54.035219 ptr_address_string nfo unique_ptr *up_string=new string
21 : 02-28 16:42:54.035223 ptr_address_string nfo unique_ptr (*up_string).c_str()=new string
22 : 02-28 16:42:54.035227 ptr_address_string nfo unique_ptr (void*)((*up_string).c_str())=0xc4ace0
23 : 02-28 16:42:54.035230 ptr_address_string nfo unique_ptr &(*up_string)[0]=new string
24 : 02-28 16:42:54.035234 ptr_address_string nfo unique_ptr (void*)&(*up_string)[0]=0xc4ace0
25 : 02-28 16:42:54.035244 ptr_address_string nfo unique_ptr &(*up_vector_int)[0]=0xc4ad20
26 : 02-28 16:42:54.035249 ptr_address_string nfo unique_ptr (*up_vector_int)[0]=1
27 : --2019-02-28_16:42:54--
28 : #endif
29 :
30 : #include "TRACE/trace.h" // TRACE
31 :
32 : #include <stdio.h> // printf
33 : #include <stdlib.h> // malloc
34 : #include <string.h> // rindex
35 : #include <memory> // std::unique_ptr
36 : #include <vector> // std::vector
37 :
38 : #if __cplusplus >= 201103L
39 : # define TRACE_NAME \
40 : [](const char *path) { \
41 : const char *bp= rindex(path, '/'); \
42 : std::string ss((bp != NULL) ? bp + 1 : path); \
43 : size_t lastindex= ss.find_last_of("."); \
44 : return ss.substr(0, lastindex); \
45 : }(__BASE_FILE__) \
46 : .c_str()
47 : #else
48 : # define TRACE_NAME "ptr_address_string"
49 : #endif
50 :
51 0 : int main()
52 : {
53 0 : const char *real_msg= "hello";
54 0 : int *ip= (int *)malloc(sizeof(int));
55 0 : char *cp= (char *)malloc(100);
56 0 : strncpy(cp, real_msg, 99);
57 0 : cp[99]= '\0';
58 0 : unsigned long long *ullp= (unsigned long long *)malloc(sizeof(unsigned long long));
59 0 : *ullp= 555;
60 :
61 0 : TLOG(TLVL_INFO) << "TRACE_NAME is " << TRACE_NAME;
62 0 : TLOG(TLVL_INFO) << "the real message is " << real_msg;
63 0 : TLOG(TLVL_INFO) << "the copy message is " << cp;
64 0 : TLOG(TLVL_INFO) << "the address of message string - (void*)real_msg=" << (void *)real_msg;
65 0 : TLOG(TLVL_INFO) << "ip=" << ip;
66 0 : TLOG(TLVL_INFO) << "ullp=" << ullp << " *ullp=" << *ullp;
67 0 : TLOG(TLVL_INFO) << "std::string(\"hello\")=" << std::string("hello");
68 : #if __cplusplus >= 201103L
69 0 : std::unique_ptr<std::string> up_string{new std::string("new string")};
70 0 : std::unique_ptr<std::vector<int>> up_vector_int{new std::vector<int>{1, 2, 3}};
71 0 : TLOG(TLVL_INFO) << "unique_ptr up_string=" << up_string << " which is ptr to std::string (not the actual string within)";
72 0 : TLOG(TLVL_INFO) << "unique_ptr *up_string=" << *up_string;
73 0 : TLOG(TLVL_INFO) << "unique_ptr (*up_string).c_str()=" << (*up_string).c_str();
74 0 : TLOG(TLVL_INFO) << "unique_ptr (void*)((*up_string).c_str())=" << (void *)((*up_string).c_str());
75 0 : TLOG(TLVL_INFO) << "unique_ptr &(*up_string)[0]=" << &(*up_string)[0];
76 0 : TLOG(TLVL_INFO) << "unique_ptr (void*)&(*up_string)[0]=" << (void *)&(*up_string)[0];
77 0 : TLOG(TLVL_INFO) << "unique_ptr &(*up_vector_int)[0]=" << &(*up_vector_int)[0];
78 0 : TLOG(TLVL_INFO) << "unique_ptr (*up_vector_int)[0]=" << (*up_vector_int)[0];
79 : #endif
80 0 : return (0);
81 0 : } // main
|