Brace-enclosed initializer list in an assignment

This is a discussion on Brace-enclosed initializer list in an assignment within the C++ Programming forums, part of the General Programming Boards category; I found out accidentally that the following code is only supported in the c++0x standard...thus giving a gcc option "-std=c++0x" ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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. )
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    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.
    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

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    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%.

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    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.
    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

  12. #12
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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

  14. #14
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 ..!..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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.

Page 1 of 2 12 LastLast
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, 08:01 AM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 08: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


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