c++ default array size override with command line arguments - c++

I saw one question on similar lines
Specify Array from Command Line Argument
Although my problem is bit different.
I have multiple files implementing arrays of same size (NOC_SIZE). My program has a default mode and a user mode (command line arguments). I gave the 'unsigned int NOC_SIZE = 16;' line in my code before the start of main function. In another header file I declared a struct (noc_package) with parameter 'static unsigned int NOC_SIZE;'. This header file in included in all files where ever required.
But in the files where I have declared an array (int arr[noc_package :: NOC_SIZE]) it gives an error saying array bound not an integer.
Can somebody suggest a way around this?
Thank you.

C++ doesn't support variable-length arrays.
You're better of using an std::vector instead:
std::vector<int> arr(noc_package::NOC_SIZE);

Related

Declaring/defining c++ struct in different file - better compile time using gcc 4.8.5?

I currently have a c++ code and a Json file. Json file contains enumerations in 2-D structure, so every outer key in Json has a map as its value i.e {Outer_key : {{Inner_key : Inner_value}, ...}, ...}. C++ code contains overloaded print function which parses the input data, and in the process of the function call, the code fetches Inner_value using Outer_key and Inner_key. For each call of the c++ main function, around 0~10 Inner_values are retrieved; however, the entire Json file maps about ~20,000 Inner_values.
I am using python to create the c++ code, and am compiling using gcc (CMAKE). I need to keep some kind of enumeration map within the body of c++ so I can run c++ code, get intermediate integer value and pass it into enumerations to finally return the associated string.
Right now, I list-initialize a 2-D unordered_map in the main function of the c++ file. This takes the shortest time among all the other compile-time initializations; however, it still takes 5~10 minutes.
On suggestion I received is to divide the 2-D enumerations into multiple(total number of Outer_keys) 1-D structs, store them in a different file, then 'use' a specific 1-D struct when needed.
Two questions I have here.
Even if I divide them up, and put them into different files, doesn't the time to compile remain the same?
If the compile time is reduced by splitting up in multiple 1-D structs, what approach should I take in coding this? Should I declare structs in .h then call them in .cpp main()? Should I go ahead and define the structs in additional .cpp file? Should I just typedef enums? Also, within the main function or the print function, how can I initialize only the struct that I need?
.cpp file generated using python below:
void overLoadedPrint (Particular_Datatype *data, std::unordered_map<std::string, std::unordered_map<std::string, std::string>> enumMap) {
printf("%s", enumMap["SomeKey"][A->member1.innerMember1].c_str());
//A->member1.innerMember1 returns integer.
//"SomeKey" is known in python so corresponding key is inputted.
}
int main() {
std::unordered_map<std::string, std::unordered_map<std::string, std::string>> enumMap = {{"A", {{"1", "a"},{"2", "b"}}...}
//list-initalize enumMap.
//compile time significantly increases here.
//info of this map is stored in a single json file.
overLoadedPrint(someData, enumMap);
return 0;
}
Even if I divide them up, and put them into different files, doesn't the time to compile remain the same?
Splitting a translation unit into fragments usually increases compilation time from scratch.
However, each translation unit can be compiled separately, so if you change only one, then only that file need to be recompiled. Having to compile a fraction of a program is usually much faster than compiling it entirely.

C++ const array appending

I have this program which consists of a loader and different statically linked modules. The entrypoint of the program goes into a loop which executes all the module initialization-routines. The function-pointers to these routines are stored in a constant array in the main.cpp file.
My problem is that everytime I want to add or remove one of the modules I have to modify the array.
I feel like there should be a method of automating this process. Is there a way to let the preprocessor load and parse a text-file holding the function-names? Or can I append new entries to the constant array from another .cpp file? How do I generate this array at compile-time?
Thanks in advance!
If the list of modules is known in compile time, while do not simple list them in makefile?
For anyone who cares. I have actually solved my problem. Turns out that gcc has a feature
called 'section' which lets you create static memory regions that can be extended multiple times
during compile-time by just specifying a declaration with this attribute. This created memory region
packs the inserted elements side by side zero terminated, which gives it the characteristics of an array sorta. The memory location, lets call our section "name", can be accessed by defining extern const char pointers named __start_name and __end_name. Thanks gcc. :-)

