Click here to Skip to main content
15,895,423 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Upper Bound and Lower Bound of an array Pin
Richard MacCutchan4-Feb-11 1:28
mveRichard MacCutchan4-Feb-11 1:28 
GeneralRe: Upper Bound and Lower Bound of an array Pin
goldenrose94-Feb-11 1:52
goldenrose94-Feb-11 1:52 
GeneralRe: Upper Bound and Lower Bound of an array Pin
Cool_Dev4-Feb-11 1:56
Cool_Dev4-Feb-11 1:56 
GeneralRe: Upper Bound and Lower Bound of an array Pin
goldenrose94-Feb-11 2:35
goldenrose94-Feb-11 2:35 
GeneralRe: Upper Bound and Lower Bound of an array Pin
Stefan_Lang4-Feb-11 2:31
Stefan_Lang4-Feb-11 2:31 
GeneralRe: Upper Bound and Lower Bound of an array Pin
Niklas L4-Feb-11 8:05
Niklas L4-Feb-11 8:05 
GeneralRe: Upper Bound and Lower Bound of an array Pin
David Crow5-Feb-11 5:09
David Crow5-Feb-11 5:09 
AnswerRe: Upper Bound and Lower Bound of an array Pin
Andrew Brock4-Feb-11 1:52
Andrew Brock4-Feb-11 1:52 
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 scope

You still need to call delete pData; if the buffer was created with Buffer pData = new Buffer(64);
GeneralRe: Upper Bound and Lower Bound of an array Pin
Niklas L4-Feb-11 2:19
Niklas L4-Feb-11 2:19 
AnswerRe: Upper Bound and Lower Bound of an array Pin
Hans Dietrich4-Feb-11 2:01
mentorHans Dietrich4-Feb-11 2:01 
AnswerRe: Upper Bound and Lower Bound of an array [SOLVED] Pin
Pranit Kothari6-Feb-11 7:14
Pranit Kothari6-Feb-11 7:14 
QuestionReleaseBuffer() of CSimpleString is not working. Pin
Amrit Agr3-Feb-11 18:55
Amrit Agr3-Feb-11 18:55 
AnswerRe: ReleaseBuffer() of CSimpleString is not working. Pin
Andrew Brock3-Feb-11 19:10
Andrew Brock3-Feb-11 19:10 
GeneralRe: ReleaseBuffer() of CSimpleString is not working. Pin
Amrit Agr3-Feb-11 21:38
Amrit Agr3-Feb-11 21:38 
QuestionDos command Pin
MsmVc3-Feb-11 18:17
MsmVc3-Feb-11 18:17 
QuestionRe: Dos command Pin
Rajesh R Subramanian3-Feb-11 18:39
professionalRajesh R Subramanian3-Feb-11 18:39 
AnswerRe: Dos command Pin
Andrew Brock3-Feb-11 18:49
Andrew Brock3-Feb-11 18:49 
GeneralRe: Dos command Pin
MsmVc4-Feb-11 0:27
MsmVc4-Feb-11 0:27 
AnswerRe: Dos comman Pin
Cool_Dev3-Feb-11 23:03
Cool_Dev3-Feb-11 23:03 
AnswerRe: Dos command Pin
csrss3-Feb-11 23:25
csrss3-Feb-11 23:25 
GeneralRe: Dos command Pin
MsmVc4-Feb-11 1:11
MsmVc4-Feb-11 1:11 
QuestionWould you all call this namespace abuse? Pin
asincero3-Feb-11 14:06
asincero3-Feb-11 14:06 
AnswerRe: Would you all call this namespace abuse? Pin
Madhu Nair3-Feb-11 16:33
Madhu Nair3-Feb-11 16:33 
GeneralRe: Would you all call this namespace abuse? Pin
asincero3-Feb-11 16:45
asincero3-Feb-11 16:45 
GeneralRe: Would you all call this namespace abuse? Pin
Stefan_Lang3-Feb-11 21:55
Stefan_Lang3-Feb-11 21:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.