Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C Heap dongs

Name: Anonymous 2011-07-18 21:19

I keep getting heap corruption with the following code:

http://pastebin.com/gGdgPiYa

it occurs @ line 132 : free(queue_poll(q));

i checked it with a debugger and my polled element was still there and not Null or anything so why isn't it allowing to be freed? Also i use the queue code with another application that doesn't free the polled element and i end up memory leaks but if i try to free the element when i'm finish using it it heap corruptions me.

Header looks like:
http://pastebin.com/AwFKQcGe


queue_init(...) is no longer used i just haven't bothered to take it out btw so pay no attention to it.

Name: Anonymous 2011-07-18 21:23

if it ain't Lisp it's crap

Name: Anonymous 2011-07-18 21:26

http://i.imgur.com/mh5oR.png

also little pic to show the values when it errors out, ty is the struct being freed. As you can see it's not null or anything but it still error's out due to heap corruption but if i don't free it then it leads to memory leak as i said before.

Name: Anonymous 2011-07-18 21:32

>>3
What program did you take a screenshot of?

Name: Anonymous 2011-07-18 21:36

>>4
it's in mstudio 2010 :/ ya i know i suck dick


anyway i changed my queue_poll method around to hold the address of the element instead of a copy of the Entry. then it returns that address. I also double checked to see if it was returning the same address as the one i mallocd in the beginning and it's.
http://pastebin.com/xw5Kijax

error still happens though when i try to free the element.

Name: Anonymous 2011-07-18 21:45

also this is the error it gives:
HEAP[mapgen.exe]: Heap block at 008D3660 modified at 008D3690 past requested size of 28


It also errors out when i do:

struct ty * create_ty(int,int,int){
  struct ty * dick = malloc(sizeof(ty));
  //init shit
  return ty;
}

struct ty * penis = create_ty(941,451,11);
free(penis);

Name: Anonymous 2011-07-18 21:52

I've solved my stupid problem, thank you for your lovely wisdom /prog/


error was due to me malloc(sizeof(ty)) instead of malloc(sizeof(struct ty))

Name: Anonymous 2011-07-18 21:56

Well, ok.

Name: Anonymous 2011-07-18 22:11

Might I recommend renaming poll to pop? Thanks.

>>7
You should use sizeof(*ty), not sizeof(struct ty). This way if the type of ty changes, the code will still work.

Also? Never fucking name a variable the same thing as a struct! I don't care if C has a separate namespace for struct tags. It's bad.

Name: Anonymous 2011-07-18 22:15

>>9
ya i was being lazy about my naming system sorry.

Name: Anonymous 2011-07-19 0:51

struct ty * ty = malloc(sizeof(ty));
why

Don't change these.
Name: Email:
Entire Thread Thread List