Unknown Stack corruption

This is a discussion on Unknown Stack corruption within the C++ Programming forums, part of the General Programming Boards category; Hello, everyone. I'm having a problem with a corrupted stack and I have no idea what the problem is. I ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Unknown Stack corruption

    Hello, everyone.

    I'm having a problem with a corrupted stack and I have no idea what the problem is.

    I defined a function to receive a character as input and spit out a binary representation in a string:

    Code:
    string to_binary_c(char c)
    {
    	int k = 0;
    	unsigned char j, i = static_cast<unsigned char>(c);
    	string t;
    	for(((j = ~0U) >>= 1)++; j > 0; j >>= 1, k++)
    		i & j ? t += '1' : t += '0';
    	return t;
    }
    If I ignore the compiler warning, there's nothing wrong with the result, but it keeps telling me that, "the stack around the variable j has been corrupted."

    BTW, I'm calling the function from within another function which does the same thing as the above but only does it with a character pointer:

    Code:
    string to_binary_cp(char* cp)
    {
    	int k = 0, s_len = strlen(cp);
    	string t, u;
    	for(int i = 0; i < s_len; i++)
    	{
    		u = to_binary_c(cp[i]);
    		u += '-';
    		t += u;
    	}
    	t.resize(t.size() - 1, '\0');
    	return t;
    }
    Any help would be appreciated.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    Made a couple changes to your function:
    Code:
    string to_binary_c(char c)
    {
        unsigned char j(128), i = static_cast<unsigned char>(c);
        string t;
        for(; j > 0; j >>= 1)
            i & j ? t += '1' : t += '0';
        return t;
    }


    This can also be done using a bitset:
    Code:
    #include <bitset>
    using namespace std;
    
    ...
    
    string to_binary_c(char c)
    {
        return bitset<8>(c).to_string();
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    i dont get any warnings at all except an unused var from gcc -Wall.. what compiler are you using.. also could it be that you never initialize j?

    or

    what if you use this in place of the for in to_bin_c
    for( ; c; c <<= 1) c & 128 ? t += '1' : t += '0';

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21