Upper Bound and Lower Bound of an array [SOLVED]
-
how to get the upper bound and lower bound of an array BYTE *pByte = new BYTE[64];
Some Day I Will Prove MySelf :: GOLD
modified on Friday, February 4, 2011 8:37 AM
Since it is C++, why not make your own little class that stores the length of the array.
class Buffer {
public:
Buffer(unsigned nSize) : m_pBuffer(NULL), m_nLength(nSize) {
if (nSize > 0) {
m_pBuffer = new BYTE[nSize];
if (m_pBuffer == NULL) {
m_nLength = 0;
//you ran out of memory, do something
}
}
}
~Buffer() {
if (m_pBuffer != NULL) {
delete []m_pBuffer;
}
}
unsigned GetLength() {
return m_nLength;
}
BYTE &operator[](unsigned nIndex) {
if (nIndex >= m_nLength) {
//error somehow
}
return m_pBuffer[nIndex];
}
BYTE *operator*() {
return m_pBuffer;
}private: BYTE \*m\_pBuffer; unsigned nLength;
};
Then you can use it like
Buffer bufData(64);
strcpy(*bufData, "hello world"); //the operator*() can be used to get a pointer to the data
//print the length
printf("The valid range is 0-%u\n", bufData.GetLength() - 1); //remember indices start at 0 and go to n-1
printf("The 2nd character is \'%c\'\n", bufData[1]); //Again, indices start at 0 making the 2nd element index 1
//The buffer automatically cleans up its memory when it goes out of scopeYou still need to call
delete pData;
if the buffer was created with BufferpData = new Buffer(64);
-
goldenrose9 wrote:
BYTE *pByte = new BYTE[64];
You are allocating the array, then it is your responsibility to keep the bounds as 0 and 64. :)
yes, 0 to 63, but i was in search of a function that returns the bound of an array, as in vb UBound() and LBound() function is used. Is there any similer function that helps to acheive bounds in c++
Some Day I Will Prove MySelf :: GOLD
-
yes, 0 to 63, but i was in search of a function that returns the bound of an array, as in vb UBound() and LBound() function is used. Is there any similer function that helps to acheive bounds in c++
Some Day I Will Prove MySelf :: GOLD
-
how to get the upper bound and lower bound of an array BYTE *pByte = new BYTE[64];
Some Day I Will Prove MySelf :: GOLD
modified on Friday, February 4, 2011 8:37 AM
VS provides the macro
#define _countof(array) (sizeof(array)/sizeof(array[0]))
[Note: this can't be used for arrays allocated via new.]
Best wishes, Hans
-
Since it is C++, why not make your own little class that stores the length of the array.
class Buffer {
public:
Buffer(unsigned nSize) : m_pBuffer(NULL), m_nLength(nSize) {
if (nSize > 0) {
m_pBuffer = new BYTE[nSize];
if (m_pBuffer == NULL) {
m_nLength = 0;
//you ran out of memory, do something
}
}
}
~Buffer() {
if (m_pBuffer != NULL) {
delete []m_pBuffer;
}
}
unsigned GetLength() {
return m_nLength;
}
BYTE &operator[](unsigned nIndex) {
if (nIndex >= m_nLength) {
//error somehow
}
return m_pBuffer[nIndex];
}
BYTE *operator*() {
return m_pBuffer;
}private: BYTE \*m\_pBuffer; unsigned nLength;
};
Then you can use it like
Buffer bufData(64);
strcpy(*bufData, "hello world"); //the operator*() can be used to get a pointer to the data
//print the length
printf("The valid range is 0-%u\n", bufData.GetLength() - 1); //remember indices start at 0 and go to n-1
printf("The 2nd character is \'%c\'\n", bufData[1]); //Again, indices start at 0 making the 2nd element index 1
//The buffer automatically cleans up its memory when it goes out of scopeYou still need to call
delete pData;
if the buffer was created with BufferpData = new Buffer(64);
-
yes, 0 to 63, but i was in search of a function that returns the bound of an array, as in vb UBound() and LBound() function is used. Is there any similer function that helps to acheive bounds in c++
Some Day I Will Prove MySelf :: GOLD
pByte
as you declared is just a pointer. You are putting the semantics of an array into it by assigning the memory for it. But the compiler has no way of knowing what, exactly this pointer is pointing to at any time. In other words, no, the compiler cannot know what the semantics of the memory is that any pointer is pointing to, nor can any function. That said, the above is not entirely true: when you compile your program in debug mode and check whatnew
(anddelete
) actually do, you will find that, internally, the system's memory manager indeed does store information about the memory being allocated, including the information whether this memory is for an array of objects or just one object. Therefore, if you try to release an array usingdelete
rather thandelete []
, you will get a runtime error! If you want to, you can replace the system'soperator new
anddelete
with your own implementations. These could then store additional information, such as the number of elements being allocated, and then you could implement a functionUBound()
that takes advantage of that hidden information. -
Nope. :) Otherwise you have to go for STL container classes in C++, collection classes in MFC, or your own array class to achieve this.
thanx.:thumbsup:
Some Day I Will Prove MySelf :: GOLD
-
pByte
as you declared is just a pointer. You are putting the semantics of an array into it by assigning the memory for it. But the compiler has no way of knowing what, exactly this pointer is pointing to at any time. In other words, no, the compiler cannot know what the semantics of the memory is that any pointer is pointing to, nor can any function. That said, the above is not entirely true: when you compile your program in debug mode and check whatnew
(anddelete
) actually do, you will find that, internally, the system's memory manager indeed does store information about the memory being allocated, including the information whether this memory is for an array of objects or just one object. Therefore, if you try to release an array usingdelete
rather thandelete []
, you will get a runtime error! If you want to, you can replace the system'soperator new
anddelete
with your own implementations. These could then store additional information, such as the number of elements being allocated, and then you could implement a functionUBound()
that takes advantage of that hidden information. -
pByte
as you declared is just a pointer. You are putting the semantics of an array into it by assigning the memory for it. But the compiler has no way of knowing what, exactly this pointer is pointing to at any time. In other words, no, the compiler cannot know what the semantics of the memory is that any pointer is pointing to, nor can any function. That said, the above is not entirely true: when you compile your program in debug mode and check whatnew
(anddelete
) actually do, you will find that, internally, the system's memory manager indeed does store information about the memory being allocated, including the information whether this memory is for an array of objects or just one object. Therefore, if you try to release an array usingdelete
rather thandelete []
, you will get a runtime error! If you want to, you can replace the system'soperator new
anddelete
with your own implementations. These could then store additional information, such as the number of elements being allocated, and then you could implement a functionUBound()
that takes advantage of that hidden information.Stefan63 wrote:
If you want to, you can replace the system's operator new and delete with your own implementations. These could then store additional information, such as the number of elements being allocated...
Which is essentially what happens in debug mode.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
how to get the upper bound and lower bound of an array BYTE *pByte = new BYTE[64];
Some Day I Will Prove MySelf :: GOLD
modified on Friday, February 4, 2011 8:37 AM