Thread: [Dev-C++] 'extern const' and std::vector
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: David M. <ci...@ya...> - 2003-05-01 13:10:10
|
My first question concerns the extern keyword. If I have a varible in one file (it is not a constant) but that varible needs to accessed from the functions in another file. However these other functions do not need (actually I don't want them to have) write access to this variable, can I do something similar to 'const <var_type> <var_name>?'. For those of you who are wondering why I don;t just try it myself I would like to know what EVERY ANSI compatible compiler will do. The program I am writing must remain portable. My other question concerns the std::vector container class of the STD. As far as I have seen the std:vector class stores it's contents contiguously in memory (just like an array). Does this make vector classes reach their maximum size once there is no block of memory big enough to hold it's data? I plan to use this class to act as the index for world system with up to 2^32 rooms in it (even if I store only pointers to each structure it still gets pretty big). Would any of the other containers be better suited to this purpose? I chose the vector bacause of it's speed (4 billion possible rooms, gonna need the speed). The requirements for the container is to allow for a numbering system for eah of the rooms with rooms being grouped within ranges. --------------------------------- Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. |
|
From: Per W. <pw...@ia...> - 2003-05-01 14:39:11
|
I'm not too good at the latest updates of the standards, but to the best of my knowledge, there is no const extern declarations. Extern is just a keyword to inform the compiler that somewhere in another linking unit, the specified variable or function exists. However, you can not just lie, because when you reference the symbol, the compiler will insert a name-mangled reference and the linker will then fail if an exactly identical reference isn't available somewhere in one of the included object or library files. Because c++ uses name-mangling, you can't just change the declaration of a variable in your extern statement. There might be C++ compilers that doesn't change the name-mangling for a const contra non-const variable. In that case, this scheme would work for that compiler. However, I'm pretty sure that you would have to search a lot for a compiler with such a name-mangling. If, on the other hand, you declare the variable as "C", you can do just about anything, i.e. you might declare the variable as char in one source file and as unsigned long in another. Then it's up to you to figure out if the program will survive, or if it will go up in a big explosion when you try and run it. About 2^32 elements in a vector - I hope you are running a 64-bit operating system. Even a vector of 2^32 characters would fill the full address space of a 32-bit processor. Ever wondered about the big need of 64-bit processors and operating systems for large database applications? If I assume that you don't need 2^32 elements, just an address range of 2^32, I would suggest a slower (log n) solution using some form of btree or similar. Then you might traverse down the tree until you find the node containing the range, or no match is found. If you don't have to support ranges, but just want to either find an element, or know that it isn't there, go for a hash solution instead. It will perform insert and lookup operations in constant time. /Per W On Thu, 1 May 2003, David McKen wrote: > My first question concerns the extern keyword. If I have a varible in one= file (it is not a constant) but that varible needs to accessed from the fu= nctions in another file. However these other functions do not need (actuall= y I don't want them to have) write access to this variable, can I do someth= ing similar to 'const <var_type> <var_name>?'. For those of you who are won= dering why I don;t just try it myself I would like to know what EVERY ANSI = compatible compiler will do. The program I am writing must remain portable.= My other question concerns the std::vector container class of the STD. As= far as I have seen the std:vector class stores it's contents contiguously = in memory (just like an array). Does this make vector classes reach their m= aximum size once there is no block of memory big enough to hold it's data? = I plan to use this class to act as the index for world system with up to 2^= 32 rooms in it (even if I store only pointers to each structure it still ge= ts pretty big). Would any of the other containers be better suited to this = purpose? I chose the vector bacause of it's speed (4 billion possible rooms= , gonna need the speed). The requirements for the container is to allow for= a numbering system for eah of the rooms with rooms being grouped within ra= nges. > > --------------------------------- > Do you Yahoo!? > The New Yahoo! Search - Faster. Easier. Bingo. |
|
From: Ioannis V. <iv...@at...> - 2003-05-01 15:05:30
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Per Westermark > Sent: Thursday, May 01, 2003 5:23 PM > To: David McKen > Cc: dev...@li... > Subject: Re: [Dev-C++] 'extern const' and std::vector > > > I'm not too good at the latest updates of the standards, but > to the best > of my knowledge, there is no const extern declarations. Why? > Extern is just a keyword to inform the compiler that > somewhere in another > linking unit, the specified variable or function exists. > However, you can > not just lie, because when you reference the symbol, the compiler will > insert a name-mangled reference and the linker will then fail if an > exactly identical reference isn't available somewhere in one of the > included object or library files. If you do extern int x; and you do not define x elsewhere, it is considered a definition. The same applies for extern int x=1; It is considered a definition (extern is ignored). Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net Anti-racism movement: http://www.positive-youthfoundation.org Iraq Peace Pledge: http://www.peacepledge.org |
|
From: Per W. <pw...@ia...> - 2003-05-01 15:16:06
|
Yes, this exists: extern const int x; However, that is a constant. The request - as I read it - was for a global variable to be const in just one file, while writeable in other files. When extern-declaring something (that is already declared somewhere else), you should declare it identical to the original declaration, and it this case the original declaration is not const. /Per W On Thu, 1 May 2003, Ioannis Vranos wrote: > > -----Original Message----- > > From: dev...@li... > > [mailto:dev...@li...] On Behalf > > Of Per Westermark > > Sent: Thursday, May 01, 2003 5:23 PM > > To: David McKen > > Cc: dev...@li... > > Subject: Re: [Dev-C++] 'extern const' and std::vector > > > > > > I'm not too good at the latest updates of the standards, but > > to the best > > of my knowledge, there is no const extern declarations. > > > Why? > > > > > Extern is just a keyword to inform the compiler that > > somewhere in another > > linking unit, the specified variable or function exists. > > However, you can > > not just lie, because when you reference the symbol, the compiler will > > insert a name-mangled reference and the linker will then fail if an > > exactly identical reference isn't available somewhere in one of the > > included object or library files. > > > If you do extern int x; > > > and you do not define x elsewhere, it is considered a definition. The same > applies for > > extern int x=1; > > > It is considered a definition (extern is ignored). > > > > > > Ioannis Vranos > > * Programming pages: http://www.noicys.freeurl.com > * Alternative URL 1: http://run.to/noicys > * Alternative URL 2: http://www.noicys.cjb.net > > > Anti-racism movement: http://www.positive-youthfoundation.org > Iraq Peace Pledge: http://www.peacepledge.org > > > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Ioannis V. <iv...@at...> - 2003-05-01 16:27:11
|
> -----Original Message----- > From: Per Westermark [mailto:pw...@ia...] > Sent: Thursday, May 01, 2003 6:15 PM > To: Ioannis Vranos > Cc: Dev-C++ Mailing List > Subject: RE: [Dev-C++] 'extern const' and std::vector > > > Yes, this exists: > extern const int x; > > However, that is a constant. The request - as I read it - was > for a global > variable to be const in just one file, while writeable in other files. Ok then I recall. :) It sounds like he is doing a bad design though. > When extern-declaring something (that is already declared > somewhere else), > you should declare it identical to the original declaration, > and it this > case the original declaration is not const. Yes. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net Anti-racism movement: http://www.positive-youthfoundation.org Iraq Peace Pledge: http://www.peacepledge.org |
|
From: Ioannis V. <iv...@at...> - 2003-05-01 15:02:42
|
-----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of David = McKen Sent: Thursday, May 01, 2003 4:10 PM To: dev...@li... Subject: [Dev-C++] 'extern const' and std::vector My first question concerns the extern keyword. =20 If I have a varible in one file (it is not a constant) but that varible needs to accessed from the functions in another file. However these = other functions do not need (actually I don't want them to have) write access = to this variable, can I do something similar to 'const <var_type> = <var_name>?'. For those of you who are wondering why I don;t just try it myself I = would like to know what EVERY ANSI compatible compiler will do. The program I = am writing must remain portable. =20 =20 You do not specify language. That creates extra pain. In C & C++ yes you = can do extern const int x; In C++ you have better use a namespace. =20 My other question concerns the std::vector container class of the STD. =20 As far as I have seen the std:vector class stores it's contents = contiguously in memory (just like an array). Does this make vector classes reach = their maximum size once there is no block of memory big enough to hold it's = data?=20 =20 You haven't to worry about these details. It is an implementation worry = and in reality the vector is just a handler interface. That's why the vector::swap() works instantly. Also you can specify for it your own = memory allocator. But handlers and allocators are advanced magic for me yet so = i can't tell you more on that till i end TC++PL3. :) =20 =20 In any case you have not to worry about these details. =20 =20 =20 I plan to use this class to act as the index for world system with up = to 2^32 rooms in it (even if I store only pointers to each structure it = still gets pretty big). Would any of the other containers be better suited to = this purpose? I chose the vector bacause of it's speed (4 billion possible = rooms, gonna need the speed). The requirements for the container is to allow = for a numbering system for eah of the rooms with rooms being grouped within ranges.=20 =20 Regarding constant random access with [] time cost: vector, deque, = string, valarray and bitset have this. =20 =20 Reference for all the time costs for all containers for all types of = access ([], front operations, etc): =20 "The C++ Programming Language" 3rd Edition or Special Edition by Bjarne Stroustrup (the creator of C++): =20 On page 464. =20 =20 =20 =20 Ioannis Vranos * Programming pages: <http://www.noicys.freeurl.com/> http://www.noicys.freeurl.com * Alternative URL 1: <http://run.to/noicys> http://run.to/noicys * Alternative URL 2: <http://www.noicys.cjb.net/> = http://www.noicys.cjb.net Anti-racism movement: <http://www.positive-youthfoundation.org/> http://www.positive-youthfoundation.org Iraq Peace Pledge: <http://www.peacepledge.org/> = http://www.peacepledge.org =20 |