Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

bosjoh

Allocating memory for a struct (DirectSound)

This topic is 6914 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Any chance of an error message?

You could try
sizeof(WAVEFORMATEX).

-Mezz

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Now I changed the code so that the struct is a pointer to a struct.
It works now. Thanks for the extra info.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!