Thread: Brace-enclosed initializer list in an assignment

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Question Brace-enclosed initializer list in an assignment

    I found out accidentally that the following code is only supported in the c++0x standard...thus giving a gcc option "-std=c++0x" ....compiles it without problems..
    Code:
    int x[3];
    x = {1,2};
    x[2]=3;
    The three values in x are ...as expected 1,2 and 3.
    But when "int x[3];" is replaced by
    Code:
    int *x;
    x = new int[3];
    ..the error message "error: cannot convert ‘<brace-enclosed initializer list>’ to ‘int*’ in assignment " is displayed.
    Why does it happen?..
    also is there a way around this ?..i.e. I want the array dynamically allocated....
    also Does the STL define such a container to make something similar possible?(in that case I'd like to assign ..say..3 arbitrary values to three of its elements in a single statement. )

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Code:
    int x[3];
    x = {1,2};
    x[2]=3;
    I am surprised that this works at all in C++0x. As far as I know initializer lists are only available during initialization, the idea that you can assign to an initializer list is new to me. I'd like to hear from others about this who know more than me.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by manasij7479 View Post
    Why does it happen?..
    Because it makes absolutely no sense to assign an array of values to a pointer?

    Pointers and arrays behave similarly in many ways but they aren't the same thing.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by manasij
    also is there a way around this ?..i.e. I want the array dynamically allocated....
    also Does the STL define such a container to make something similar possible?(in that case I'd like to assign ..say..3 arbitrary values to three of its elements in a single statement. )
    For dynamic array use vector<>
    To assign three values in one statement use a function, like
    Code:
    int ass3values(vector<int>& v, int a, int b, int c)
    {
        v.push_back(a);
        v.push_back(b);
        v.push_back(c);
    }
    You can of course design this much better and use a variable number of parameters, but in any case the point is that you have to implement this yourself.

  5. #5
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Interestingly, g++4.4.5, g++4.5 accept the assignment using the initializer list, but g++4.6 does not. Just goes to show C++0x support is experimental. To clarify, this is the code I'm talking about:

    Code:
    #include <iostream>
    
    int main()
    {
        int x[3];
        x = {1, 2, 3};
    
        std::cout << x[0] << std::endl;
    
        return 0;
    }
    g++4.6 gives this error message:
    Code:
    initializerListAfterInitialization.cpp:6:17: error: assigning to an array from an initializer list

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by brewbuck View Post
    but they aren't the same thing.
    Is there a difference when I create a pointer and dynamically allocate it three blocks of integer sized memory ?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by manasij7479
    Is there a difference when I create a pointer and dynamically allocate it three blocks of integer sized memory ?
    Yes, the difference between x being an array and x being a pointer.
    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

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by C_ntua View Post
    For dynamic array use vector<>
    I thought about using it ..but someone told me that it adds much overhead(Is it true?)..and the case where I would need this is inside a much used loop.. |..during the parsing of the input, it puts relevant information inside the array depending on the parts of the input...So Would using vector<int> be a huge problem ?

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is overhead in doing anything. Better to try it and see if it works as desired. If you can then objectively determine that it doesn't work as needed, then find another technique.

    The only way to sensibly manage "overhead" is to define what you mean by "overhead", then specify how much overhead is too much, and lastly find a reliable way to measure that overhead. Generally, if you can't specify all those things, any attempt to minimise overhead is better described as "premature optimisation" and is not worth doing.

    In other words, see if you can get your code working using vector<>. That will not be particularly problematical. If you can then determine that it has too much overhead, you know you need to do something else.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by laserlight View Post
    the difference between x being an array and x being a pointer.
    Could making an array to assign the values and then assigning the array to the pointer be a good idea ? (I can't check because my code is not in working state now.)

    Better to try it and see if it works as desired.
    I would try to implement it using vectors.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by manasij7479
    Could making an array to assign the values and then assigning the array to the pointer be a good idea ?
    It depends on what exactly you are trying to do.
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I'm trying to put a number(not predetermined) of integers to a int* which is a member of a class,
    without resorting to the creation of a huge array(as suggested by the 'source' of the problem) and using the amount of memory necessary from within it.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Then use a std::vector<int> as suggested earlier.
    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

  14. #14
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Already did ..but was looking for something our teacher can't complain about..though the fact that I'd provide a working binary would be enough to end the problem ..!..

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    I thought about using it ..but someone told me that it adds much overhead(Is it true?)..and the case where I would need this is inside a much used loop.. |..during the parsing of the input, it puts relevant information inside the array depending on the parts of the input...So Would using vector<int> be a huge problem ?
    Vector's push_back function has amortized constant time. That basically means that on average, it will take constant time. Thus, the overhead is negligible to dismiss it outright (unless you really know you need the speed).
    Hence, in these situations, you put it in, and see if it still runs quick enough. If not, you identify the bottleneck (could be the vector, or not), then optimize that by either optimizing the usage of the vector or switching to another algorithm/data structure.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. One more linked list implementation
    By BlackOps in forum C Programming
    Replies: 17
    Last Post: 07-16-2009, 09:34 PM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread