Thread: initialize vectors

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    50

    initialize vectors

    Hi guys...
    I do not understand why the hell have I to initialize a vector. For example it is sensible, up to me, to initialize a scalar in this situation
    Code:
    int foo(int a)
    {
         int i, res=0; //ok, great idea!!! otherwise bad random result
         for(i=0; i<a;i++) res+=i;
         return(res);
    }
    but in this one I not agree
    Code:
    void foo2(int *res, int a)
    {
         int i, res=0; //ok, great idea!!! otherwise bad random result
         for(i=0; i<a;i++) res[i]=i;
    }
    
    int main()
    {
         int *res, ini_res[10], i;
         res=&ini_res[0];
         foo2(res, 10);
        for(i=0; i<10;i++) printf("%d\n",res[i]); 
    }
    but I am getting bad result without initializing ini_res...
    Has somebody a clue?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Why do you think an array of ints would be any different than a single int? "Vector" is not a datatype in C, and "scalar" is not particularly meaningful either, since all types in C are "scalar". Use the correct nomenclature if you want to be properly understood. Importing terms from some other setting is pointless.

    You can use this to initialize everything to zero:
    Code:
    int ray[10] = {0};
    However, by the standard zero is the only value that will work that way. You can initialize this way too:
    Code:
    int ray[4] = {1,2,3,4};
    If you do that, any elements you leave of the end (eg, if it were ray[10]) will be zero.
    Last edited by MK27; 06-07-2010 at 10:30 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    "scalar" is not particularly meaningful either, since all types in C are "scalar".
    Correction:
    Quote Originally Posted by C99 Section 6.2.5 Paragraph 21
    Arithmetic types and pointer types are collectively called scalar types. Array and structure types are collectively called aggregate types.
    Quote Originally Posted by C99 Section 6.2.5 Note 37
    Note that aggregate type does not include union type because an object with union type can only contain one member at a time.
    But yes, you should "use the correct nomenclature", i.e., aggregate type, or simply array, instead of "vector".
    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

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    50
    what I am trying to say is that I am initializing the vector with the final values without wasting time with loops to fill up the vector with "temporary" values...
    I did not know that in C it is sufficient to initialize a vector to zero in the way you showed me. Now I am happy because I can initialize a vector without a loop!!!

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Correction:
    But yes, you should "use the correct nomenclature", i.e., aggregate type, or simply array, instead of "vector".
    Thanks laserlight. I was not aware of the term "aggregate" or that an array qualifies as a type distinct from it's "scalar" base.

    Quote Originally Posted by violatro View Post
    what I am trying to say is that I am initializing the vector with the final values
    IT IS NOT A VECTOR! GRRRR!!! You might as well use the term "bunch 'o" or "collection" or "set" or whatever.
    Last edited by MK27; 06-07-2010 at 10:44 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Code:
    void foo2(int *res, int a)
    {
         int i, res=0; //ok, great idea!!! otherwise bad random result
         for(i=0; i<a;i++) res[i]=i;
    }
    You are passing foo2 an int * called res, and then in the first line you are declaring an int called res. This is BAD. You are declaring a variable with the same name but different type from what it was originally.

    I think what you think you are doing is this:

    Code:
    void foo2(int *res, int a)
    {
         int i;
         res=0;
         for(i=0; i<a;i++) res[i]=i;
    }
    And I think you think that this is going to set all of the values in the array res to 0. It won't. res is a pointer, telling res=0 probably won't get you anywhere. If you want to make all of the values in the int array 0, you have to use a loop in this context.

    Of course, since you are setting the elements of res equal to i in your loop there is no need to initialize here anyway, but there would be if you were doing +=i instead of =i, in which case your function would have to look like:

    Code:
    void foo2(int *res, int a)
    {
         int i;
         res=0;
         for(i=0; i<a;i++)
         {
              res[i]=0;
              res[i]+=i;
         }
    }



    And stop calling it a vector. It is an array of ints.
    Last edited by KBriggs; 06-07-2010 at 10:50 AM.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    50
    your suggestion is not working with variable-sized arrays...(bad new for me)

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    50
    Quote Originally Posted by MK27 View Post
    Thanks laserlight. I was not aware of the term "aggregate" or that an array qualifies as a type distinct from it's "scalar" base.



    IT IS NOT A VECTOR! GRRRR!!! You might as well use the term "bunch" or "collection" or "set" or whatever.
    I did not see laser's reply....sorry! array!
    Last edited by violatro; 06-07-2010 at 10:50 AM.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    50
    Quote Originally Posted by KBriggs View Post
    Code:
    void foo2(int *res, int a)
    {
         int i, res=0; //ok, great idea!!! otherwise bad random result
         for(i=0; i<a;i++) res[i]=i;
    }
    You are passing foo2 an int * called res, and then in the first line you are declaring an int called res. This is BAD. You are declaring a variable with the same name but different type from what it was originally.

    I think what you think you are doing is this:

    Code:
    void foo2(int *res, int a)
    {
         int i;
         res=0;
         for(i=0; i<a;i++) res[i]=i;
    }
    And I think you think that this is going to set all of the values in the array res to 0. It won't. res is a pointer, telling res=0 probably won't get you anywhere. If you want to make all of the values in the int array 0, you have to use a loop in this context.



    And stop calling it a vector. It is an array of ints.
    yes, it was a mistake. The second version is the right one.
    doing a loop to fill the vector with an iterative procedure is very time wasting.
    All of you are telling me to call a vector as "array"...ok, forgive me...but nobody, up to now, makes clear why is not the same to fill the array once, with the right values

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    It is the same, if you are putting the right values in using =. You just have to be careful when yo uuse the += operator that the value that is already in the slot you want is correct before you add anything to it.

    Your mistake was the double declaration though, there was nothing wrong with how you were filling the array.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by violatro
    your suggestion is not working with variable-sized arrays...(bad new for me)
    Yes, but then there are functions such as memset() from <string.h> that could help, depending on what you want. But if you need to use a loop, just use a loop.
    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

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    50
    Quote Originally Posted by KBriggs View Post
    It is the same, if you are putting the right values in using =. You just have to be careful when yo uuse the += operator that the value that is already in the slot you want is correct before you add anything to it.

    Your mistake was the double declaration though, there was nothing wrong with how you were filling the array.
    I thought so. but if I do not initialize the vector I get bad results...I really do not understand why. Maybe it is because I am using C code inside R...

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    50
    Quote Originally Posted by laserlight View Post
    Yes, but then there are functions such as memset() from <string.h> that could help, depending on what you want. But if you need to use a loop, just use a loop.
    Ok...but in this way I loose time. Maybe there is something wrong in the code because I used to do not initialize and all worked well...

  14. #14
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    . but if I do not initialize the vector I get bad results...I really do not understand why
    Post the smallest compilable program that demonstrates the problem. I think your problem is not coming from where you think it is.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by violatro
    Ok...but in this way I loose time.
    Have you actually measured the difference and found that it was significant with respect to the rest of your program?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM