Help with logic

This is a discussion on Help with logic within the C++ Programming forums, part of the General Programming Boards category; Im trying to replace this for() statement with memset() in a program, which seem to me the exact same thing, ...

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

    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);
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    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
    816
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    816
    Awesome, thank you Laserlight!
    Compiler MSVC++ 2013 with Code::Blocks.

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, 11: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, 06:41 PM
  4. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 11:58 PM

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