Thread: Help with logic

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Help with logic

    Im trying to replace this for() statement with memset() in a program,
    which seem to me the exact same thing,
    but i dont get the same result with memset() in the program.


    Code:
    int pass = 4;
    int array[pass-1];
    
    for(; j<pass; j++) array[j]=0;
           
    
    memset(array,'\0',pass-1);
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    How about
    Code:
    int array[pass] = { 0 };
    if you're just doing it once?

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    Well, you could try this way,

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    vector<int> test(10);
    
    int main(int argc, char *argv[])
    {
    	fill(test.begin(), test.begin()+10, 0);
    	for (int i = 0; i < test.size(); ++i)
    	{
    		cout << test[i] << " ";
    	}
    	return 0;
    }

    Also, there's an error here:
    Code:
    int pass = 4;
    int array[pass-1]; // only three indices declared, array[0,2].
    for(; j<pass; j++) array[j]=0; // out of bounds check on j = 3 ***
    You probably meant;

    Code:
    int pass = 4;
    int array[pass];
    Last edited by since; 12-04-2009 at 03:24 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks for the answers!

    Well i thought that in my example both meant to initialize every element of the array to 0.

    @Rags_to_riches example is simplier but i wanna initialize it dynamicaly so i guess i
    would have to use "new" + memset and that would be more code than just to use the for() loop.
    Code:
    cin  >>  pass;
    int * array = new int[pass];
    memset(array,'\0',pass);
    @Since, your first example is too much code for just to initialize to zero.

    And then you are right its out of bounds check on j = 3. Good catch. Thanks.
    Though its interesting that if i do it the right way like this, i get the same wrong result as with memset().

    Code:
    for(; j<pass-1; j++) array[j]=0;
    So to be on the safe side i guess i have to go with dynamic allocation.
    As i cant use c++ string, its not an int.

    Now i realize thats why you talked about vectors.
    Cool! Problem solved with vector.
    Last edited by Ducky; 12-04-2009 at 04:42 PM.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    @Since, your first example is too much code for just to initialize to zero.
    That is true. std::fill is not needed, because the vector is constructed to value initialise its elements, and value initialisation for elements of type int means zero initialisation. As such, the example could be written as:
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
        vector<int> test(10);
        for (vector<int>::size_type i = 0; i < test.size(); ++i)
        {
            cout << test[i] << ' ';
        }
        return 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

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Awesome, thank you Laserlight!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Handling logic and draw cycles
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 07-25-2009, 10:15 AM
  2. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 01:05 PM
  3. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 05:41 PM
  4. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 10:58 PM