|
Thanks for reply but it's not working
i have a CString store[100];,CString File; and int cou=0;. And i use store[cou]=File. And i want to get Size of store[cou].Plz help me
|
|
|
|
|
|
CArray or user defined array??
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You/xml>
|
|
|
|
|
|
do you want the size of the array in bytes, or the number of elements in the array?
if you want the size in bytes, use the sizeof() like so;
int size_in_bytes;
int theArray[20];
size_in_bytes = sizeof(theArray);
|
|
|
|
|
Hi,
Dont use user defined array in this case. You better use CStringArray.
Then you no need to calculate the size of the array. This class is having a member function int GetSize() to get the size of the array.
The price of anything is the amount of life you exchange for it.
Thanks and Regards.
SANTHOSH V
|
|
|
|
|
please read at first the docs about the used obejcts carefully. Often there are tiny little helper as .length or .GetLength to solve such problems. (And use the search box in top of this site a lot)
Greetings from Germany
|
|
|
|
|
What kind of array are you using? Could you please post array declaration?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
|
The size of such array is
int iSize = sizeof(store);
The number of elements of such array is
int iCount = sizeof(store)/sizeof(store[0]);
or, more concisely, 100 .
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks for reply but it's not working.
CString store[100];
CString File;
and int cou=0;
store[cou]=File.
File name added at the run time.Plz help me
|
|
|
|
|
What does it mean 'it's not working?' in such context?
What are you trying to do?
What happens instead?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
store[cou]=File;
int iSize=sizeof(store);
AfxMessageBox(iSize);
Then i am geting Debug Assertion Fail Line number:163.
|
|
|
|
|
On what line does the code assert?
BTW your array is fixed-sized to 100 elements, regardless if you assign or not any of them.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thank Now it is working.Your code is good.I make a mistake.SO thanks again.Ome more question how can i use dynamic array.Plz help me
|
|
|
|
|
If you need an array that grows dynamically then have a look at std::vector .
MFC has the CArray container but it's a crap (please don't tell this to Rajesh ).
very basic (and naive) vector usage sample follows:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void main()
{
vector < string > v;
v.push_back(string("hi,"));
v.push_back(string("how"));
v.push_back(string("do you do?"));
cout << "vector size = " << v.size() << endl;
for (int i=0; i<v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks for help.Every reply boost me..
|
|
|
|
|
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
In one of my projects I have an array of pointers to Tile 's (a class of my own) within a Block class. Upon calling the constructor for the Block class I assign the pointer to each Tile in the array to the address of a Tile created on the heap using the new operator like so;
Block::Block()
{
while(i=0; i < 20; i++)
{
Array_of_pointers_to_Tiles[i] = new Tile;
}
}
I then call the delete operator in the destructor like so;
Block::~Block()
{
while(i=0; i < 20; i++)
{
delete Array_of_pointers_to_Tiles[i];
Array_of_pointers_to_Tiles[i] = NULL;
}
}
This code does not compile.
According to this resource[^] I am to allocate the array like so;
int* a = NULL;
int n;
cin >> n;
a = new int[n];
for (int i=0; i<n; i++) {
a[i] = 0;
}
. . .
delete [] a;
a = NULL;
and to free the memory allocated to the array;
delete [] a;
a = NULL;
However this code doesn't compile either!
What is the correct way?!?
modified on Monday, September 29, 2008 1:44 AM
|
|
|
|
|
Can you tell what is the compiler error?
Saurabh
|
|
|
|
|
As soon as I get home, yes
I'm at work
|
|
|
|
|
Okay, so it turns out that I was accessing each array element using the points-to operator instead of the dot-operator :P
You can't use the points-to operator in this situation because when you think about it, it doesn't make any sense to :P
EDIT: When running in debug mode I get a debug assertion failure. Attempting to debug produces this:
"0 > 8828 Main Thread Main Thread Tile::`vector deleting destructor' Normal 0"
on the line delete [] p_TileArray;
What's going on?
modified on Tuesday, September 30, 2008 3:08 AM
|
|
|
|
|
Unless we see how you defined the array (from your Block class definition), we're not going to be able to help...
Iain.
|
|
|
|
|
In Block.h
class Block
{
public:
Block(void);
Block(int itsShape, int itsColour);
~Block(void);
bool isGrounded(void);
void MoveLeft();
void MoveRight();
void MoveDown();
void Rotate();
void Render();
private:
int Shape;
int Colour;
bool Grounded;
Tile* p_TileArray;
};
in Block.cpp
#include "Block.h"
Block::Block(void)
{
Grounded = false;
Shape = (rand() % 7);
Colour = (rand() % 6);
p_TileArray = NULL;
p_TileArray = new Tile[4];
p_TileArray[0].SetPos(498.0, 82.0);
p_TileArray[1].SetPos(530.0, 82.0);
p_TileArray[2].SetPos(498.0, 114.0);
p_TileArray[3].SetPos(530.0, 114.0);
}
Block::~Block(void)
{
if (p_TileArray)
{
delete [] p_TileArray;
p_TileArray = NULL;
}
}
bool Block::isGrounded(void)
{
return Grounded;
}
void Block::MoveLeft()
{
if(Grounded == false)
{
for(int i = 0; i < (sizeof(p_TileArray) / sizeof(p_TileArray[0])); i++)
{
p_TileArray[i].MoveLeft();
}
}
}
void Block::MoveRight()
{
if(Grounded == false)
{
for(int i = 0; i < (sizeof(p_TileArray) / sizeof(p_TileArray[0])); i++)
{
p_TileArray[i].MoveRight();
}
}
}
void Block::MoveDown()
{
if(Grounded == false)
{
for(int i = 0; i < (sizeof(p_TileArray) / sizeof(p_TileArray[0])); i++)
{
p_TileArray[i].MoveDown();
}
}
}
void Block::Rotate()
{
}
void Block::Render()
{
for(int i = 0; i < (sizeof(p_TileArray) / sizeof(p_TileArray[0])); i++)
{
p_TileArray[i].Render();
}
}
|
|
|
|
|
On the code above, I'm quite surprised at the delete [] failing, unless there is a problem inside the destructor for Tile.
Have you tried making a test app, and tried Tile as a very simple class (ie, just an int ), and implicit constructor / destructor?
Also put a breakpoint on your allocation and deallocation part, and make sure you haven't messed up the address of p_TileArray.
Lastly, I can see one other bug, which is in place many times.
for(int i = 0; i < (sizeof(p_TileArray) / sizeof(p_TileArray[0])); i++)
As m_pTileArray is a pointer, sizeof(p_TileArray) will be 4 on a 32 bit system, and 8 on a 64 one. I'm assuming sizeof (Tile) is larger, so your loops will never work. When you weren't dynamically allocating Tiles, and had Tile p_TileArray[4]; (I'm guessing), then the sizeof trick would work nicely.
You;'ll need a separate member variable to tell the loops how many you have.
eg:
p_TileArray = NULL;
n_TileArray = 4;
p_TileArray = new Tile[n_TileArray];
You may also want to use array structures to hold the tiles, which will clear things up.
In block.h
CArray<Tile, Tile &> m_Tiles;
In block.cpp
constructor
m_Tiles.SetSize (4);
m_Tiles [0].Set....
...
void Block::Render()
{
for(int i = 0; i < m_Tiles.GetSize (); i++)
{
m_Tiles[i].Render();
}
}
If you prefer, you could use std::vector instead. Either choice frees you from needing to delete the dynamic "array", and may help you.
Good luck,
Iain.
|
|
|
|