C++ :: What Is Default Size Of STL Vector
Jan 30, 2012For example, I have an empty vector of integer. If I keep calling push_back on vector, is it going to be out of memory?
View 8 RepliesFor example, I have an empty vector of integer. If I keep calling push_back on vector, is it going to be out of memory?
View 8 RepliesFor my default constructor I need to set an array of strings to null and the size variable to 0. Here is my code for that
DynamicStringArray::DynamicStringArray() {
*DynamicArray[]=NULL;
size=0;
}
When I try and compile it I get an "expected primary expression before ']' token" error.
My program enters the size of the vector from the user and then creates a vector of vectors (lets say SIZE1). In addition the user enters the number of vector of vectors he needs (lets say SIZE2) as follows:
class Vectors {
// member functions goes here
private
vector<vector<int>> vectors;
vector<int>::iterator it;
[Code] .....
With a few calculations and insertions to my vector (vector of vectors)... the program works fine and gives me the results...
However, with huge calculations and insertions the program stops working and gives me this message
"Unhandled exception at at 0x770DC41F in Test.exe: Microsoft C++ exception:std:bad_alloc at memory location 0x001CEADC"
Thus, it seems that the vector reached it's maximum size... I tried to use reserve() but did not work
I read that "By default, when you run a 64-bit managed application on a 64-bit Windows operating system, you can create an object of no more than 2 gigabytes (GB). However, in the .NET Framework 4.5, you can increase this limit"
What do you think would be the best option for me to do (note my program is very long and complex)(I'm currently using Microsoft Visual Studio 2012 32Win application):
1. convert my program to the .NET Framework (C++)
2. convert my program to C# in case c#
3. do any settings on my computer (my workstation has a 3.6GHZ xion processor with 32RAM
4. convert to another version of C++ that does not have any restriction on the size of the array (if available)
Please note that I never worked neither with the .NET framework nor C#
I had a question about memory allocation/how iterators work for a std::vector<foo> of a user defined class 'foo'. Say foo contains variables of variable size, so that each member of the std::vector<foo> does not require the same amount of memory space.
Does c++ allocate the same amount of memory for each element, equal to the amount of memory required for the largest element? Or does it use some sort of array of pointers pointing to the location of each element in the vector to make the iterator work? Or does it use some other method? I am wondering because I wrote a code which reads data from a binary files and stores most of it in std::vectors.
The code seems to be using significantly more memory than the sum of the size of all the binary files, and I am using vectors made up of the datatype within the binary files (float). So I was wondering if internally the code was allocating space for each vector element which is the size of the largest element as a way to handle indexing/iterators. I ran my code through a memory leak checker and it found no errors.
I'm trying to do is write a program that fits to a separate test program. The test program provides different size vectors that my function should try and binary search. If the element is found, the function should return 1, and if the element is not found, it returns -1.
Here is the code:
int binSearch(const vector<double> & data, int elem, int & comps) { {
int beg=data[0];
int end=data[data.size()-1];
int mid=(end+beg)/2;
[Code] ......
The problem is that one of the vectors my function is supposed to binary search is a vector of size 0. I tried to throw in an if statement that would return -1 if the size was == 0, but then the program never fully completed and just kept running. So, how can I account for a size 0 vector in my function?
I am writing a raytracer, and currently I'm working on creating a bounding volume hierarchy to accelerate the process. To do this, I am first creating a vector that holds each of the objects in the scene, and passing this vector to the constructor for my BVH.
Code:
//in header
BVH_Node* bvh;
//in main raytrace function
[Code] .....
I am testing a scene that has only 2 objects, and so it goes to the size == 2 check. The first time it hits makeLeaf(), I segfault. I've used both gdb and valgrind, and of course it's a memory mapping error. gdb's backtrace tells me that the length of the vector I've passed in is -805305610 and the capacity is -21, and that it is inside my makeLeaf() function that the error occurs.
Here's the function:
Code:
BVH_Node* BVH_Node::makeLeaf(GeomObj* v){
BVH_Node* node;
node->obj = v;
node->isObj = true;
return node;
}
The segfault happens at
Code: node->obj = v;
If I run my raytracer without a BVH, the objList works perfectly.
Getting back into programming after a few years off and a bit rusty.
My question is: Is this going to initialize the size of the vector array's position and color properly?
#include <GLFW/glfw3.h>
#include <vector>
class TerrainClass {
private:
struct VertexType {
std::vector<float> position[3];
[Code]...
I want to save the char[8][8] // fixed size 2 dimension array
to a vector
such as
vector<?????> temp;
is there anyway to approach this?
I am trying to use an array of a size that will be determined by the user, therefore I must use a vector, right?
In class I was told that this is how I call a vector:
vector <int> x;
Is the vector called vector? Is it called x?
Can I do this?
for(int i=0;i<=10;i++)
{
cout<<x[i];
}
Some basic ways of implementing a vector? How it works or how I can do anything with it.
Change the frame window size according to font size increases.
View 3 Replies View RelatedI must take an old MFC project in VC++ 6.0 and make changes.
The problem is text size in screen is different from size in print preview.
for example with this font
Code:
CFont f50;
f50.CreateFont(100,0,0,0,FW_BOLD,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_DONTCARE,"Frutiger LT Std 45 Light");
And this text
Code:
s=_T("Let's try to calculate the size of this text");
and with MM_LOMETRIC map mode
GetTextExtent() returns me:
On screen: (1595,99)
Ink printer + print preview: (1589,100)
PDFCreator + print preview: (1580,100)
comparing with screen size the height is bigger but lenght is smaller. I don't understand.
I can understand that different printers process the fonts in different way and then to have different lenghts. That's not the problem. The problem is I need to simulate in screen the same behaviour i will have on printer because these texts are being aligned in the document, and I don't want to see that the text si aligned different in text than in paper.
What can I do to render the text on screen with the same size I will have on the printer? Print preview is doing it. Should I change the font parameters? is something related with pixels per inch?
I was wondering why, in C, the sizeof of a struct is larger than the the sum of all the sizeofs of it's members. It only seems to be by a few bytes, but as a bit of a perfectionist I fine this a bit annoying.
View 1 Replies View RelatedFollowing is the code snippet
Code:
[COLOR=white !important]?
1
2
3
4
5 char str1[]="Bombay";
char str2[]="Pune";
char *s1=str,*s2=str2;
while(*s1++=*s2=str2);
printf("%s",str1);
Output of this code comes out to be Pune
But according to me output should be puneay.
Pune should be copied in place of Bomb.and rest should be as it is.
[/COLOR]
I have a problem. I want to set pointers to a default value for both 32 bit and 64 bit compiles. The 32-bit version works as:
enum constants { UNDEFINED = 0xDeadBeef }
if ((unsigned long)ptr == UNDEFINED)
but I can't seem to extend this to 64-bits. I've tried
#if __SIZEOF_POINTER__ == 4
enum constants { UNDEFDATA = 0xDeadBeef };
}; // enum constants
#elif __SIZEOF_POINTER__ == 8
enum constants { UNDEFDATA = 0xDeadBeefDeadBeef };
#endif
with:
if (ptr == UNDEFINED)
but get a message saying the '==' is undefined (I understand this)
Is there any way to setup so that I can change the size of my constants so that the comparisons will always work correctly? I've tried a 'typedef' but the compiler complains at
'typedef unsigned long long ADDR' // won't accept, and
static const SlipCellBase * const TEMPORARY = (SlipCellBase&)0xFFFFFFFFFFFFFFFF; // illegal conversion
enum doesn't work (because it's an int?)
First I wrote a Binary-tree class to draw a binary tree on the window. The nodes were small circles. Then I become wanted to change the shape of the nodes from circles to triangles by another class, Binary-tree-derived which is derived from Binary-tree class. I wrote the below code to do the job but I get two errors about constructors. First, code:
/* The binary-tree class, one of the topics that has been said in Programming Principles and Practice using C++ book by Biarne Stroustrup.
This code is written by R Abbasi (s.rabbasi@yahoo.com) */
#include <Simple_window.h>
[Code].....
Errors are:
Error12error C2512: 'Binary_tree' : no appropriate default constructor availablec:userscsdocumentsvisual studio 2012projects est_1 est_1 est_1.cpp91
13IntelliSense: no default constructor exists for class "Binary_tree"c:UsersCSDocumentsVisual Studio 2012Projects est_1 est_1 est_1.cpp91
How to create default arguments in C? Is there any way to make default arguments ( i mean any alternative for them).
View 5 Replies View RelatedHow do you write a default constructor?I need to use a default constructor that will initialize the data members:
- salesPerson to “Unknown”
- noOfWeek to 13
- amount to point to an array of thirteen 0.00s.
This is my weeklysales class
class WeeklySales {
char* salesPerson;
double* amount; // Pointer to an array
int noOfWeek; // Size of the array
};
Here's my attempted code:
//default constructor
WeeklySales (){
salesPerson="Unknown";
noOfWeek = 13;
amount = 0.00;
}
A compiler auto created default constructor is called a synthesized default constructor. It will initialize the built-in members to 0 or not depends on where the class object is defined? if I define a class
class point{
public:
double x, y;
};
if I define point point1; in global scope then point1.x and point1.y will be initialized to 0, if I define point point2; in a local scope, then its x and y won't be initialized? If it is like this, then I believe if there are built-in type members in a class, then the synthesized default constructor is almost useless!
I have an inherited class that essentially manages a Qt Window.
It is as follows (prototype below):
class QTMyOpenGLWindow : public QWindow, protected QOpenGLFunctions {
Q_OBJECT
[Code] ....
Now, I can understand the confusion of the compiler, but the functionality as I laid it out works for me (I can create the class with just specifying the parent and also have the option of preventing auto-initialization when creating). But, is there a better approach?
I am trying to figure out what's the best way to accomplish something like this:
void function(t1 a=0,t2 b=0){/*...*/};
t2 value;
function(value);
That would throw a compile error, since the first argument that is being passed to the function (value) is considered the first argument in the declaration (a), which is of type t1. So, is there a way to force my function to consider value as the second argument instead of the first one? I am aware that this could be done using overloading, but the larger the amount of arguments, the larger amount of possibilities, so it might end up with a huge list of overloads. The best case scenario would be being able to set things like:
void function(t1 a=16,t1 b=0,t2 c=1){/*body*/};
function(b=3,a=0);
but I'm not aware of such feature in C++.
Would it be possible to design some sort of macro system to take care of this?
So a long time ago I messed around with DirectX but I don't remember where it gets placed by default. I want to just get rid of the SDK and get the up to date version of it.
View 4 Replies View RelatedI have developed an application in C++ that creates some text files in a directory chosen by the user.
How can I ask the user set a Default Directory Path (and some other default parameters) so that she doesn't have to enter the same data in the GUI everytime the application is run.
The application has been developed using Qt Creator.
refer to the code below, the attribute class is created with the value type and default value, but why it doesn't work for std::string?
#include <string>
#include <iostream>
using namespace std;
[Code].....
Implicit default ctor does not initialize the built-in data members, so what is it needed for?
View 5 Replies View RelatedI've been pondering which of these 2 approaches would make for the best interface for a library: Defining custom exceptions with specific names for different error scenarios but with standard behaviour, or simply using the predefined exceptions from the STL.
This is my current approach:
Code:
namespace rpp
{
class ConnectionError : public std::exception
{
public:
ConnectionError(const std::string &p_err);
[Code] .....
This seems to make for more descriptive code but it adds no functionality and the implementations are completely identical, which seems "off" to me, somehow.
I have a question about the default constructor.
To my best understanding, the compiler will provide me with a deafult constructor only if there are no any user defined constructors, at all. Now consider the following code:
Code: class MyClass
{
private:
int m_data;
public:
MyClass(int init):m_data(init){cout<<"Ctr called"<<endl;}
[Code] ....
How is it that suddenly, there is a default constructor?