Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > different ways of allocating memory

Reply
Thread Tools

different ways of allocating memory

 
 
Larry
Guest
Posts: n/a
 
      12-23-2009
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[i].lpData = (LPSTR) malloc(system_buf_len);
buffer[i].dwBufferLength = system_buf_len;
buffer[i].dwBytesRecorded = 0;
buffer[i].dwUser = 0;
buffer[i].dwFlags = 0;
buffer[i].dwLoops = 0;

waveInPrepareHeader(phvi, &buffer[i], sizeof(WAVEHDR));
waveInAddBuffer(phvi, &buffer[i], 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: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[i] = NULL;

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

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

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

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

if(!sb[i]->lpData)
goto problem;

if (waveInPrepareHeader(rip->hwi, sb[i], sizeof(WAVEHDR))) {
put_debug_message("waveInPrepareHeader problem!\n");
goto problem;
}
if (waveInAddBuffer(rip->hwi, sb[i], 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[i].lpData to access data...in the other code
is used: sb[i]->lpData... why?

thanks

 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      12-24-2009
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.

--
Max
 
Reply With Quote
 
 
 
 
Larry
Guest
Posts: n/a
 
      12-24-2009

"Maxim Yegorushkin" <> ha scritto nel messaggio
news:4b32b260$0$9753$...

>> 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.


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

 
Reply With Quote
 
Larry
Guest
Posts: n/a
 
      12-24-2009

"Larry" <> ha scritto nel messaggio
news:4b32b597$0$1112$. ..
>
> "Maxim Yegorushkin" <> ha scritto nel messaggio
> news:4b32b260$0$9753$...
>


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Allocating vector of strings seem to crash. Allocating array ofstrings seems to be ok . Rakesh Kumar C++ 5 12-21-2007 10:42 AM
segmenatation fault while allocating memory Sameer C++ 2 11-03-2003 09:24 AM
allocating memory for std::string? Axel C++ 1 10-27-2003 12:35 PM
basic allocating memory question soni29 C++ 6 09-05-2003 05:45 PM
Pushing memory allocating objects into a vector. hall C++ 4 08-20-2003 09:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57