C++ (gcc/g++) Two huge arrays of strings take extremely long to compile

For a program written in C++, I need two huge arrays of strings that contain data.
They are defined in a header file as follows:
#include <string>
static const string strdataA[30000]={"this is the first line of the data",
"the second line of data",
"other stuff in the third line",
down to
"last line."};
//second array strings
static const string strdataB[60000]={"this is the first line of the data",
"the second line of data",
"other stuff in the third line",
down to
"last line."};
But when I compile this with g++, it takes so long that I have not seen it complete. It also uses about two GB of virtual memory. So I commented out strdataB[], and then the program did compile, but still after a long while. The executable was only about 8 Mb and worked fine.
What can I do in order to speed up the compiling process? I don't mind if I have to change the code, but I don't want to use an external file to load from. I would like an array because it works extremely well for me inside the program.
I read on the net somewhere that "static const" should do the trick, but I learned by experience that it doesn't.
Thanks a lot in advance for any suggestions!
You should not use std::string for that. Use instead plain old const char*:
const char * const strdataA[30000] = {
"one",
"two",
//...
};
The static keyword shouldn't make much of a difference here.
This way, the strings themselves will be stored in the read-only data section as simple literals, and the array itself will be simply an array of pointers. Also, you avoid running the strings constructors/destructors at runtime.
I believe these are known issues in GCC. You do not say what version of GCC you are using, maybe you should try with the newest stable release of GCC, to see if it does or does not improve things.
You probably should not keep all of your string in source code any. You should probably load them from external file at startup or such.
What can I do in order to speed up the compiling process?
const char* strdataA ... should speed up the compilation process. Because in your current version g++ must create huge list of constructor calls for every single string.

C++ declaring variable inside if statement

I have a really basic problem in c++, I'm reading a tab separated file and I want to declare an array with the dimension if the number of fields the file has (work with different files with different widths) so I need to read the first line and count the number of fields, I tried this:
while(getline(t, line));{
...
if(!flag)
{int array[size][5];
flag=1}
...
}
But then I get the error:
error: 'array' was not declared in this scope
I understand it is because the scope of the variable is in the if loop, is there any way to declare a null array and resize it? Or will I have to use pointers?
The size of an array must be a compile-time constant. Use a std::vector if you want a dynamically-sized array.
Other issues with your code:
Remove the semicolon after the while, or your loop body will only be executed once after the whole file is read.
Add a semicolon after flag=1.
You are writing C++, so why not use a std::vector<std::vector<int> >? If possible (here it is) try not to use raw pointers.
Pointers will be the way to go...
int array[size] possible just in C99.
In C++ plain-C array sizes should be compile-time constant.

Is it possible to create a struct type based on file contents?

Is it possible in C++ to create a struct type based on file contents?
STRUCT_NAME
int var1;
int var2;
string v3;
STRUCT NAME *pointer;
The first line would be the name of the struct, the other lines would be the variables
Do you mean on the fly, when your application is running? No, you can't do that, all types must be known at compile time. Of course, you can always create some kind of container object that could be configured dynamically during run-time, but that is a much more advanced system.
On the other hand, if you mean to do this once when you build you application, you could write a simple tool that eats your text file and emits a C++ header file, that you later could use when compiling.
If you are reading from a file and trying to create this struct,
--> if in same program, i.e. runtime then NO; because you can not compile the source code which is presently executing
--> for some different source code which is yet to be compiled and you are manipulating using file operations then YES
No. The closest you can come is an std::map<std::string, boost::any> (or boost::variant if you can limit the set of types).
Whatever #lindydancer #iammilind #james are saying is correct.. You cannot do it in single go. You may get work around (Though thats not professinal way of doing) like this :
1. First read the file in which your structure is define and create new file(Cpp and h files).
2. Now compile new file and provide new exe file to your user, you can do this process in background so that for user its all dynamic.. (But atlease someone has to do this work...)