473,789 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf("%s", myclass) prints vtable instead of m_data

Hi,

class MyString
{
char* m_data;
public:
MyString(const char* c)
{
m_data = new char[1024];
strcpy(m_data, c);
}
virtual ~MyString()
{
delete[] m_data;
}
};

int main()
{
MyString ms("KungFoo");
print("Here is: %s", ms);
}
this prints the address of the vtable pointer (VC++7.1) instead of
m_data. Is there any way of getting this to work, so that &ms points
to ms.m_data?

I know - removing the virtual keyword will do, but any other way?
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%c gl%ssic%ccom%c" , "ma", 58, 'g', 64, "ba", 46, 10);}

Sep 27 '06 #1
16 2782
Gernot Frisch <Me@Privacy.net wrote:
Hi,

class MyString
{
char* m_data;
public:
MyString(const char* c)
{
m_data = new char[1024];
strcpy(m_data, c);
}
virtual ~MyString()
{
delete[] m_data;
}
};

int main()
{
MyString ms("KungFoo");
print("Here is: %s", ms);
}
this prints the address of the vtable pointer (VC++7.1) instead of
m_data. Is there any way of getting this to work, so that &ms points
to ms.m_data?
Provide a function c_str (or whatever you want to call it), which
returns the char*. Then call printf like:

printf ("Here is: %s", ms.c_str ());

You cannot pass non-PODs thru the ellipsis, afaik.
I know - removing the virtual keyword will do, but any other way?
Removing the virtual keyword may work in your case, but it is still
invalid, as you are still passing a non-POD thru an ellipsis.

hth
--
jb

(reply address in rot13, unscramble first)
Sep 27 '06 #2
Gernot Frisch schrieb:
Hi,

class MyString
{
char* m_data;
public:
MyString(const char* c)
{
m_data = new char[1024];
strcpy(m_data, c);
You really don't want this. This will result, sooner or later, in an
access violation. What happens if c is nullptr? What happens if c does
not point to a null terminated C-style-string? What happens if c is is
1025 characters long?
}
virtual ~MyString()
{
delete[] m_data;
}
};

int main()
{
MyString ms("KungFoo");
print("Here is: %s", ms);
printf
}
this prints the address of the vtable pointer (VC++7.1) instead of
m_data. Is there any way of getting this to work, so that &ms points
to ms.m_data?
No. &ms will always point to the address of the instance ms. BTW, you
provided ms, not &ms to print(f). You'll save your own time if you use
copy'n'paste instead of typing source in your newsreader all the time,
including typing errors or whatever.
I know - removing the virtual keyword will do, but any other way?
No, it will not do. You just have been lucky that your program didn't
crash.

Summarized:
* you want std::string, or
* you want to implement a MyString::c_str () method, or
* you want to use <iostream>s and implement a custom
operator<< for ostream and MyString.

Just i.e.:

const char* MyString::c_str ()
{
return m_data;
}

and:

printf("%s", ms.c_str());

or:

cout << ms.c_str() << endl;

or (given that you implemented operator<< for ostreams):

cout << ms << endl;

This is, btw, more or less (omitting some details) what std::string
does. You probably do want to use std::string, it is not the worlds
best design of a string class, but its quite much better than anything
you and me will ever invent.
Best regards,
-- Markus
>
Sep 27 '06 #3

Markus Grueneis wrote:

