Thread: pointer problem

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    242

    pointer problem

    I'm trying to create a program that simulates blackjack to test some counting strategies. My program is having problems in the following (partial) function:

    Code:
    int * Carddeck::dealBlackJack(const int numPlayers)
    {
    	if (numPlayers < 1 || numPlayers > 6)
    	{
    		cout << "Number of players must be between 1 and 6.\n";
    		return 0;
    	}
    	int numCards = 3 + (2 * numPlayers); // +1 in case we have to burn a card
    	int * bjackHand;
    	bjackHand = new int [numCards];
    When I debug, I can't get past the last line here, and I get the message: "No source code available for this location." Then it allows me to look at the assembly, which I don't understand but will be happy to copy here if you guys need it to figure out what's wrong.

    I don't see what's wrong with this dynamic allocation of memory.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It seems to me that your IDE screwed up building something.
    Try a "Build->Clean" and then a "Build->Rebuild all" (for the debug variant of the project).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    242
    still does it

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I don't see what's wrong with this dynamic allocation of memory.

    There is everything wrong with this type of allocation. It's unsafe. Use an std::vector and be done with it.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are running in Debug mode, right? Not Release?
    And you are sure you haven't touched any Debug-related settings?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    242
    Yes, I'm running in debug mode. I tried eliminating this function from the class definition and making it into a function defined in the main program, and it started working (with an error that I still need to fix, but not an exception that just locks everything up).

    To Sebastiani: The way this problem has arisen and gotten more or less fixed (essentially without changing the program in any substantive way, just shuffling things around a bit) makes me think you're right about its being unsafe. But does that apply in general to dynamic memory allocation for an array? I mean, dynamic memory allocation seems like an option that one should use when called for... ?

    Also, I assume that since vectors aren't basic data types that they themselves get memory dynamically allocated--although the code regulating vectors (and strings) has obviously been created by experienced programmers with knowledge about possible problems and how to avoid them.

    But that would also mean that there are ways to allocate memory dynamically that have some additional safety measures (?).

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    242
    This function returning a vector is obviously incomplete, but I wondered if this basic technique is just fundamentally better:

    Code:
    vector<int> dealBjackHand(const int numPlayers, Carddeck bjDeck)
    {
    	if (numPlayers < 1 || numPlayers > 6)
    	{
    		cout << "Number of players must be between 1 and 6.\n";
    		vector<int> bjHand(1, -1);
    		return bjHand;
    	}
    
    	const int numCards = 3 + (2 * numPlayers); // +1 in case we have to burn a card
    
    	vector<int> bjHand(numCards, 0);
    	return bjHand;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Aisthesis
    This function returning a vector is obviously incomplete, but I wondered if this basic technique is just fundamentally better:
    In terms of memory management, yes. However, you probably want to pass the Carddeck by non-const reference since the complete implementation of this function will presumably modify it, and if not, then pass by const reference since a copy is probably unnecessary.

    Incidentally, you might want to consider throwing an exception instead of printing an error message in this function and returning a vector. If you really want to return a vector, returning an empty vector to signal an error is probably better since the caller can check for an error by calling the empty() member function.

    Also, note that you do not need to name an object that you will immediately return, e.g.,
    Code:
    return vector<int>(numCards, 0);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> But does that apply in general to dynamic memory allocation for an array? I mean, dynamic memory allocation seems like an option that one should use when called for... ?

    Well, in a nutshell, managing memory via a raw pointer means you don't have a destructor "protecting" it during the various aspects of it's lifetime. This is basically what RAII is all about.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM