Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am writing a program which reads from a file and processes the data in the file. Each line in the file is an entity. There are 85620 lines in the file. Is it efficient to define a class of the entity and have 85620 instances of that class?
Depends a bit on the class. But in general, 80k objects is a non-concern.
Yes, especially if you're storing them in a memory-efficient container like std::vector (hint: reserve some space up front if you know you always need at least a thousand or so).
It depends on a lot of factor, and the target software/hardware requirements.
On modern computer with 8GB+ memory, 80k object with even 1K each would not be a big deal, however it may be an issue on mobile phone or embedded systems.
Also note that if you are doing single pass processing and do not need the data afterward, there is no reason to store them.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
I have a problem where I need to sort buses arriving at a bus station on the basis of time of arrival without using STL (standard template library) in ascending order
You may first want to read about sorting algorithms in general. A good staring point is here.
There you see many of them.
The recommendation for newbies is to start with bubble sort.
Please see here for an example including source code.
Then, you need to store your bus data in a struct. Along with the timing information. All those struct shoulb be stored in an array, best a std::vector.
Then you need to write a compare function for times. The complexity of this depends, if you have one varaible that stores the complete time, like in a unix timestamp, or in a struct, for example tm. Then you need to compare hours, minutes and seconds and some boolean relation.
But first, you need to read a lot, then think even longer on how to implement, and then write the code.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Just wondering, what would be the most efficient way to write same data to two files, on linux and C/C++.
For example, this is the most trivial way.
while(1) {
... getting data from somewhere ....
write(fd1, data, datalen);
write(fd2, data, datalen);
}
However, the disadvantage is that kernel needs to copy data twice even though the data is same.
Any thoughts?
what would be the most efficient way to write same data to two files
Write the data to one file only.
Copy that file to another. Use an OS call to do that efficiently (Copy a file in a sane, safe and efficient way).
Another way for step 2 would be to create a hard link (check link()).
However, please watch out of not becomning a victim of premature optimization. I this is not the bottleneck in your program, then just use the trivial, easy-it-read approach.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is basically what i am now using a lot in my application for accessing objects that needed to be accessed from many classes:
Application::getInstance()->getComponentList()
Still think that this is not the good way how to access objects that need to be shared among many classes.
Question is if there is better approach to share objects in big application.
I have to agree with you and juanchopanza: accessing objects via a single singleton object throughout an entire project is a poor practice since modules are supposed to have few and well-stated dependencies among them (ideally: interfaces or means of communication).
Also: it's easy to violate the single responsibility principle.
There's no "do X instead of the singleton approach" solution, or no silver bullet: sometimes a singleton can really be useful but if possible: avoid it and design a clear interface instead.
Also: there isn't enough information in your question to state something more specific.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to pass a pointer to an array from my app to a console exe program?
The scenario is: my app needs for certain cases some extra things to be done to the content of the array which is done by the EXE. So if its possible to send a pointer to the EXE and return it after modification ?
To exchange data between separate processes, you need a means of inter-process communication.
Options include:
shared memory
pipes
possibly even sockets
Which of the above is appropriate for you depends on the type and amount of data you want to exchange, how frequently you want to exchange it, etc.
You can do so by using the execv*() functions to start the new process from the process which provides the array, but are limited to an array of character pointers (char *).
No. The parameters given to a new process by the operating systems are an array of strings. You can neither replace that array nor make one of the strings an array.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following problem:
I have a configuration file that consists a description of fields , which I read it and then parse it. I want to move it into the code to compile it inside.
How would you do that as bug structure ??? or else ?
Thanks
I wouldn't move it into the code, I'd leave the configuration file as a configuration file.
If you really must do this, you can just embed the file as a string resource into the application and use that - that way you'd change only a minimal amount of existing code. The way you do this depends upon your platform.
If thats not feasible (for whatever reason) I'd set up a single configuration class / namespace to contain all the values.
It's not very clear what are you exactly asking.
If you are looking for on-the-fly code execution (like eval() function in some languages), then there is no such thing in C++. It's not an interpreted language which can be read and executed line-by-line, it needs to be compiled every time code changes. While it technically is possible to write self-changing code, it's probably not worth the effort.