class MyString
{
char* m_data;
public:
MyString(const char* c)
{
m_data = new char[1024];
strcpy(m_data, c);

You really don't want this. This will result, sooner or later, in an
access violation. What happens if c is nullptr? What happens if c does
not point to a null terminated C-style-string? What happens if c is is
1025 characters long?
c being null or not pointing to a nul-terminated string are a problem
with std::string too though.

It is true that 1024 is arbitrary and I don't see the reason for it.
Apart from that the class above is flawed in:

1. No defined copy-constructor or assignment, nor are they disabled in
a case where the default ones are not valid.

2. No means to access the string.

although perhaps he intended to add more methods (after all why have a
virtual destructor with no other virtual methods?)
Just i.e.:

const char* MyString::c_str ()
{
return m_data;
}
You missed a const. Or put in one too many. Actually you can do this:

char* MyString::c_str () const
{
return m_data;
}

but probably wouldn't want to as you'd probably want a const access
function to return a non-writable string (deep const).
This is, btw, more or less (omitting some details) what std::string
does. You probably do want to use std::string, it is not the worlds
best design of a string class, but its quite much better than anything
you and me will ever invent.
But possibly not the same semantics, and therefore there may be reasons
to write a string class.

For example, with std::string:

1. No method to get a writable buffer.
2. No methods for locking
3. Not immutable
4. Not safely portable between libraries.

For any number of those reasons you might wish to write a string class.
For the purpose of locking you'd possibly implement it in terms of
std::string (i.e. your class has a nested std::string and wraps it).

That's not saying that std::string should have all those features, just
that there doesn't have to be only one string class.

Sep 27 '06 #4
> MyString(const char* c)
> {
m_data = new char[1024];
strcpy(m_data, c);

You really don't want this. This will result, sooner or later, in
an access violation. What happens if c is nullptr? What happens if
c does not point to a null terminated C-style-string? What happens
if c is is 1025 characters long?
It's just for demonstration and giving you a working example of my
problem.

No. &ms will always point to the address of the instance ms. BTW,
you provided ms, not &ms to print(f). You'll save your own time if
you use copy'n'paste instead of typing source in your newsreader all
the time, including typing errors or whatever.
I provided "ms" because I wanted "ms". The Microsoft CString is
designed to work in a printf ellisis, so I wanted a safe way of doing
the same.
* you want std::string, or
* you want to implement a MyString::c_str () method, or
* you want to use <iostream>s and implement a custom
operator<< for ostream and MyString.
That's exaclty what I wanted _not_. I wanted to write "ms" to an
printf ellipsis. Thank you for showing the options to newbies, though.
Sep 27 '06 #5

Gernot Frisch wrote:
[snip]
>
I provided "ms" because I wanted "ms". The Microsoft CString is
designed to work in a printf ellisis, so I wanted a safe way of doing
the same.
If CString can print via printf it is because Microsoft relies on some
specific behaviour from its C++ compiler. This is more than a quick
hack in my opinion.
>
* you want std::string, or
* you want to implement a MyString::c_str () method, or
* you want to use <iostream>s and implement a custom
operator<< for ostream and MyString.

That's exaclty what I wanted _not_. I wanted to write "ms" to an
printf ellipsis. Thank you for showing the options to newbies, though.
Perhaps you are the newbie? In any case you can not in standard C++
create a class that prints via the printf family. If you want to ask
compiler specific questions go to the appropriate newsgroup.

/Peter

Sep 27 '06 #6
Perhaps you are the newbie? In any case you can not in standard C++
create a class that prints via the printf family. If you want to ask
compiler specific questions go to the appropriate newsgroup.
I just wanted to ask if there is a standard way of making my class's
object's address default the address of its first member. I know - and
have done - overloading of (const char*) operator, so I can use
printf. I just wanted to know if the very convenient way MS takes is
std.
As you said - it's not. That would suffice as an answer to me.
Regards,
-Gernot
Sep 27 '06 #7
peter koch wrote:
Gernot Frisch wrote:
[snip]

I provided "ms" because I wanted "ms". The Microsoft CString is
designed to work in a printf ellisis, so I wanted a safe way of doing
the same.

If CString can print via printf it is because Microsoft relies on some
specific behaviour from its C++ compiler. This is more than a quick
hack in my opinion.
If your string class contains a single pointer into a null-terminated
buffer of chars, with length, reference count or whatever housekeeping
stuff you want in memory *before* the chars [so eg bytes0-3 ref count,
bytes4-7 length, bytes8ff the chars, pointer points at byte 8, pace
alignmnent issues], and if the class has no virtual functions, then one
can see how printf("%s")ing such a thing would "just work". Is this
relying on specific compiler behaviour? Well I guess technically it is;
I'm sure I read somewhere that CString is designed specifically to
allow people to pass CStrings to printf in this broken way and have it
work, so I'd guess that this is how MS have done it.
Now just because they *could* have done it this way, doesn't mean they
*should* have done it this way, but making CString behave just like a
char* wrt printf is a good commercial decision even if we cringe at it
from a technical/purist perspective.

Sep 27 '06 #8
Earl Purple schrieb:
Markus Grueneis wrote:
>>[OP code]
You really don't want this. This will result, sooner or later, in an
access violation. What happens if c is nullptr? What happens if c does
not point to a null terminated C-style-string? What happens if c is is
1025 characters long?

c being null or not pointing to a nul-terminated string are a problem
with std::string too though.
That's correct, but you can initialize a std::string with a specified
length, and therefore have strings with \0 chars in it, etc. etc. It
does not deal with the problems I listed above, though.
It is true that 1024 is arbitrary and I don't see the reason for it.
Apart from that the class above is flawed in:

1. No defined copy-constructor or assignment, nor are they disabled in
a case where the default ones are not valid.

2. No means to access the string.
;-)
although perhaps he intended to add more methods (after all why have a
virtual destructor with no other virtual methods?)
>Just i.e.:

const char* MyString::c_str ()
{
return m_data;
}

You missed a const. Or put in one too many. Actually you can do this:

char* MyString::c_str () const
{
return m_data;
}

but probably wouldn't want to as you'd probably want a const access
function to return a non-writable string (deep const).
Thanks for the additions. I'm still to sloppy very often.
>This is, btw, more or less (omitting some details) what std::string
does. You probably do want to use std::string, it is not the worlds
best design of a string class, but its quite much better than anything
you and me will ever invent.

But possibly not the same semantics, and therefore there may be reasons
to write a string class.
That's correct too. But I was extrapolation from the OP's use of
printf() that this should not be the case. I was shown wrong on this.
Best regards,
-- Markus
For example, with std::string:

[real reasons to write a string class]
Sep 27 '06 #9
"Gernot Frisch" <Me@Privacy.net writes:
I just wanted to ask if there is a standard way of making my class's
object's address default the address of its first member. I know - and
have done - overloading of (const char*) operator, so I can use
printf.
A conversion operator will not be invoked if you pass something to an
ellipsis (what should be converted to?).

As standard C++ is concerned, you can't pass non-POD types to an
ellipsis, as others have said.

printf belongs to the C language; in C++ std::string and iostreams
are usually preferable.
I just wanted to know if the very convenient way MS takes is
std.
It wouldn't be MS if it was, would it?

Regards,

Jens
Sep 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
7992
by: Gernot Frisch | last post by:
Hi, I want to represent a double in char* as accurate as possible, but as short as possible: double char* 10000 1E5 1000.00123 1000.00123 ....
11
14553
by: Dario (drinking coffee in the office…) | last post by:
The following program int main(){printf("%.2f\n", 90.625);} what it must print out? I try it on several compiler/operating system and I obtaing different results. On some compilers it writes 90.62,
12
2358
by: baumann | last post by:
hi all, printf("%c",b) doesn't work properly. #include <stdio.h> int a , b; char d, e; char * p; float f; int main(int argc, char* argv) {
9
2794
by: geek | last post by:
Hi all, Why does a printf("%d") statement prints this as output and when I do printf("%c") it doesn't print anything. -13361016 Any help would be appreciated. Thanks,
19
5466
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
6
8801
by: WeiWangJi | last post by:
int i=8; printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--); printf("%d\n", i); why the results is: 8 8 7 8 8 -8 7 How to explain?
29
11119
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
3
34148
by: Ioannis Vranos | last post by:
Is printf("%ls") for printing wchar_t strings defined in C90, or it was added in C95?
27
2124
by: sophia | last post by:
Dear all, why in the following program #include<stdio.h> #include<stdlib.h> int main(void) {
0
9657
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10389
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10132
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9006
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6753
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5408
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5544
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2901
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.