Click here to Skip to main content
15,887,027 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Corruption of the Heap Error Pin
Niklas L14-Feb-11 21:17
Niklas L14-Feb-11 21:17 
AnswerRe: Corruption of the Heap Error Pin
Stefan_Lang15-Feb-11 6:52
Stefan_Lang15-Feb-11 6:52 
GeneralRe: Corruption of the Heap Error Pin
Niklas L15-Feb-11 10:54
Niklas L15-Feb-11 10:54 
GeneralRe: Corruption of the Heap Error Pin
Stefan_Lang15-Feb-11 22:10
Stefan_Lang15-Feb-11 22:10 
GeneralRe: Corruption of the Heap Error [modified] Pin
Niklas L15-Feb-11 22:37
Niklas L15-Feb-11 22:37 
GeneralRe: Corruption of the Heap Error Pin
Stefan_Lang15-Feb-11 22:51
Stefan_Lang15-Feb-11 22:51 
GeneralRe: Corruption of the Heap Error [modified] Pin
Niklas L15-Feb-11 23:13
Niklas L15-Feb-11 23:13 
GeneralRe: Corruption of the Heap Error Pin
Niklas L16-Feb-11 6:57
Niklas L16-Feb-11 6:57 
I will try to explain this in a different way. Suppose you call a black box API with returns a pointer to a chunk of memory. According to the specifications, the first four bytes are an integer containing a version number of the data. After that there is a four byte unsigned integer giving how many samples you will get, followed by that many eight byte double float sample values.

In code that might look like:
C++
void *data = GetSamples();

// Extract data
int version = * (int*) data;
unsigned int sampleCount = * (unsigned int*) (( (char*) data ) + sizeof(int))
double *samples = (double*) (( (char*) data ) + sizeof(int) + sizeof(unsigned int))

This will extract the variables needed, and they are now ready to be used. The way they are found is rather error prone.

A better way of doing this would be to map a known struct over the data with the same binary representation
C++
struct Samples
{
    int version;
    unsigned int sampleCount;
    double samples[];
};

void *data = GetSamples();
Samples *samples = (Samples*) data;

Now I can use samples as if it was a pointer to Samples. The good thing here is that the compiler will help me calculate the offsets for each known part of the binary data. This is the reverse of what I described in my earlier post.

Now assume there is another API which accepts such a set of binary data for processing, ProcessSamples(void* data); How would you construct the data if you only had the specification to it, and the data you had access to was in another format?
C++
struct Samples
{
    int version;
    unsigned int sampleCount;
    double samples[];
};

// Calculate the size in bytes of our data block
size_t dataSizeNeeded = sizeof(int) + sizeof(unsigned int) + sizeof(double) * mySampleCount;
char *bytes = new char[dataSizeNeeded]; // Allocate enough bytes to hold the data
Samples *samples = (Samples*) bytes; // Treat it as if it was pointing to a Samples struct

samples->version = 1;
samples->sampleCount = mySampleCount;

for (size_t i = 0; i < mySampleCount; ++i)
{
    samples->samples[i] = ...;
}

// Now we have a properly initialized data block
ProcessSamples(samples);

delete [] bytes;

The compiler will again help with the offsets, even though we're dealing with a 'dynamically sized struct'.

N.B. I intensionally left out important info on struct member alignment, Microsoft extensions to the C++ language, size of data types, data hiding, how to handle multiple dynamically sized members in the same 'struct', and the sample is not from the real world.

GeneralRe: Corruption of the Heap Error Pin
Stefan_Lang16-Feb-11 22:22
Stefan_Lang16-Feb-11 22:22 
GeneralRe: Corruption of the Heap Error Pin
Niklas L16-Feb-11 23:12
Niklas L16-Feb-11 23:12 
GeneralRe: Corruption of the Heap Error Pin
Stefan_Lang17-Feb-11 1:10
Stefan_Lang17-Feb-11 1:10 
GeneralRe: Corruption of the Heap Error Pin
Niklas L17-Feb-11 1:34
Niklas L17-Feb-11 1:34 
GeneralRe: Corruption of the Heap Error Pin
Stefan_Lang17-Feb-11 1:42
Stefan_Lang17-Feb-11 1:42 
GeneralRe: Corruption of the Heap Error Pin
Foothill15-Feb-11 11:16
professionalFoothill15-Feb-11 11:16 
QuestionHow to use CMFCVisualManagerOffice2007 in dialog App? Pin
stephen_young14-Feb-11 3:24
stephen_young14-Feb-11 3:24 
AnswerRe: How to use CMFCVisualManagerOffice2007 in dialog App? Pin
David Crow14-Feb-11 3:43
David Crow14-Feb-11 3:43 
GeneralRe: How to use CMFCVisualManagerOffice2007 in dialog App? Pin
stephen_young14-Feb-11 6:14
stephen_young14-Feb-11 6:14 
QuestionMulti Language Support Pin
T.RATHA KRISHNAN14-Feb-11 2:44
T.RATHA KRISHNAN14-Feb-11 2:44 
AnswerRe: Multi Language Support Pin
loyal ginger14-Feb-11 10:16
loyal ginger14-Feb-11 10:16 
AnswerRe: Multi Language Support Pin
yu-jian14-Feb-11 16:29
yu-jian14-Feb-11 16:29 
AnswerRe: Multi Language Support Pin
ShilpiP14-Feb-11 18:17
ShilpiP14-Feb-11 18:17 
AnswerRe: Multi Language Support Pin
RaviRanjanKr16-Feb-11 3:02
professionalRaviRanjanKr16-Feb-11 3:02 
QuestionImplementing Remeber me Facility Pin
pix_programmer14-Feb-11 2:12
pix_programmer14-Feb-11 2:12 
AnswerRe: Implementing Remeber me Facility Pin
CPallini14-Feb-11 2:33
mveCPallini14-Feb-11 2:33 
GeneralRe: Implementing Remeber me Facility Pin
pix_programmer14-Feb-11 2:41
pix_programmer14-Feb-11 2:41 

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.