how to create structure array with dynamic size [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to create a structure of array dynamically but i dont know the exact size so it will be added whenever i want...
Consider the following example..
Struct abc
{
double **ptrPoints;
int size;
};
i am defining pointer variable
abc* obj;
i dont know the exact size will be so i can not defile like
obj = new abc[size];
the elements will be added whenever condition satisfied.. i want it like vector but i dont want to use it ....
Please suggest me any way to write the functionality like this...
Thank u

Look up vector. Does all the leg work for you.

Wonder why you do not want to use vector. But you may initially declare array of sufficient size. Then maintain a counter of the elements being used up. If it has no space then use realloc.
Seems the only solution except vectors.
See THIS for realloc

Related

How do we make our C++ programs fix array index out of range by themselves? [closed]

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 want to use some C++ code which is not written by me.
However, there are a lot of array index out of range in the code.
Moreover, the lengths of arrays may be not fixed; for example, they may be determined by the size of the input image.
I do not have enough budget to fix them manually, so I ask this question here.
I hope a[i] can be a[0] if i<0 and a[a.Length-1] if i>=a.Length, but I can keep the code a[i].
How do I make it?
You might want to try a wrapper class that is initialized with the original array and then uses an operator[] to behave as you need.
You could write a (templated) class that wrapped an array and overloaded the [] operator to perform bounds checked access to the underlying array. You could then use this class instead of normal C arrays.
How workable this will be will depend heavilly on how the application uses the array. If the array is a gloabl variable or part of a structure/class and is only ever accessed by [] then it will work great but if the array is passed arround by "degrading" to a pointer (and note that array parameters are really pointer parameters) then more work will be needed, changing parameter types and possiblly creating a seperate "checked array reference" class to be used in addition to your "checked array".

If you use new to allocate space, when must you free it? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
We created the binary tree structures in class which used new to create tnodes. I have to write freetree function for it.
I just have general question. If you use new to allocate space, when must you free it?
When you don't need the allocated space anymore.
struct Data { ... }
...
Data* data{new Data};
data->something();
doSomethingWithDataPtr(data);
delete data;
Obviously this example is very simple, but deciding when to delete allocated space is completely subjective... just make sure you eventually delete it. (Consider using smart pointers instead of new and delete to avoid mistakes).

Is there a "generics-like" feature in C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to create a list class in C++ similar to the lists in Java. Is there a way I can have it be able to list whatever object it wants to? The class resizes arrays to create the list, but what I need to do is find out the kind of object that's needed to store.
Yes, C++ has templates that can be used to create generic containers roughly similar to Java generic containers.
While your immediate reaction might be to assume that std::list is similar to a Java list, that would be a mistake. In Java, a list basically just means a sequence. In C++, a std::list is a linked list (which is rarely useful). Most of the time you want to use an std::vector (which is more like Java's ArrayList).
Yes, there is, and it's called Templates

Java ArrayList equivalent in C++ for Tizen [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have experience in Java, but not in C++ and unfortunately I have to write small application in C++ for Tizen. The problem is I have to store data as follows:
data should be stored in one object
ideal object would be java ArrayList (or LinkedList) of ArrayList of Points
How can I achieve that in C++?
Could you propose any sample declaration, definition and get(), add() examples? Is the following a good way to do that:
std::vector<std::vector<Tizen::Graphics::Point> > __strokes;
Use the std::vector class from the standard library
std::vector is a sequence container that encapsulates dynamic size arrays.The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets on regular pointers to elements. This means that a pointer to an element of a vector may be passed to any function that expects a pointer to an element of an array.

Data structure to represent a solution of 2D material cutting? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am working on the cutting problem, and I need to figure out how
to represent the solution.
For example look at this image, where the gray areas are unused material.
Can you please recommend me possible representations? By the way I am using c++ for this.
Thanks
You could use a vector of structs std::vector<sub> areas; like
struct sub
{
size_t x, y;
size_t extent_x, extent_y;
sub (void) : x(0U), y(0U), extent_x(0U), extent_y(0U) { }
};
Where (x,y) as well as (x+extent_x, y+extent_y) are mapped on the Points of the total image.
This vector may either store used or unused parts of the Image.
The 2D image looks like a system memory. Gray area is un-allocated memory and white space is allocated memory. The solution can be similar to memory management done by OS.