Allocating memory for a struct (DirectSound)

Started by
5 comments, last by bosjoh 23 years, 11 months ago
I was fooling around with DirectSound. In one of the example programs (adjustsound), a WAVEFORMATEX structure is deleted from the memory, and reallocated with some extra bytes (for whatever reason). I tried something similar, but it won''t work.

void test(void){
   WAVEFORMATEX wfx;
   delete &wfx
   wfx=(WAVEFORMATEX *)malloc(sizeof(wfx)+100);
};
I tried &wfx, *wfx, **wfx etc. Any solutions?
Advertisement
Any chance of an error message?

You could try
sizeof(WAVEFORMATEX).

-Mezz
I believe Mezz has it... your sizeof is pointing to a deleted variable. I''ve seen this problem before (really hard to debug) when people have attempted to take a sizeof in a function call that includes the creation of the variable the sizeof refers to, which causes it to randomly pick a number. Use the WAVEFORMATEX in your sizeof instead.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Whoa, that code is full is errors. wfx is a local variable, and they''re trying to delete it??? And then allocate it from the heap?? That''s just totally illegal. If its dynamic mem, then it needs a pointer, in which case you wouldn''t need to delete the memory first, since there isn''t any yet. And they shouldn''t mix delete and malloc either.

As far as sizeof goes, it shouldn''t matter whether the variable ''exists'' or not (although obviously doing anything with a deleted value is wrong). Its determined at compile time. So as long as the compiler can see that wfx is of type WAVEFORMATEX, it should be exactly the same anyways.

Rock
Well, what they are actually doing is declare a global variable wfx (other name used in the example) and passing it as **wfx through the function as an argument. Then they are deleting it (using delete) and reallocating it using the new operator(new didn''t work, so maybe malloc works, that''s why there is malloc in my code).
I also tried sizeof(WAVEFORMATEX), but it says that wfx is no lvalue.
If its a double pointer in the function (**wfx), do:
wfx = *(WAVEFORMATEX *)malloc(sizeof(WAVEFORMATEX)+100); 


No lvalue means that you cant assign the right to the left. Like
3 = x + y 


x and y could possibly add up to three, but 3 is a constant and can''t be assigned a value too. This isn''t the case here, just wanted to point that out.

-----------------------------------------------------------
PCMCIA - People Can't Memorize Computer Industry Acronyms
ISDN - It Still Does Nothing
APPLE - Arrogance Produces Profit-Losing Entity
SCSI - System Can't See It
DOS - Defunct Operating System
BASIC - Bill's Attempt to Seize Industry Control
IBM - I Blame Microsoft
DEC - Do Expect Cuts
CD-ROM - Consumer Device, Rendered Obsolete in Months
OS/2 - Obsolete Soon, Too.
WWW - World Wide Wait
MACINTOSH - Most Applications Crash; If Not, The Operating System Hangs
Now I changed the code so that the struct is a pointer to a struct.
It works now. Thanks for the extra info.

This topic is closed to new replies.

Advertisement