different ways of allocating memory

L

Larry

Hi,

I am in the process of coding a C++ programm to capture input audio data
and save it real time (wave file) by using the waveForm API.

Now this is the code I have written so far:

<<main.cpp
#include <windows.h>
#pragma comment (lib, "winmm.lib")
#include <mmsystem.h>
#include <iostream>
#include <fstream>
#include <stdlib.h> // Define "system" function
#include <string>
#define system_buf_len 4096

void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD
dwParam1,DWORD dwParam2);
bool addbuffer(WAVEHDR *pWaveHdr);


int main()
{
// Definisco la struttura WAVEFORMATEX
WAVEFORMATEX waveFormat;
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.wBitsPerSample = 16;
waveFormat.nChannels = 1;
waveFormat.nSamplesPerSec = 8000;
waveFormat.nBlockAlign = (waveFormat.nChannels *
waveFormat.wBitsPerSample) / 8;
waveFormat.nAvgBytesPerSec = (waveFormat.nSamplesPerSec *
waveFormat.nBlockAlign);
waveFormat.cbSize = 0;

MMRESULT mmres; // ...
HWAVEIN phvi; // Handle for the input device
UINT uDevice = 0; // Device id "Gruppo Microfoni"

// waveInOpen
mmres = waveInOpen(&phvi,
uDevice,
(LPWAVEFORMATEX)&waveFormat,
(DWORD)waveInProc,
0,
CALLBACK_FUNCTION
);

// Prepare Buffer

int i=0;
int num_buff = 3;
WAVEHDR *buffer = (WAVEHDR *) malloc(sizeof(WAVEHDR)*num_buff);
for (i=0; i<num_buff; i++)
{
buffer.lpData = (LPSTR) malloc(system_buf_len);
buffer.dwBufferLength = system_buf_len;
buffer.dwBytesRecorded = 0;
buffer.dwUser = 0;
buffer.dwFlags = 0;
buffer.dwLoops = 0;

waveInPrepareHeader(phvi, &buffer, sizeof(WAVEHDR));
waveInAddBuffer(phvi, &buffer, sizeof(WAVEHDR));
}
//waveInStart;
waveInStart(phvi);
system("pause");
//waveInClose;
waveInClose(phvi);
return 0;
}

void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD
dwParam1,DWORD dwParam2)
{
WAVEHDR* pWaveHdr;
switch(uMsg)
{
case MM_WIM_DATA:
pWaveHdr = ((WAVEHDR*)dwParam1);
if (pWaveHdr && hwi)
{
if (pWaveHdr->dwFlags && WHDR_DONE == WHDR_DONE)
{
addbuffer(pWaveHdr);
waveInAddBuffer(hwi, pWaveHdr, sizeof(WAVEHDR));
}
}
break;

case MM_WIM_OPEN:
std::cout << "MM_WIN_OPEN" << std::endl;
break;
case MM_WIM_CLOSE:
std::cout << "MM_WIN_CLOSE" << std::endl;
break;
}
}

bool addbuffer(WAVEHDR *pWaveHdr)
{
std::cout << pWaveHdr->dwBytesRecorded << std::endl;
char * buff = pWaveHdr->lpData;
std::eek:fstream usc;
usc.open("ciao.wave", std::ios::app|std::ios::binary);
usc.write(buff,pWaveHdr->dwBytesRecorded);
usc.close();

return true;
}
<</main.cpp

I run across sort of similar code where it uses a different way to allocate
memory:

<<diff.cpp
pwf.wBitsPerSample= 16;
pwf.wf.nChannels = 1;
pwf.wf.nSamplesPerSec = 8000;

pwf.wf.wFormatTag = WAVE_FORMAT_PCM;
pwf.wf.nBlockAlign =
pwf.wf.nChannels * pwf.wBitsPerSample / 8;
pwf.wf.nAvgBytesPerSec =
pwf.wf.nSamplesPerSec * pwf.wf.nBlockAlign;

if (waveInOpen(&rip->hwi,
/*WAVE_MAPPER*/ 0, (LPWAVEFORMATEX)&pwf,
(DWORD) rip->eventh, 0, CALLBACK_EVENT )) {
printf("Couldn't open sound device. Leaving..\n");
goto problem;
}

/* Preparing system buffers */

sb = (WAVEHDR**) malloc(sizeof(WAVEHDR**) * system_buf_num);
if(!sb)
goto problem;

for (i = 0; i < system_buf_num; i++)
sb = NULL;

for (i = 0; i < system_buf_num; i++) {

count = i;
sb = (WAVEHDR*) malloc(sizeof(WAVEHDR));

if (sb == NULL) {
put_debug_message("malloc() error!\n");
goto problem;
}

sb->lpData = (LPBYTE) malloc(system_buf_len);
sb->dwBufferLength = system_buf_len;
sb->dwBytesRecorded = 0;
sb->dwUser = 0;
sb->dwFlags = 0;
sb->dwLoops = 0;

if(!sb->lpData)
goto problem;

if (waveInPrepareHeader(rip->hwi, sb, sizeof(WAVEHDR))) {
put_debug_message("waveInPrepareHeader problem!\n");
goto problem;
}
if (waveInAddBuffer(rip->hwi, sb, sizeof(WAVEHDR))) {
put_debug_message("waveInAddBuffer problem!\n");
goto problem;
}
}

<</diff.cpp

So, basically I am using:

WAVEHDR *buffer = (WAVEHDR *) malloc(sizeof(WAVEHDR)*num_buff);

whereas the other code is using:

sb = (WAVEHDR**) malloc(sizeof(WAVEHDR**) * system_buf_num);

Now, I am really interested in this latter form of allocating! what is the
main differences?

How should I declare "sb" before allocating it? would: /WAVEHDR *sb/ ok?

Also, in my code I use: buffer.lpData to access data...in the other code
is used: sb->lpData... why?

thanks
 
M

Maxim Yegorushkin

On 23/12/09 23:20, Larry wrote:

[]
So, basically I am using:

WAVEHDR *buffer = (WAVEHDR *) malloc(sizeof(WAVEHDR)*num_buff);

This allocates num_buff WAVEHDR objects.
whereas the other code is using:

sb = (WAVEHDR**) malloc(sizeof(WAVEHDR**) * system_buf_num);

This allocates system_buf_num WAVEHDR** objects. However, the cast is
incorrect. It is WAVEHDR*** pointer that can point to WAVEHDR** objects.
Now, I am really interested in this latter form of allocating! what is
the main differences?

They allocate different objects. The first allocates WAVEHDR objects,
the second allocates WAVEHDR** objects.
 
L

Larry

This allocates system_buf_num WAVEHDR** objects. However, the cast is
incorrect. It is WAVEHDR*** pointer that can point to WAVEHDR** objects.

so should it be:

sb = (WAVEHDR***) malloc(sizeof(WAVEHDR**) * system_buf_num); ??

ho should I declare sb first??

is there any reason to go for this way of allocating buffers?

thanks
 
L

Larry

Larry said:
"Maxim Yegorushkin" <[email protected]> ha scritto nel messaggio

typedef struct {
char* titolo;
char* autore;
int annopub;
} LIBRO;

// main

const int nbuff = 5;
LIBRO *l = new LIBRO[nbuff];
l[0].annopub = 2009;
l[0].titolo = "...some chars...";
cout << l[0].titolo << " " << l[0].annopub << endl;
delete[] l;


Why if I just use:

LIBRO *l = new LIBRO;

I can access the structure with the -> operator while with LIBRO[nbuff] I
can only use the dot operator? is there much difference?

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top