different ways of allocating memory

Discussion in 'C++' started by Larry, Dec 23, 2009.

  1. Larry

    Larry Guest

    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[i].lpData to access data...in the other code
    is used: sb[i]->lpData... why?

    thanks[/i][/i]
    Larry, Dec 23, 2009
    #1
    1. Advertising

  2. 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
    Maxim Yegorushkin, Dec 24, 2009
    #2
    1. Advertising

  3. Larry

    Larry Guest

    "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
    Larry, Dec 24, 2009
    #3
  4. Larry

    Larry Guest

    "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
    Larry, Dec 24, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hall
    Replies:
    4
    Views:
    425
  2. soni29
    Replies:
    6
    Views:
    393
    Kevin Goodsell
    Sep 5, 2003
  3. Axel
    Replies:
    1
    Views:
    713
    stephan beal
    Oct 27, 2003
  4. Sameer
    Replies:
    2
    Views:
    273
    David White
    Nov 3, 2003
  5. Rakesh Kumar
    Replies:
    5
    Views:
    664
    James Kanze
    Dec 21, 2007
Loading...

Share This Page