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 7 years ago.
Improve this question
I am beginner in oop in c++ fairly say in programming. i am trying to make the array of objects and pass it to member function sort(class_name[size]) to sort by their id number.
i have tried so hard for passing arrays of objects but cant find the solution.
please help on it.
Thanks in advance.
Classically and erroneously, arrays have been passed by:
Name of the array
Address of first element
The biggest issue is the length (capacity) of the array. In C and C++ the array (not std::array), does not store it's capacity. So the receiving function has no clue to the end of the array.
Another issue is the number of elements in the array. In C and C++, the array structure does not maintain the number of items in the array. An array may have a capacity of 32 elements, but only 3 loaded.
If you write your own functions and use an array, please provide arguments for the array, the capacity and the number of elements in the array.
Edit 1:
One problem with passing a pointer or the address of the first element is that the pointer only points to a single character, and technically, not an array object. The assumption has been that an array is a container where the elements are consecutive with no other data types between. Thus you only needed to know the first position of the array.
A problem is that I could pass a pointer to a single character and the receiving function would treat the data as an array of characters instead of a single character. Thus creating undefined behavior often known as buffer overruns.
So to be more type safe, you should use the array syntax when passing arrays rather than a pointer to the first element. For better safety, using std::vector or std::array or put the array in a structure and pass the structure.
Related
This question already has answers here:
how to set or initialize default value for all elements of a table or 2d array or multdimentional array
(4 answers)
Closed 3 years ago.
I have two dimensional char array in c++ and want to give all element one value without using loops
is there any function to achieve it ?
char demo[5][7];
memset(demo, '\0', sizeof(demo));
Based on the provided info, does this do what you want?
Since you're using C++ I'd question to use a native char array, as long as you're not required to and/or needing to use the stack.
Otherwise its idiomatic to use the collection classes provided by the STL.
Adding to the answer as requested, this obviously does what the question asks for: initializing the memory of a char array.
Please see the docs for more information on memset: https://en.cppreference.com/w/cpp/string/byte/memset
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 5 years ago.
Improve this question
I created my own flowchart and I am following that. What stands as a problem for me is that I cant keep different data type at different places(for eg. the array is arr[4][4] then in arr[3][1] and arr[4][3] I want char and rest int)
What I want to try is in a multidimensional array, the first row, last row, first column, last column 4th and 8th both row and column to store '*' and rest numbers
I'm not sure if it is possible or not with templates or by any method.
What stands as a problem for me is that i cant keep different data type at different places(for eg. the array is arr[4][4] then in arr[3][1] and arr[4][3] i want char and rest int)
An array has one element type. All its elements have that type, no other.
However, char is an integer data type, and every C++ implementation you are likely ever to see can accommodate every possible value of a char in an object of type int. Therefore, it is safe, in practice, to store the value of a char in an array element of type int. If you have done so and not subsequently modified that array element, then you can also read it back and store the value in a char.
(Do note, by the way, that there is a big difference between one char and an array of char, such as C uses for its strings. We sometimes see confusion about that around here.)
More generally, though, you should be choosing appropriate data types. If you have hetergeneous data that you want to treat as a unit, then you should be declaring a class to contain it, not using an array for that.
This question already has answers here:
How do I find the length of an array?
(30 answers)
Closed 6 years ago.
I have a function like:
function(double* numbers)
I know that if I know the size of the numbers array, I can then do
double* numbers = new double[size];
function(numbers)
This will return the value in numbers correctly, however, now I don't know the size of numbers, I still want to use function function, which is a third party library function which I don't want to change, is there a way to get the array out without knowing the size of array?
No, there's no standard way to retrieve the size of the array at run time from a mere pointer. I suggest you either use std::array for fixed-size arrays, or std::vector for variable-length arrays. An less safe alternative would be to pass the size of the array together with the pointer.
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 7 years ago.
Improve this question
I know in java an array can be declared as
int[3] values;
or
int value[3];
Is both form valid in c++ as well?
thanks in advance for any help!
No. Only the second one is valid in C or C++.
In C++ you should preferably use std::array (or similar type) for a fixed size array, like this:
array<int, 3> values;
For a dynamic size generally use std::vector (or similar type), like this:
vector<int> values( 3 );
The declaration int[3] values is invalid in C++, but you can write
template< class Some_type >
using Type_ = Some_type;
// ...
Type_< int[3] > values;
C++ arrays constitute a large hole in the C++ type system, because in a context where a pointer is expected a C++ array expression decays to a pointer to the first item. Java arrays constitute a smaller hole in the Java type system. Still it's possible to get an exception (run time error) in Java due to incorrect type for an array item. Which seems to indicate that arrays are difficult to get right in any language. C++ raw arrays are, however, among the worst language designs for arrays, so better use the alternatives discussed above.
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 am using a code that someone else wrote for calculating chemical reactions. The user must specify many values for a calculation and this can lead to mistakes. I am trying to automate/simply this process.
I can instantiate a class by doing (for example):
Algorithm<double> chlorine;
I would like to do multiple instantiations--for example, chlorine, hydrogen, and oxygen. I don't understand why I get a segmentation fault when I put "chlorine," "hydrogen," and "oxygen" as elements in a vector of strings called "chemicalElements"and then do:
for (i = 0; i < chemicalElements.size(); i++)
{
Algorithm<double> chemicalElements[i].data();
}
Am I missing something simple here? When I write:
Algorithm<double> chlorine;
"chlorine" is just a string, right? So why would it not work to add "chlorine" from an element in a vector of strings?
chlorine is not a string in your example code, it's an identifier for a variable (of type Algorithm<double>).
Variables must be given compile-time identifiers; that means the identifier must be specified when the compiler is traversing your code. The result of chemicalElements[i].data() is unknown until runtime.
C++ doesn't have any facility for creating variable names at runtime, so you cannot do what you are directly asking. However, it sounds like what you really need is a collection of algorithm objects, one for each of your elements. To create an array of algorithm objects, you can do:
Algorithm<double> algorithms[15];
This creates 15 distinct algorithm objects, which you can map to your elements however you like. You can of course choose a different number than 15, so long as that number is a compile-time constant value.
You may also be interested in learning about std::vector<T>, a type that allows you to create dynamically-resizing arrays, or std::map<K,V> which allows you to create an associative mapping between a key value (a string, such as "chlorine," and a value, such as the associated algorithm).
To use the latter, you can do something like this:
std::map<std::string, Algorithm<double>> algorithms;
algorithms["chlorine"] = Algorithm<double>();
algorithms["argon"] = Algorithm<double>();
and then later:
auto results = algorithms["chlorine"].data();
(You should of course peruse the linked documentation on the above types, since I am omitting some error handling for brevity.)
Algorithm chlorine , means that
You've instantiated an "Algorithm" object named "chlorine"
to make array of "Algorithm"
you code it like:
Algorithm<double> chemicalElements[Const_num];
and to pass through each one of its items you call the array's name + it's index like:
chemicalElements[0 or 1 or 2 or ... etc].data();
So it would be like
for (i = 0; i < Const_num i++)
{
chemicalElements[i].data();
}
In this statement
Algorithm<double> chlorine;
chlorine is not a string. It is an identificator that names an object of type Algorithm<double>.
This construction
Algorithm<double> chemicalElements[i].data();
has no syntaxical sense in C++ and the compiler shall issue an error.