Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
M

Mike the Red

@Mike the Red
About
Posts
68
Topics
25
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Unanticipated behavior from overloaded cast operator... [modified]
    M Mike the Red

    I didn't understand your post until Pallini gave a longer explanation of this. Thanks for your help!!! PS: Thanks for the suggestion about the Big Two, but I don't think so in this case. The idea behind the class is for it to always behave as a dynamically-allocated LPBYTE, but somtimes point to a MMF, when the buffer is larger than a certain threshold. In both cases (true LPBYTE or LPBYTE pointing to an MMF), the standard memory functions (memcpy, memset, etc.) are appropriate. (Which is why I want the cast to point to the same place as the member pointer m_buff.)

    C / C++ / MFC c++ question

  • Unanticipated behavior from overloaded cast operator... [modified]
    M Mike the Red

    -nt-

    C / C++ / MFC c++ question

  • Unanticipated behavior from overloaded cast operator... [modified]
    M Mike the Red

    I'm clear that m_buff is a pointer, and what I want is for (LPBYTE) foo to point to the same place m_buff points, not to point to m_buff itself. My class uses the buffer size to sometimes have m_buff point to a dynamically allocated BYTE array and sometimes point to a MMF, but to allow the given buffer to be accessed as an LPBYTE regardless of the buffer 'implementation.' It seems, though, that (LPBYTE) foo gives a pointer to the testClass object, rather than the memory pointed to by m_buff. I think I said it in the original post, but I could accomplish this easily with a member function like

    LPBYTE getBuff() { return &this->m_buff[0]; };

    ..but I thought this was a good opportunity to learn about operator overloading. Thanks! MZR

    C / C++ / MFC c++ question

  • Unanticipated behavior from overloaded cast operator... [modified]
    M Mike the Red

    This:

    LPBYTE bar = foo->operator LPBYTE();
    for (int i = 0; i < 10; i++)
    cout << bar[i];
    cout << "\n";

    Gives the expected output

    0123456789

    ....it's like my overloaded operator isn't being called when I try to explicitly cast to (LPBYTE).

    C / C++ / MFC c++ question

  • Unanticipated behavior from overloaded cast operator... [modified]
    M Mike the Red

    I changed the operator function to:

    operator LPBYTE () { cout << "Inside operator.\n"; return &this->m_buff[0]; };

    ...but Inside operator. never appears in the output. If I explicitly use foo->operator LPBYTE() then Inside operator. appears in the output but not if I use an explicit cast (LPBYTE) foo. Any ideas on that one ? Thanks, MZR

    C / C++ / MFC c++ question

  • Unanticipated behavior from overloaded cast operator... [modified]
    M Mike the Red

    The code:

    class testClass {
    public:
    testClass() { m_buff = new BYTE[20];
    memcpy(m_buff, "0123456789", 10);
    };
    ~testClass() { delete m_buff; };

    operator LPBYTE () { return m\_buff; };
    
    void show() {
    	cout << "m\_buff = " << (\_\_int64) (void \*) m\_buff << "\\n\\t"; // Show address of m\_buff
    	for (int i = 0; i < 10; i++) // Show contents of m\_buff
    		cout << m\_buff\[i\];
    	cout << "\\n";
    };
    

    private:
    LPBYTE m_buff;
    };

    int main(int argc, char * argv[])
    {
    testClass * foo = new testClass();
    foo->show();
    cout << "foo = " << (__int64) (void *) foo << "\n"; // Show address of foo
    cout << "(LPBYTE) foo = " << (__int64) (void *) ( (LPBYTE) foo ) << "\n\t"; // Show address of cast (LPBYTE) foo
    for (int i = 0; i < 10; i++) // Show contents of cast (LPBYTE) foo
    cout << ((LPBYTE) foo)[i];

    cout << "\\n";
    delete foo;
    return true;
    

    };

    And some output:

    m_buff = 9180016
    0123456789
    foo = 9179968
    (LPBYTE) foo = 9179968
    -garbage-

    I was expecting to see:

    m_buff = 9180016
    0123456789
    foo = 9179968
    (LPBYTE) foo = 9180016 // same address as m_buff
    0123456789

    What am I missing ? I know I could accomplish something similar with a member function returning the address of m_buff, but in my ever more ridiculous quest to learn more aspects of C++, I wanted to try this operator overloading bit.... As always, any guidance you can offer is greatly appreciated. MZR

    modified on Wednesday, March 17, 2010 5:11 AMP.S. My apologies for using __int64... I know it's Microsoft specific, but I use what I've got.

    C / C++ / MFC c++ question

  • Structure/Class member addresses
    M Mike the Red

    -nt-

    C / C++ / MFC performance help question

  • Data Type Conversions
    M Mike the Red

    If you're dealing with floats that have been converted to strings, just convert them back to floats. The unsigned short is the same as wchar_t - you should be able to use an explicit cast to 'convert' between them. _wtof() converts a wchar_t string to a double, which you can then cast to a float, if the value is withing the range of type float. _ecvt() converts a double (or float cast to double) to a wchar_t string, though you'll have to do some string manipulation to put the sign and/or decimal in the indicated place(s). Both functions are in <stdlib.h>. Hope this is useful, MZR

    C / C++ / MFC data-structures help question

  • Structure/Class member addresses
    M Mike the Red

    struct myStruct {
    int x;
    int y;
    char ch[10];
    };
    struct myOtherStruct {
    int y;
    char ch[10];
    int x;
    };
    class myClass {
    public:
    myClass() { };
    ~myClass() { };

    void someMethod(int x) { /\*do something with x\*/; };
    

    private:
    int a;
    int b;
    char ch[10];
    };

    int main() {

    myStruct	s1;
    myOtherStruct	s2;
    myClass		c;
    return 0;
    

    };

    Given these struct/class definitions, is it always safe to assume: &s1.x < &s1.y < &s1.ch &s2.y < &s2.ch < &s2.x &c.a < &c.b < &c.ch (Considering the addresses as numbers, like you'd get from (__int64)&s1.x) Maybe an easier way to ask this is, "Is the order of the members in memory the same as the order in which they are declared?" Thanks in advance for any help you guys can offer!

    C / C++ / MFC performance help question

  • delete [] problem, and extra char(s) showing up in substring?
    M Mike the Red

    I can't tell you how many times I've tried to find this function - I KNEW there had to be one... I always looked at the list of "String Manipulation Routines", saw strtok and its description, and said to myself "What the hell is a token?" :doh: Thankfully, I followed your link and looked at the example. You're right - this could ease my life in a LOT of cases! :thumbsup: Thank you, sir! MZR

    C / C++ / MFC help question

  • delete [] problem, and extra char(s) showing up in substring?
    M Mike the Red

    -nt-

    C / C++ / MFC help question

  • delete [] problem, and extra char(s) showing up in substring?
    M Mike the Red

    int main() {
    char * lpszData = "file=abcdefg&info=&x=3&4=5";
    int count = 0;
    char * pt = strchr(lpszData, '&');
    while ( pt != NULL ) {
    count++;
    pt = strchr(++pt, '&');
    };
    cout << "Data: " << lpszData << "\n";
    cout << "Count: " << count << "\n\n";

    	char \*\* split = new char \* \[count + 1\];
    	char \* start = lpszData;
    
    	for (int i = 0; i <= count; i++) {
    		pt = strchr(start, '&');
    		if ( pt == NULL )
    			pt = lpszData + strlen(lpszData);
    		split\[i\] = new char\[pt - start + 1\];
    		strset(split\[i\], 0);
    		strncpy(split\[i\], start, pt - start);
    
    		start = pt + 1;
    	};
    	for (int i = 0; i <= count; i++) {
    		cout << "split\[" << i << "\] = " << split\[i\] << "\\n";
    	};
    	for (int i = 0; i <= count; i++)
    		delete \[\] split\[i\];
    	        
    	delete \[\] split;
    

    }

    First, the delte [] split[i]; in the for loop at the end gives me an error, but when I remove this line the code leaks. Second, the output is:

    Data: file=abcdefg&info=&x=3&4=5
    Count: 3

    split[0] = file=abcdefg
    split[1] = info=_[extended_char]_3
    split[2] = x=3
    split[3] = 4=5

    Try as I might I can't figure out where the extra characters are coming from in split[1]. Can anyone help with either of these problems? Thanks for any assistance you can give, MZR

    C / C++ / MFC help question

  • std::search matches start of data before passing to function, but not within function?
    M Mike the Red

    In main(), del is of type BYTE and sizeof(del) is 1. Once it's passed to separate, del is of type LPBYTE and sizeof(del) is 4.

    C / C++ / MFC graphics algorithms question

  • std::search matches start of data before passing to function, but not within function?
    M Mike the Red

    #ifndef BYTE // For OS X -ers
    #define unsigned char BYTE
    #define unsigned char * LPBYTE
    #endif
    #include <vector>
    #include <algorithm>
    void separate(vector< pair<LPBYTE, LPBYTE> >& retVec, LPBYTE data, LPBYTE delimeter, /* out */bool *bComplete = 0) {
    retVec.clear();
    if (bComplete) *bComplete = false;

    if (!data)	// No data - leave retVec empty
    	return;
    
    LPBYTE dataEnd = data + sizeof(data);
    LPBYTE delEnd = delimeter + sizeof(delimeter);
    size\_t delSize = distance(delimeter, delEnd);
    
    LPBYTE found = std::search(data, dataEnd, delimeter, delEnd);
    
    if (found == delEnd)	// Delimeter not in data - leave retVec empty
    	return;
    
    cout << "In separate(...)\\n";
    if (data\[0\] == delimeter\[0\])
    	cout << "data\[0\] == delimeter\[0\]\\n";
    
    if (found == data)
    	cout << "Found == data.\\n(" << found\[0\] << " == " << data\[0\] << ")\\n";
    
    if (found != data)  			// Found delimeter ends first split
    	cout << "Found != data; found == data\[" << distance(data, found) << "\].\\n";
    return;
    

    };
    int main()
    {
    BYTE buffer[] = {',', '1', '2', '3', ',', '4', '5', '6', ',', '7', '8', '9', ',', 0};
    BYTE del = ',';
    bool bComp = true;

    LPBYTE found = std::search(&buffer\[0\], &buffer\[0\] + sizeof(buffer), &del, &del + sizeof(del));
    if (found == &buffer\[0\])
    	cout << "found == &buffer\[0\]\\n";
    else
    	cout << "found == &buffer\[" << distance(&buffer\[0\], found) << "\]\\n";
    
    vector< pair<LPBYTE, LPBYTE> > lpb;
    separate(lpb, buffer, &del, &bComp);
    
    return 0;
    

    }

    Output:

    found == &buffer[0]
    In separate(...)
    data[0] == delimeter[0]
    Found != data; Found == data[4]

    When I search from within main(), the leading comma is found, but once I pass the data & delimeter to separate(), it misses the leading comma and finds the next one.. obviously I'm missing something - can anyone spot what it is? Your assistance is greatly appreciated! MZR

    C / C++ / MFC graphics algorithms question

  • Vector elements and the heap...or the stack...
    M Mike the Red

    That's exactly what I was looking for - thank you! I know you've mentioned BOOST to me a couple times, and I may look into it when I've learned a bit more, but if I use other people's code/libraries, how will I learn for myself? MZR

    C / C++ / MFC data-structures help graphics algorithms announcement

  • Vector elements and the heap...or the stack...
    M Mike the Red

    I have a BYTE array that I'm trying to split into parts and put the parts in a vector:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <malloc.h> // For the 'on the stack' version
    #ifndef LPBYTE
    typedef unsigned char * LPBYTE // For you OS X types ; )
    #endif

    void split(vector<LPBYTE>& vect, const LPBYTE& lpbData) {
    LPBYTE start = lpbData; // first part starts at beginning of lpbData
    LPBYTE end = lpbData + (size_t) 2; // This is normally calculated by finding a separating sequence within lpbData
    size_t len = distance(start, end); // Size of the part

    // Add part to vector
    LPBYTE lpbPart = new BYTE\[len\];
    /\* Or, on the stack 
    LPBYTE lpbPart = (LPBYTE) \_alloca(len)
    \*/
    
    memcpy(lpbPart, start, len);
    vect.push\_back( lpbPart );
    

    }
    int main() {
    LPBYTE data = {'a', 'b', 'c', 12, 22, 'Q'};
    vector<LPBYTE> parts;
    split(parts, data);

    for (vector<LPBYTE>::iterator vIT = parts.begin(); vIT != parts.end(); vIT++)
    	cout << (char \*) \*vIT << "\\n";
    parts.clear(); // This does no good, but I threw it in for good measure o\_O
    

    }

    The problem with the heap version is that it leaks. But if I use the stack version, or if I were to delete lpbPart inside split() (after adding it to the vector), a bunch of garbage is output 'cause the address is no longer valid. I could iterate through the vector when I'm done with it, delete-ing each element... but I was told recently that that is a bad programming practice. I am leaning towards deriving a class from vector and delete-ing the elements in the destructor, but I thought I'd run it by you guys first and see if there was a better way. OR if there is a way to make a sub-array of an array, where the sub-array points to an address within the array but has a length shorter than the distance from the address within the source array to its end. This way would avoid the stack and the heap and the addresses would be valid so long as the source LPBYTE data was valid. It isn't good code, but to better explain what I mean:

    LPBYTE data = {'a', 'b', 'c', 12, 22, 'Q'};
    BYTE sub[2];
    &sub = data + 2; // This is bad code, but what I mean is to point sub at the 'c' in data

    As always, any help ya'll can give is greatly appreciated! MZR

    C / C++ / MFC data-structures help graphics algorithms announcement

  • Is it safe to cast from LPBYTE to LPSTR to LPBYTE ?
    M Mike the Red

    -nt-

    C / C++ / MFC data-structures help question

  • Is it safe to cast from LPBYTE to LPSTR to LPBYTE ?
    M Mike the Red

    I have a BYTE array that contains a mix of ANSI and binary data. I'm wanting to convert the LPBYTE to LPSTR so that I can use the CString functions to pull substrings of either ANSI or binary data out of the BYTE array. I know that explicit casts can be hazardous, so I tried this:

    int _tmain(int argc, _TCHAR* argv[])
    { // (BYTE = unsigned char)
    unsigned char uc = 255; // 255 is max value for unsigned char, outside range for char

    cout << "unsigned char uc = " << (int) uc << "\\n";
    char c = (char) uc;
    cout << "char c = (char) uc = " << (int) c << "\\n";
    uc = (unsigned char) c;
    cout << "(unsigned char) c = " << (int) uc << "\\n";
    

    }


    OUTPUT:
    unsigned char uc = 255
    char c = (char) uc = -1
    (unsigned char) c = 255

    I'm not 100% that this little test tells me it's safe to convert binary data in a BYTE array to a CHAR array and back... Can you gurus help me out, again, please? :confused:

    C / C++ / MFC data-structures help question

  • RAII / RIIA best-practices question about std::vector
    M Mike the Red

    -nt-

    C / C++ / MFC question learning graphics docker lounge

  • RAII / RIIA best-practices question about std::vector
    M Mike the Red

    That I wasted the last 4 hours trying to stop this from leaking:

    int _tmain(int argc, _TCHAR* argv[])
    {
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    vector<char *> vc;
    vc.push_back("test");
    _CrtDumpMemoryLeaks();
    return 0;
    }


    Detected memory leaks!
    Dumping objects ->
    c:\program files\microsoft visual studio .net 2003\vc7\include\crtdbg.h(689) : {44} normal block at 0x008A0FF0, 4 bytes long.
    Data: < C > A0 A5 43 00
    Object dump complete.
    The program '[7368] ConTest.exe: Native' has exited with code 0 (0x0).

    When all I needed was a $#^$#&-ing pair of braces...

    int _tmain(int argc, _TCHAR* argv[])
    {
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    {
    vector<char *> vc;
    vc.push_back("test");
    }
    _CrtDumpMemoryLeaks();
    return 0;
    }

    I just love learning curves, don't you? Thank you for your help, Stuart! ...thanks to you, I wasted 4 hours instead of 5 or 6 ;P

    C / C++ / MFC question learning graphics docker lounge
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups