scoping issue

Discussion in 'C++' started by Frederick Grim, Sep 12, 2003.

  1. 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::eek:penw(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
     
    Frederick Grim, Sep 12, 2003
    #1
    1. Advertising

  2. Frederick Grim

    Ron Natalie Guest

    "Frederick Grim" <> wrote in message news:jUm8b.3$...
    > 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).
     
    Ron Natalie, Sep 12, 2003
    #2
    1. Advertising

  3. 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";

    }
     
    Gianni Mariani, Sep 12, 2003
    #3
  4. Gianni Mariani <> 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
     
    Frederick Grim, Sep 13, 2003
    #4
  5. Frederick Grim wrote:
    > Gianni Mariani <> 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
     
    Gianni Mariani, Sep 14, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Stroud

    Scoping issue with import

    James Stroud, Feb 28, 2005, in forum: Python
    Replies:
    2
    Views:
    313
    Steve Holden
    Mar 1, 2005
  2. Replies:
    12
    Views:
    735
    Bo Persson
    Aug 15, 2007
  3. John Nagle
    Replies:
    22
    Views:
    639
    Gregory Ewing
    Oct 16, 2010
  4. Pawel Szymczykowski

    require_gem scoping issue?

    Pawel Szymczykowski, Nov 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    96
    Pawel Szymczykowski
    Nov 7, 2006
  5. James Golick

    Setter scoping issue

    James Golick, Oct 13, 2007, in forum: Ruby
    Replies:
    3
    Views:
    93
    ara.t.howard
    Oct 13, 2007
Loading...

Share This Page