Thread: malloc access problem

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    58

    malloc access problem

    the code:

    Code:
    struct Vertex
    {
    	float x, y, z;	// Position of vertex in 3D space
    	RGBquad color;	// Color of vertex
    };
    
    Vertex *HenonList;
    
    foo()
    {
    HenonList = (Vertex*)malloc(rits * sizeof(Vertex*));
    	if(HenonList == NULL) exit(1);
    	HenonList[900].x = 5;
    }
    when compiled says: "Access violation writing location" on the HenonList[900].x = 5; line

    But I don't understand how come? I malloc'd correctly right?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> sizeof(Vertex*)
    You are asking for memory to hold rits # of Vertex pointers. What you probably want is rits # of whole Vertex objects.

    Why use malloc instead of new?
    If this is really C, then don't cast malloc.

    gg

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    We don't know what "rits" is - if it is less than 900, then the rest of the malloc is irrelevant.
    But I guess that your actual problem is that you are using sizeof(Vertex*) rather than sizeof(Vertex) or sizeof(*HenonList).

    Since sizeof(Vertex *) is the size of a pointer, it is likely much smaller than the x, y, z and RGQQuad values.

    Also, since this is C++, you should probably use
    Code:
    Vertex *HenonList = new Vertex[rits];
    That way, you don't need a cast for malloc, and you don't need to worry about getting any sizeof right. Just remember to use delete [] to delete the value after you finish.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    58
    Hey, thank you VERY much with your responces!

    You were EXACTLY right, I didn't catch it until now lol

    And your right, I should be using the new function instead. I will work on converting the code now

    - Again, Thank you very much

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by parad0x13 View Post
    Hey, thank you VERY much with your responces!

    You were EXACTLY right, I didn't catch it until now lol

    And your right, I should be using the new function instead. I will work on converting the code now

    - Again, Thank you very much
    Just remember that in modern C++, new never returns NULL, if it cant fill the memory request, it throws an exception instead. For the most part, all this means is that you don't need to check for NULL. You don't necessarily have to try catching this exception either, but if you do, just a single catch around the contents of main should be fine.
    There's also a way of calling a "no throw" version of new, for compatibility with older code you could say, but I'd just go for the normal 'new' here if you can.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc access violation
    By JOCAAN in forum C Programming
    Replies: 7
    Last Post: 11-30-2008, 04:47 AM
  2. microsoft access problem
    By ElastoManiac in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 12-05-2005, 07:42 AM
  3. malloc problem
    By fnoyan in forum C Programming
    Replies: 3
    Last Post: 09-19-2003, 11:59 AM
  4. malloc problem in SUN in 64-bit compilation
    By ylzhang in forum C Programming
    Replies: 6
    Last Post: 05-31-2003, 11:48 AM
  5. malloc problem
    By Jase in forum C Programming
    Replies: 4
    Last Post: 05-25-2003, 07:28 PM