Click here to Skip to main content
15,915,093 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to Get the path of Internet Explorer using C++ Pin
tns_ranjith11-Jul-09 0:20
tns_ranjith11-Jul-09 0:20 
AnswerRe: How to Get the path of Internet Explorer using C++ Pin
Stuart Dootson11-Jul-09 1:00
professionalStuart Dootson11-Jul-09 1:00 
GeneralRe: How to Get the path of Internet Explorer using C++ Pin
tns_ranjith11-Jul-09 1:13
tns_ranjith11-Jul-09 1:13 
GeneralRe: How to Get the path of Internet Explorer using C++ Pin
Stuart Dootson11-Jul-09 1:19
professionalStuart Dootson11-Jul-09 1:19 
AnswerRe: How to Get the path of Internet Explorer using C++ Pin
David Crow11-Jul-09 15:54
David Crow11-Jul-09 15:54 
QuestionData population Pin
Chiman110-Jul-09 20:21
Chiman110-Jul-09 20:21 
AnswerRe: Data population Pin
Rajesh R Subramanian10-Jul-09 22:07
professionalRajesh R Subramanian10-Jul-09 22:07 
QuestionVector elements and the heap...or the stack... Pin
Mike the Red10-Jul-09 14:16
Mike the Red10-Jul-09 14:16 
I have a BYTE array that I'm trying to split into parts and put the parts in a vector:
#include <iostream>
#include <vector>
#include <algorithm>
#include <malloc.h> // For the 'on the stack' version
#ifndef LPBYTE
typedef unsigned char * LPBYTE // For you OS X types ; )
#endif

void split(vector<LPBYTE>& vect, const LPBYTE& lpbData) {
	LPBYTE start = lpbData;		// first part starts at beginning of lpbData
	LPBYTE end = lpbData + (size_t) 2;	// This is normally calculated by finding a separating sequence within lpbData
	size_t len = distance(start, end);	// Size of the part

	// Add part to vector
	LPBYTE lpbPart = new BYTE[len];
	/* Or, on the stack 
	LPBYTE lpbPart = (LPBYTE) _alloca(len)
	*/

	memcpy(lpbPart, start, len);
	vect.push_back( lpbPart );
}
int main() {
	LPBYTE data = {'a', 'b', 'c', 12, 22, 'Q'}; 
	vector<LPBYTE> parts;
	split(parts, data);
	
	for (vector<LPBYTE>::iterator vIT = parts.begin(); vIT != parts.end(); vIT++)
		cout << (char *) *vIT << "\n";
	parts.clear(); // This does no good, but I threw it in for good measure o_O
}
The problem with the heap version is that it leaks.

But if I use the stack version, or if I were to delete lpbPart inside split() (after adding it to the vector), a bunch of garbage is output 'cause the address is no longer valid.

I could iterate through the vector when I'm done with it, delete-ing each element... but I was told recently that that is a bad programming practice.

I am leaning towards deriving a class from vector and delete-ing the elements in the destructor, but I thought I'd run it by you guys first and see if there was a better way.

OR if there is a way to make a sub-array of an array, where the sub-array points to an address within the array but has a length shorter than the distance from the address within the source array to its end. This way would avoid the stack and the heap and the addresses would be valid so long as the source LPBYTE data was valid. It isn't good code, but to better explain what I mean:
LPBYTE data = {'a', 'b', 'c', 12, 22, 'Q'};
BYTE sub[2];
&sub = data + 2; // This is bad code, but what I mean is to point sub at the 'c' in data

As always, any help ya'll can give is greatly appreciated!

MZR
AnswerRe: Vector elements and the heap...or the stack... Pin
«_Superman_»10-Jul-09 16:06
professional«_Superman_»10-Jul-09 16:06 
AnswerRe: Vector elements and the heap...or the stack... Pin
Stuart Dootson10-Jul-09 23:10
professionalStuart Dootson10-Jul-09 23:10 
GeneralRe: Vector elements and the heap...or the stack... Pin
Mike the Red11-Jul-09 8:53
Mike the Red11-Jul-09 8:53 
GeneralRe: Vector elements and the heap...or the stack... Pin
Stuart Dootson11-Jul-09 8:56
professionalStuart Dootson11-Jul-09 8:56 
QuestionDialogBox function Pin
dipuks10-Jul-09 12:36
dipuks10-Jul-09 12:36 
AnswerRe: DialogBox function Pin
«_Superman_»10-Jul-09 15:59
professional«_Superman_»10-Jul-09 15:59 
QuestionToolbar-like custom draw buttons Pin
tiresias210-Jul-09 10:00
tiresias210-Jul-09 10:00 
QuestionAdding BigInt's Pin
TannerB10-Jul-09 9:35
TannerB10-Jul-09 9:35 
AnswerRe: Adding BigInt's Pin
josda100010-Jul-09 9:57
josda100010-Jul-09 9:57 
Question"Debug Assert Error" in VC++.NET Pin
james_bond10-Jul-09 8:00
james_bond10-Jul-09 8:00 
AnswerRe: "Debug Assert Error" in VC++.NET Pin
josda100010-Jul-09 8:33
josda100010-Jul-09 8:33 
GeneralRe: "Debug Assert Error" in VC++.NET Pin
james_bond10-Jul-09 10:56
james_bond10-Jul-09 10:56 
AnswerRe: "Debug Assert Error" in VC++.NET Pin
josda100010-Jul-09 11:07
josda100010-Jul-09 11:07 
GeneralRe: "Debug Assert Error" in VC++.NET Pin
james_bond10-Jul-09 14:05
james_bond10-Jul-09 14:05 
GeneralRe: "Debug Assert Error" in VC++.NET Pin
josda100010-Jul-09 16:02
josda100010-Jul-09 16:02 
AnswerRe: "Debug Assert Error" in VC++.NET Pin
Stuart Dootson10-Jul-09 8:35
professionalStuart Dootson10-Jul-09 8:35 
QuestionRelease Mode issue Pin
dipuks10-Jul-09 7:30
dipuks10-Jul-09 7:30 

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.