Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 No Access Violations, Then Access Violations.
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
copthesaint




PostPosted: Sat Nov 30, 2013 11:47 pm   Post subject: No Access Violations, Then Access Violations.

Be aware, I am using malloc and free, and will not be using new and delelte. Any such comments will not be taken into consideration.

So I'm having fun trying to figure out why I cannot get access to my method "Count()" which just returns the size of my array. When I construct it and then try to call count everything works fine, If I then try to destruct my class and reconstruct it the same way, I can no longer call count. I'll post the files below with the full code. Normally I try and figure out these problems, however I'm kinda running out of ideas of how to solve this, and it may be that I just need a second set of eyes.

Code in main:

c++:

GameEntity myEntity = GameEntity(5);
//IF THIS RUNS IT BEARKS
//myEntity.~GameEntity();
//myEntity = GameEntity(5);

cout << "Parent: " << myEntity.Count() << endl << endl;
for (int i = 0; i < myEntity.Count(); i++){
        cout << "Child " << i << ": " <<myEntity.Child(i) << " = ";
        cout << (myEntity.Child(i))->Count() << endl;
}


Code in GameEntity.cpp for creating the array of pointers. This array is called in Child(int index) method.

c++:
//Create the new pointer array for the child
        GameEntity** tempChildrenPointers = (GameEntity**)malloc(m_ChildrenSize * sizeof(GameEntity*));
        if (tempChildrenPointers == 0){
                //if could not malloc
                m_ChildrenSize = 0;
                return; //Even though new array would be created, the final one is not set yet and is still null, should not have any advese effects.
        }
        //Init the new values
        for (GameEntity** temp = tempChildrenPointers; temp < tempChildrenPointers + m_ChildrenSize; temp++){
                *temp = tempChildren; //May have some access issues, we'll find out when it runs
                tempChildren++;
        }
        //Do not need to free any old values
        //Set the new value
        m_ChildrenPointers = tempChildrenPointers;


Code in GameEntity.h

c++:
class GameEntity {
public:
        GameEntity();
        GameEntity(int children);
        ~GameEntity();

        GameEntity* AddChild();
        GameEntity* AddChild(GameEntity* child);

        GameEntity** Children();
        GameEntity* Child(int index);
        int Count();
       
private:
        GameEntity(GameEntity* copy);//copies a GameEntity
        void Mirror(GameEntity* mirror);//Creates a clone of an GameEntity

        GameEntity* m_Children;
        GameEntity** m_ChildrenPointers;
        int m_ChildrenBound;
        int m_ChildrenSize;     
        bool m_Mirrored;
};



Files.zip
 Description:
Added Nov 30th, 2013

Download
 Filename:  Files.zip
 Filesize:  2.23 KB
 Downloaded:  265 Time(s)

 
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Sun Dec 01, 2013 2:00 am   Post subject: RE:No Access Violations, Then Access Violations.

I believe your issue is caused by this line not doing what you expect:

code:
GameEntity myEntity = GameEntity(5);


Note that the same pattern appears several times in your code, and I'm pretty sure it's wrong everywhere. See this for details: http://stackoverflow.com/questions/2722879/calling-constructors-in-c-without-new

When I replaced that initial bit with the following, it worked without exploding (doesn't mean it's bug-free, I'd be suspicious of some of the code in GameEntity.cpp too):

code:

GameEntity myEntity(5);
myEntity.~GameEntity();
new (&myEntity) GameEntity(5);


Edit: this code would not pass a code review, even ignoring the malloc/free vs new/delete issue.
1) Your copy-constructor does not copy-construct.
2) You have no definition of mirroring, how to use it, what it's for, etc. I have no idea whether the code conforms to the spec, since no spec exists.
3) Your comments all describe literally what the line said, not the reasoning behind it, and are therefore worse than useless (they're just one more thing to break if you have to change the code).
4) Your constructor does not emit any kind of notification in the case of allocator failure, and the class provides no standard way to ask whether the instance is valid.
 
Raknarg




PostPosted: Sun Dec 01, 2013 2:16 am   Post subject: RE:No Access Violations, Then Access Violations.

I've never used C before, and I've heard "malloc" used many times. What does it do, and why is it bad coding practice?
 
copthesaint




PostPosted: Sun Dec 01, 2013 6:13 am   Post subject: Re: RE:No Access Violations, Then Access Violations.

DemonWasp @ Sun Dec 01, 2013 wrote:


Edit: this code would not pass a code review, even ignoring the malloc/free vs new/delete issue.
1) Your copy-constructor does not copy-construct.
2) You have no definition of mirroring, how to use it, what it's for, etc. I have no idea whether the code conforms to the spec, since no spec exists.
3) Your comments all describe literally what the line said, not the reasoning behind it, and are therefore worse than useless (they're just one more thing to break if you have to change the code).
4) Your constructor does not emit any kind of notification in the case of allocator failure, and the class provides no standard way to ask whether the instance is valid.



Due to some "feature", this may appear after the second quote.
1) I haven't written it because I haven't solved my first issue.

2) If you understand what I'm trying to do, can you post something explaining what I should do? Even just a link to an article that you believe is a good source would be helpful.

4) This is true, not yet sure how I want to go about this in my constructors. However that being said, I don't need to check if there is an allocation failure for my computer at the moment, because the excessive amount of memory has never caused an error. Not saying that my logic is right, just justifying my reasoning.


