DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 23rd October 2009
robbak's Avatar
robbak robbak is offline
Real Name: Robert Backhaus
VPN Cryptographer
 
Join Date: May 2008
Location: North Queensland, Australia
Posts: 366
Default c++: writing to the *this pointer?

For context, I have a bitmapped field coming from a snmp reply that I can get from a printer. (panasonic workio DP-C264 - if anyone has technical documentation on it, I'd be glad to know!) I can information out of it with this line-
Code:
    vb.get_value( (unsigned char *)&Job, Job_len, sizeof(snmpjoblog ));
where Job is an instance of a class containing the what ints and char arrays I have detected: there are a lot of char[n] unknowns and int unknowns.
I'd like to roll the function to extract that info into the class, which would mean this:
Code:
    vb.get_value( (unsigned char *)this, Job_len, sizeof(snmpjoblog ));
As in, writing binary data over the top of the class itself. Now, when it compiled without error, I was suprised. But will this really work?
(I'm going to go try it with a utility I wrote, but I'd still like your input!)
__________________
The only dumb question is a question not asked.
The only dumb answer is an answer not given.
Reply With Quote
  #2   (View Single Post)  
Old 23rd October 2009
robbak's Avatar
robbak robbak is offline
Real Name: Robert Backhaus
VPN Cryptographer
 
Join Date: May 2008
Location: North Queensland, Australia
Posts: 366
Default

Strange as it all seems, this is working fine, reading the binary from a save file.
PHP Code:
#include <fstream>
#include <stdlib.h>

class snmpjoblog
{   
public:
    
char unknown1[8];
    
int jobCounter;
    
int unknown6;
    
char userName[40];
    
char computerName[20];
//8< snip other fields

    
int  Retrievechar *HostNameint requestJobCounter );
    
std::istreamRetrieve(std::istreaminStream );
};  

std::istreamsnmpjoblog::Retrievestd::istreaminStream )
{

    
inStream.read( (char *)thissizeof(snmpjoblog) );
    return 
inStream;
}

int main(int argcchar **argv)
{
    
snmpjoblog trap;
    
ifstream file;
    
file.openargv[1], ios::in );
    
trap.Retrievefile );
    
cout << "reading " << sizeoftrap) << " bytes of " << argv[2] << "\n";
    
cout << "Job counter = " << ntohltrap.jobCounter) << endl;
    
cout << "User name = " << trap.userName << endl;
    
cout << "computername = " << trap.computerName << endl;
    
file.close();
    return 
0;

(note : some headers may be missing)

I still cannot believe that this is a good thing - I mean, the class is overwriting itself!
__________________
The only dumb question is a question not asked.
The only dumb answer is an answer not given.
Reply With Quote
  #3   (View Single Post)  
Old 23rd October 2009
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,318
Default

Quote:
Originally Posted by robbak View Post
I still cannot believe that this is a good thing - I mean, the class is overwriting itself!
From the information provided, the class is simply a repository for data (& the data is composed of fundamental types, no user-defined types...) -- no inheritance or virtual member functions are involved. Because of this scenario, this most likely points to the beginning of data, so the problem decomposes down to how one dumps/restores a struct. You may want to confirm this to yourself by looking at the address of this & compare to the address of the first member.

If the class structure involved inheritance, this would point to the vtable which would change from one instance of the application to the next. Writing to disk wouldn't be a problem, but restoring followed by calling any virtual member function (if present...) would most likely cause an exception. Depending upon the compiler used, accessing data from a restored object may be problematic as well.

The exact semantics of how & where the vtable is located in memory is compiler-dependent, so you would need to look further into your compiler's documentation or play with a debugger to confirm if you are interested.
Reply With Quote
Reply

Tags
*this, c++

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing Papers in FreeBSD kagayaki General software and network 4 14th July 2008 11:31 PM
FreeBSD 7.0 Writing large amount to USB Disc cause kernel panic pvree FreeBSD General 1 13th June 2008 02:50 AM


All times are GMT. The time now is 06:06 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick