473,387 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

scoping issue

okay so the code in question looks like:

namespace util {
template<typename C> inline void mmapw(C *& ptr, size_t length, int prot,
int flags, int fd, off_t offset) {

if((ptr = (C*)mmap(NULL,length,prot,flags,fd,offset)) == NULL) {
err_mesg("mmap failed");
exit(EXIT_FAILURE);
}
return;
}
// more wrappers

}

namespace Parser {

// lot of stuff

class XMLRobot : public ost::XMLStream {

private:
// Data types and private functions for parsing

// tons of decs

unsigned char *xml_string;

public:

// Constructors

XMLRobot(void);
// XMLRobot can either be instantiated with a file or a string of XML data
// However the constructors need to be very different as we need an explicit length
// For the time when a string is passed;

XMLRobot(const char *);
XMLRobot(unsigned char*, int);
XMLRobot(const XMLRobot&);

XMLRobot& operator=(const XMLRobot& b) {
this->name2movementmap = b.name2movementmap;
this->addlist = b.addlist;
this->is_file = b.is_file;
}

bool got_state_req(void) const { return get_state; }
void Close(void);
int Read(unsigned char*, int);
// tons more code
};

XMLRobot::XMLRobot(const char *filen):is_file(true),get_state(false),b_read(0){
int fd = util::openw(filen, O_RDONLY);
struct stat fs;

util::fstatw(fd, &fs);
util::mmapw<unsigned char>(this->xml_string, fs.st_size, PROT_READ,MAP_SHARED, fd, 0);
this->t_bytes = fs.st_size;
this->xml_file = filen;

init_perf_hash();
util::closew(fd);

}

int XMLRobot::Read(unsigned char* buf, int len) {

if(this->b_read + len > this->t_bytes) {
int amt = this->t_bytes - this->b_read;
unsigned char *ptr = this->xml_string + this->b_read;

for(int i = 0; i < amt; i++, ptr++, buf++) { *buf = *ptr; }

this->b_read = this->t_bytes;
return amt;

} else {
std::cout << (const char*)this->xml_string << "\n";
unsigned char *ptr = this->xml_string;
ptr += this->b_read;
for(int i = 0; i < len; i++, ptr++, buf++) { *buf = *ptr; }

this->b_read += len;
return len;
}
}

so the seg fault is in the std::cout line directly above. I hope this makes the problem a bit more clear
as this is slowly driving me nuts. Thanks for the time

Fred

--
Politics is the art of looking for trouble, finding it,
misdiagnosing it, and then misapplying the wrong remedies.
Groucho Marx
Jul 19 '05 #1
4 1665

"Frederick Grim" <fe****@quixote.cims.nyu.edu> wrote in message news:jU**************@typhoon.nyu.edu...
okay so the code in question looks like:

I don't know wh at scoping has to do with this problem.

std::cout << (const char*)this->xml_string << "\n";


If I understand what the rest of your code does, xml_string points to a memory
mapped piece of file. What makes you think that you will encounter a null before
you run off the end of the end of the mapping.

You're going to have to use cout.write or something similar to write exactly the
number of characters that you know are there (either len which you never check
for validity or fs.st_size you used in the constructor).
Jul 19 '05 #2
Frederick Grim wrote:
okay so the code in question looks like:

namespace util {
..... code that does not compile ... snipped

}

so the seg fault is in the std::cout line directly above. I hope this makes the problem a bit more clear
as this is slowly driving me nuts. Thanks for the time


The code below compiles and runs. The problem you're looking for is not
here, like I said in my last post. Your object is getting corrupted.

From this point on, this is OT for this group.

Strong suggestion, next time you post a snippet of code, take the time
to debug it and compile it yourself.

Here is the code, all compilable and runs just fine. I had to take a
few guesses as to what missing members were and they're not initialized
and I'm sure it's wrong, but the part of the code that you seem to be
concerned about is working fine (without close inspection - since it
seems to work fine !).

I used gcc 3.3.1 and Linux 2.4.20-epoll (patched with epoll support).

I suggest you try running the code you have with efence or valgrind
(preferably valgrind). You might (likely) get lucky and get a few
choice error messages that will show you how the object is getting
corrupted.

(I feel like I should be charging for consulting services here). You
have other problems - there are some better practices. You should try
to hide low level functionality behind classes. It's a bit of a
mish-mash of code - XML parsing and mmapping need not be mixed at the
same time, mmap is far too low level. What do you think this util
library does for you ? OK OK. I'm not trying to pick on you. Just
giving some hints on making your life easier.
G
#include <unistd.h>
#include <sys/mman.h>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
namespace util {
template<typename C> inline void mmapw(C *& ptr, size_t length, int prot,
int flags, int fd, off_t offset) {

if((ptr = (C*)mmap(NULL,length,prot,flags,fd,offset)) == NULL) {
perror("mmap failed");
exit(EXIT_FAILURE);
}
return;
}
// more wrappers

}
namespace ost
{
class XMLStream
{
public:

};

}

namespace Parser {

// lot of stuff

class XMLRobot : public ost::XMLStream {

private:
// Data types and private functions for parsing

// tons of decs
char * name2movementmap;
char * addlist;
bool is_file;
bool get_state;
int b_read;

int t_bytes;
const char * xml_file;
unsigned char *xml_string;

public:

// Constructors

XMLRobot(void);
// XMLRobot can either be instantiated with a file or a string
of XML data
// However the constructors need to be very different as we
need an explicit length
// For the time when a string is passed;

XMLRobot(const char *);
XMLRobot(unsigned char*, int);
XMLRobot(const XMLRobot&);

XMLRobot& operator=(const XMLRobot& b) {
this->name2movementmap = b.name2movementmap;
this->addlist = b.addlist;
this->is_file = b.is_file;

return * this;
}

bool got_state_req(void) const { return get_state; }
void Close(void);
int Read(unsigned char*, int);
// tons more code
};

XMLRobot::XMLRobot(const char
*filen):is_file(true),get_state(false),b_read(0){
int fd = open(filen, O_RDONLY);
struct stat fs;

if ( fd < 0 )
{
perror( "can't open file" );
throw( "Can't open" );
}

fstat(fd, &fs);
util::mmapw<unsigned char>(this->xml_string, fs.st_size,
PROT_READ,MAP_SHARED, fd, 0);
this->t_bytes = fs.st_size;
this->xml_file = filen;

// init_perf_hash();
close(fd);

}

int XMLRobot::Read(unsigned char* buf, int len) {

if(this->b_read + len > this->t_bytes) {
int amt = this->t_bytes - this->b_read;
unsigned char *ptr = this->xml_string + this->b_read;

for(int i = 0; i < amt; i++, ptr++, buf++) { *buf = *ptr; }

this->b_read = this->t_bytes;
return amt;

} else {
std::cout << (const char*)this->xml_string << "\n";
unsigned char *ptr = this->xml_string;
ptr += this->b_read;
for(int i = 0; i < len; i++, ptr++, buf++) { *buf = *ptr; }

this->b_read += len;
return len;
}
}

} // end namespace

int main()
{

Parser::XMLRobot a( "jnk" );

unsigned char buf[10];

a.Read( buf, sizeof( buf ) );

std::cout << std::string( buf, buf+10 ) << "\n";

}

Jul 19 '05 #3
Gianni Mariani <gi*******@mariani.ws> wrote:
Frederick Grim wrote:
okay so the code in question looks like:

namespace util {
.... code that does not compile ... snipped
}

so the seg fault is in the std::cout line directly above. I hope this makes the problem a bit more clear
as this is slowly driving me nuts. Thanks for the time

The code below compiles and runs. The problem you're looking for is not
here, like I said in my last post. Your object is getting corrupted. From this point on, this is OT for this group. Strong suggestion, next time you post a snippet of code, take the time
to debug it and compile it yourself.
How would I know that it seg faults if I hadn't compilied it and spent some time
with a debugger? Moreover as the post should have made clear I left *alot* of
code out. I am not going to ask questions until after I have spent alot of time
trying to figure the problem out for myself. And I only saw fit to bother the
gurus (please do not take this as sarcasm it is not at all) of comp.lang.c++
once I felt as though I had genuinely misunderstood cpp
scoping rules.
Here is the code, all compilable and runs just fine.
I left out alot of code that
was important to the design of the thing (what there is of a design anyhow)

I had to take a few guesses as to what missing members were and they're not initialized
and I'm sure it's wrong, but the part of the code that you seem to be
concerned about is working fine (without close inspection - since it
seems to work fine !).
good enough. I though it looked okay (as in working okay)
I used gcc 3.3.1 and Linux 2.4.20-epoll (patched with epoll support). I suggest you try running the code you have with efence or valgrind
(preferably valgrind). You might (likely) get lucky and get a few
choice error messages that will show you how the object is getting
corrupted.
Good suggestion. Thanks
(I feel like I should be charging for consulting services here).
Now this is a little insulting don't you think?

You have other problems - there are some better practices.
I am aware of that.

You should try to hide low level functionality behind classes. It's a bit of a
mish-mash of code - XML parsing and mmapping need not be mixed at the
same time, mmap is far too low level.
You know after the consulting services comment I can't imagine as this advice
will really stick with me.

What do you think this util library does for you ?
check return values.

OK OK. I'm not trying to pick on you.

sure as hell feels like it.

Just giving some hints on making your life easier.
I have never been quite put off by such a post before. I am
not trying to start a flame or start tit-for-tat insulting but I have
participated in any number of technical discussions and I almost never get or
give this sort of patronizing tone from/to others. I know you are taking time
out to help and I deeply appreciate that however if this is the sort of response you
want to give then just ignore my post or cat freds_post > /dev/null. But come
on.

Fred

G
#include <unistd.h>
#include <sys/mman.h>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
namespace util {
template<typename C> inline void mmapw(C *& ptr, size_t length, int prot,
int flags, int fd, off_t offset) { if((ptr = (C*)mmap(NULL,length,prot,flags,fd,offset)) == NULL) {
perror("mmap failed");
exit(EXIT_FAILURE);
}
return;
}
// more wrappers }
namespace ost
{
class XMLStream
{
public: }; } namespace Parser { // lot of stuff class XMLRobot : public ost::XMLStream { private:
// Data types and private functions for parsing // tons of decs
char * name2movementmap;
char * addlist;
bool is_file;
bool get_state;
int b_read; int t_bytes;
const char * xml_file;
unsigned char *xml_string; public: // Constructors XMLRobot(void);
// XMLRobot can either be instantiated with a file or a string
of XML data
// However the constructors need to be very different as we
need an explicit length
// For the time when a string is passed; XMLRobot(const char *);
XMLRobot(unsigned char*, int);
XMLRobot(const XMLRobot&); XMLRobot& operator=(const XMLRobot& b) {
this->name2movementmap = b.name2movementmap;
this->addlist = b.addlist;
this->is_file = b.is_file; return * this;
} bool got_state_req(void) const { return get_state; }
void Close(void);
int Read(unsigned char*, int);
// tons more code
}; XMLRobot::XMLRobot(const char
*filen):is_file(true),get_state(false),b_read(0){
int fd = open(filen, O_RDONLY);
struct stat fs; if ( fd < 0 )
{
perror( "can't open file" );
throw( "Can't open" );
} fstat(fd, &fs);
util::mmapw<unsigned char>(this->xml_string, fs.st_size,
PROT_READ,MAP_SHARED, fd, 0);
this->t_bytes = fs.st_size;
this->xml_file = filen; // init_perf_hash();
close(fd); } int XMLRobot::Read(unsigned char* buf, int len) { if(this->b_read + len > this->t_bytes) {
int amt = this->t_bytes - this->b_read;
unsigned char *ptr = this->xml_string + this->b_read; for(int i = 0; i < amt; i++, ptr++, buf++) { *buf = *ptr; } this->b_read = this->t_bytes;
return amt; } else {
std::cout << (const char*)this->xml_string << "\n";
unsigned char *ptr = this->xml_string;
ptr += this->b_read;
for(int i = 0; i < len; i++, ptr++, buf++) { *buf = *ptr; } this->b_read += len;
return len;
}
} } // end namespace

int main()
{ Parser::XMLRobot a( "jnk" ); unsigned char buf[10]; a.Read( buf, sizeof( buf ) ); std::cout << std::string( buf, buf+10 ) << "\n"; }

--
Politics is the art of looking for trouble, finding it,
misdiagnosing it, and then misapplying the wrong remedies.
Groucho Marx
Jul 19 '05 #4
Frederick Grim wrote:
Gianni Mariani <gi*******@mariani.ws> wrote:

I have never been quite put off by such a post before. I am
not trying to start a flame or start tit-for-tat insulting but I have
participated in any number of technical discussions and I almost never get or
give this sort of patronizing tone from/to others. I know you are taking time
out to help and I deeply appreciate that however if this is the sort of response you
want to give then just ignore my post or cat freds_post > /dev/null. But come
on.


What did you find so insulting ?

A) You post a pile of code that does not work.

B) When someone spends the time to make it work, it does not show the
problem you describe.

C) Suggestions are made as to "percieved" problems of the design - in an
effort to give you a better alternative. (In my opinion really is a
problem waiting to happen given that one snippet.)

D) You become put-off ?

Dude. Chill a little. No-one is insulting you - yet (and it won't be
me). Go re-read my post and imagine I had a smile on my face.

Peace
G

Jul 19 '05 #5

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

Similar topics

2
by: David Stockwell | last post by:
Hi, Another of my crazy questions. I'm just in the process of learning so bear with me if you can. I actually ran it.... with two test cases TEST CASE 1: Say I have the following defined:...
2
by: Robert M. Gary | last post by:
I'm curious what the ANSI C++ standard says about nested classes. I'm not able to find where in the ANSI C++ standard this is addressed. The issue is the accessibility of sibling nested classes....
2
by: macrom75 | last post by:
I have a script that I found online to do dynamic checking of passwords (for verifying that the re-typed password matches the original). This code works great in a basic HTML document, but when I...
1
by: Michael | last post by:
I am having a problem with scoping of parameters in my XSLT Stylesheet...here is the stylesheet (the xml document is irrelevant for the example) <?xml version="1.0" encoding="UTF-8"?>...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
3
by: morris.slutsky | last post by:
So every now and then I like to mess around with hobby projects - I often end up trying to write an OpenGL video game. My last attempt aborted due to the difficulty of automating game elements and...
17
by: Chad | last post by:
The following question stems from Static vs Dynamic scoping article in wikipedia. http://en.wikipedia.org/wiki/Scope_(programming)#Static_versus_dynamic_scoping Using this sites example, if I...
4
by: bob357 | last post by:
Hello everyone, I'm a novice programmer and am having trouble with a text-adventure game I am programming. There are two classes, Hero and Monster, that contain functions that I want to be able...
2
by: Joshua Kugler | last post by:
I am trying to use lamdba to generate some functions, and it is not working the way I'd expect. The code is below, followed by the results I'm getting. More comments below that. patterns = (...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.