DemonWasp @ Sun Dec 01, 2013 wrote:

I believe your issue is caused by this line not doing what you expect:

code:
GameEntity myEntity = GameEntity(5);


Note that the same pattern appears several times in your code, and I'm pretty sure it's wrong everywhere. See this for details: http://stackoverflow.com/questions/2722879/calling-constructors-in-c-without-new

When I replaced that initial bit with the following, it worked without exploding (doesn't mean it's bug-free, I'd be suspicious of some of the code in GameEntity.cpp too):

code:

GameEntity myEntity(5);
myEntity.~GameEntity();
new (&myEntity) GameEntity(5);


Can't believe that not a singe professor in my courses have ever even mentioned this -_- so useful to know.


Raknarg @ Sun Dec 01, 2013 wrote:
I've never used C before, and I've heard "malloc" used many times. What does it do, and why is it bad coding practice?

I believe that the "Bad coding practice" is debatable. The most important thing to know, its fast, its harder to use and, If you use it incorrectly, it can kill your computer.
 
Insectoid




PostPosted: Sun Dec 01, 2013 9:51 am   Post subject: RE:No Access Violations, Then Access Violations.

Quote:
I've never used C before, and I've heard "malloc" used many times. What does it do, and why is it bad coding practice?


malloc is bad practise in C++, not C. malloc is a C function. In C++, this was replaced by the operator new. Copthesaint is under the impression that malloc is fast, though unless you're using it in a very specific manner, it isn't.

new is only slower than malloc because it calls an object's constructor after it allocates memory. This is moot, however, because in C++ you're probably going to call the constructor anyway.

The only time malloc is faster than new is if you want to allocate a huge chunk of memory right away so that you don't need to allocate it while the program is actually doing something, because sometimes malloc has trouble finding a big enough chunk of free memory to use, which causes the program to stutter. If you do this, however, you're forced to manage the memory chunk yourself and your method might be far slower than letting C++ handle that. If you're that worried about speed though, and you already know how much RAM you need, and you're not worried about having all of your RAM allocated at once, you might as well just create all of your objects with new right away and never delete them; just deconstruct/reconstruct as necessary.
 
DemonWasp




PostPosted: Sun Dec 01, 2013 3:51 pm   Post subject: RE:No Access Violations, Then Access Violations.

@Raknarg: malloc is just "Memory ALLOCate". It's one of a family of standard functions used in C to manage memory (malloc, calloc, free, etc). It's not bad practice on its own, it's just that it's the least-safe way to allocate memory because it relies on you to do all the work. You can read more about its drawbacks on its wikipedia page (http://en.wikipedia.org/wiki/C_dynamic_memory_allocation), but suffice it to say that there's a reason it was replaced with 'new' in C++ (not that C++ uses a particularly safe anything, but it's still safer than allocating memory manually).

There are a few cases where you want to allocate memory in a special way to suit what your program is doing, all of which boil down to maintaining cache performance by keeping associated information in the same blocks of RAM (if you don't understand that, read the "Memory Hierarchy" post in my sig).

Since 'new' doesn't do that on its own, it can generate "slow" code, if only because it can scatter allocations throughout RAM. This is part of why linked lists are awful - element 1 and element 2 may be on opposite sides of RAM, which may require paging in and out once per link traversed. Worse, if virtual memory is involved, it hugely increases the chances of hitting the hard drive.

It's important to note that the definition of malloc is standard, but the exact behaviour is not standardized. There are several different allocators that will generate better allocation patterns for certain circumstances. The default GNU one generates reasonable allocation patterns if you initialize objects in an order similar to their use pattern, especially if allocations are ordered by decreasing size (I think). There's also tcmalloc, which apparently generates a better pattern for multithreaded programs.

The only thing here that's bad practice is that copthesaint seems to be chasing a relatively minor performance boost without any real evidence that the work being done will deliver the improvement he hopes for.

@Cop: I have no idea what you mean by "mirroring", so I can't provide any advice.

If you don't want to implement a constructor yet, make it throw an exception so that you can tell when it's getting unintentionally used (like your copy constructor) or mark it as deleted (only applicable for C++11).

You may want to have your constructors / destructors print out (or log, if you have a logging framework) which addresses were created / destroyed so that you can debug errors). Maybe a DEBUG_PRINTF() macro would suit you: it only prints if debugging is on, and doesn't otherwise.
 
copthesaint




PostPosted: Sun Dec 01, 2013 8:03 pm   Post subject: Re: RE:No Access Violations, Then Access Violations.

DemonWasp @ Sun Dec 01, 2013 wrote:

The only thing here that's bad practice is that copthesaint seems to be chasing a relatively minor performance boost without any real evidence that the work being done will deliver the improvement he hopes for.


Just because I'm testing and I'm trying to learn how to properly allocate free and reallocate memory in this file, doesn't mean I am going to be using this in my main project. Yes guys I am eager to implement this into my working project, but I'm not simply going to rush ahead without even learning the right way to use malloc. Why do I need to implement some faster way of managing my memory now when I am having issues simply with my constructor for example. When I have something simple that works, most importantly, then I'm going to expand on what I've learned.

That aside, I really do appreciate the help. It's really interesting to me and I do want to learn about these older technologies.
 
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: