![]() |
| | #1 |
| Registered User Join Date: Sep 2008 Location: germany
Posts: 2
| i got a strange SegFault when trying to allocate memory (malloc). First i setup the object like this: Code: //global
Accelerator_KDTree *g_KdTree; //KD-Tree Objectpointer
...
int main(void) {
...
g_KdTree = new Accelerator_KDTree(SIMPLE); //create the object
int res = g_KdTree->init(&Geometry, 1); // call init method
...
}
Code: void Accelerator_KDTree::buildTree(POG* pog, int count)
{
printf("constructing KD-Tree...\n");
pc= new PrimitiveContainer(pog,count);
KDTreeNode::pc=pc;
KDTreeNode::maxDepth=512;
KDTreeNode::cutoff=8;
// WTF?
//float* blah = (float*) malloc(10*sizeof(float)); // SEGFAULT!!!
std::list<int> plist;
AABoundingBox aabb;
for(int i=0;i<pc->primitiveCount;i++)
{
//fill primitive list
plist.push_back(i); // SEGFAULT (when template calls malloc)
//construct axis aligned bounding box
aabb.pmax.x=MAX2(aabb.pmax.x,pc->getMaxAxisValue(i,0));
aabb.pmax.y=MAX2(aabb.pmax.y,pc->getMaxAxisValue(i,1));
aabb.pmax.z=MAX2(aabb.pmax.z,pc->getMaxAxisValue(i,2));
aabb.pmin.x=MIN2(aabb.pmin.x,pc->getMinAxisValue(i,0));
aabb.pmin.y=MIN2(aabb.pmin.y,pc->getMinAxisValue(i,1));
aabb.pmin.z=MIN2(aabb.pmin.z,pc->getMinAxisValue(i,2));
}
root= KDTreeNode::buildTree(plist,aabb,4);
printf("complete. %i nodes, max depth: %i,min depth: %i, avg depth: %i, %i",KDTreeNode::nodecount,KDTreeNode::treedepth,KDTreeNode::treemindepth,KDTreeNode::avgdepth,KDTreeNode::primcount);
}
![]() I have no idea what goes wrong here so i would be grateful for any hint what possibly could cause this. Maybe i just miss something stupid. Greets cipher |
| cipher82 is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Most likely, some previous code has overwritten some data needed by malloc - malloc uses a data structure immediately before the memory you get to store some "private" stuff. If free is called, a free will change some of the content in that block of memory, and some errant function may overwrite this block from an existing allocation that has the memory immediately before the freed memory. Next time you call malloc, it happily uses the data stored by free, and kaboom! -- 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. |
| matsp is offline | |
| | #3 |
| Registered User Join Date: Sep 2008 Location: germany
Posts: 2
| Thanks matsp for your advise! After reviewing all the previsous calls of malloc() i saw that one allocated not enough memory for what i was writing to that memory location. Suprisingly the programm survived a few more mallocs util one SegFaults at last ![]() Thanks for your fast help again btw: i heard a few times now that i sould not typcast the returnvalue of malloc. maybe you know the answer why? Code: vertexPNCT* vertPNCT = (vertexPNCT*) malloc ((FloatCount/3) * sizeof(vertexPNCT)); // why is the typecast bad style? Last edited by cipher82; 09-17-2008 at 05:38 AM. |
| cipher82 is offline | |
| | #4 |
| Budding Synth Programmer Join Date: Feb 2002 Location: Trefforest
Posts: 368
| As you're using C++, shouldn't you be using new? I thought malloc was made essentially obsolete with new - but I could be wrong, thought I never use malloc anymore.
__________________ MSVC++ 6.0 |
| samGwilliam is offline | |
| | #5 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- 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. | |
| matsp is offline | |
| | #6 |
| Budding Synth Programmer Join Date: Feb 2002 Location: Trefforest
Posts: 368
| How could new call malloc? New is a keyword, not a function (like sizeof), surely? At least it is on my compiler, though it could be different on others.
__________________ MSVC++ 6.0 |
| samGwilliam is offline | |
| | #7 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
For example, we can imagine a compiler that compiles this: Code: class A
{
...
};
...
A *a = new A;
...
Code: a = malloc(sizeof(A));
a->A(); // call constructor.
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. | |
| matsp is offline | |
| | #8 | |
| and the Hat of Ass Join Date: Dec 2007
Posts: 730
| Quote:
| |
| rags_to_riches is offline | |
| | #9 | |
| Budding Synth Programmer Join Date: Feb 2002 Location: Trefforest
Posts: 368
| Quote:
__________________ MSVC++ 6.0 | |
| samGwilliam is offline | |
| | #10 | |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Quote:
Quite true, matsp. Though I would not advise anyone make codes that take this piece of trivia as a universal rule. Example: Code: int *x = new int; free(x); Code: int *x = reinterpet_cast<int *>(malloc(sizeof(*x))); int *y = new y; free(x); delete y; | |
| master5001 is offline | |
| | #11 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,364
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #12 | |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Quote:
Example: Code: inline template<typename _T>
_T *malloc_ptr(size_t x)
{
return reinterpret_cast<_T *>(malloc(size_t * sizeof(_T)));
}
inline template<typename _T>
_T &malloc_ref(size_t x)
{
_T *ptr = malloc_ptr(x);
if(!ptr)
{
throw std::bad_alloc("Null reference");
}
return *ptr;
}
| |
| master5001 is offline | |
| | #13 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,364
| Quote:
![]() EDIT: hmm... or are they actually provided as an extension by your implementation, since I notice that __g__nothing is unused?
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #14 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| I changed the code to be a little less obtrusive by getting rid of the __g__nothing which was initially just a static pointer where another function could be used poll the return of the second function to determine if it is a null value or not (instead of just casting *(_T *)0). I think its a bit better just to throw an exception and give the programmer a fighting chance to know something went wrong instead of making changes to *(_T *)0. [edit]I changed the code rather quickly so you are definitely a quick one, laserlight.[/edit] |
| master5001 is offline | |
| | #15 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,364
| Quote:
![]() By the way, _T is reserved to the (compiler and standard library) implementation for any use, since "each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use" (ISO/IEC 14882:2003 Section 17.4.3.1.2).
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
![]() |
| Tags |
| malloc, segementation fault, segfault |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| malloc + segmentation fault | ch4 | C Programming | 5 | 04-07-2009 03:46 PM |
| Malloc -segfault | ganesh bala | C Programming | 8 | 02-17-2009 08:08 AM |
| Is there a limit on the number of malloc calls ? | krissy | Windows Programming | 3 | 03-19-2006 12:26 PM |
| Malloc and calloc problem!! | xxhimanshu | C Programming | 19 | 08-10-2005 05:37 AM |
| malloc() & address allocation | santechz | C Programming | 6 | 03-21-2005 09:08 AM |