ghostbody / 15-cpp-learning

15 c++ learning repository of SDCS SYSU
6 stars 3 forks source link

Memory Error Summary #1

Open ghostbody opened 8 years ago

ghostbody commented 8 years ago

A small summary for 15 C++ learning

What's the problem with my program? Why eden fetch me a lot of error information?

image

Answer: Eden, the online judge system uses a memory check tool named "valgrind" for grading assignment. There are several kinds memory errors you will open meet.

1. Memory Leak: The picture below show this error. It's your mistake that a block of memory become unreachable when you take pointer operations. This will only take place with heap allocation when you are setting a pointer to point to another block of memory while ignoring the legacy memory.

For example, in a list:

head = head->next;

The memory block which was pointed by pointer head is definitely lost after the assign operation above.

2. Invalid read or write of memory: This kind of mistake is very common because c++ compiler will not check invalid memory access. Mostly, you are accessing an element of an array of which the index is out of range.

image

For example:

int * a = new int[5]();
a[5] = 0;

a[5] is an invalid element of the array. And valgrind will throw "Invalid write of size 4.... Address 0x5202054 is 0 bytes after a block of size 20 alloc'd". This means that the address a+5 is not allocated by statement int * a = new int[5](). Also you will get runtime error in this case.

3. Condition jump or move depends on uninitialized variables. This means that some of your conditional statements, i.e. for loop, if statement or etc. use uninitialized variables.

For example:

int * a = new int[5]();
if(a[0] == '0') {
  cout << "Bad Ta!" << endl;
}

First of all, if the programmer forget to call delete a, it will cause a memory leak error. Well, the if statement above uses a[0], which has not been initialized. Then valgrind will detect and report the error for you.

4. Mismatched new/malloc and delete/free. As we all know, when you are writing c++ code using dynamic memory allocation, you are supposed to manage the heap memory by your self. New and delete are "paired operators" which means that when you call 3 of new, you should call 3 of delete after that.

For a mismatch example:(Tecent recruitment examination)

int * a = new int[10];
free(a);

This will cause a mismatched error in valgrind. Why?Think about what's difference between new and malloc as well as delete and free? (hint: constructor and destructor)

How can I fix these issues?

Basically, you should know the concepts in c++ programming, or you will know nothing when you meet these issues.

After that:

Establishing good coding habits. A good coding style does not indicate that you pass the google style check in the system. This is not good enough.You should always be careful when you meet a pointer which is an annoying but powerful tool and you should remember to delete the pointer when you are using heap allocation to get memory.

Be careful to these parts of a class:

  1. default constructor in which you are supposes to initialize the member variables. Note that the compiler will automatically add an default constructor if you do not defined one.
  2. copy constructor in which you should think about show copy and deep copy and also, you should remember to initialize the member variables because this is also a constructor.
  3. assign operator which is similar to the constructors while you need to think about to clear the legacy content of "this" object. Also remember the condition that the parameter of the assign operator is the object itself, i.e. determine whether this == &another or not.
    Later, if I have time, I will choose some codes from your exercises submissions to put here for you to have a discussion "What is good style of coding?"
  4. Using memory check tool. We can use memory check tools like valgrind. In fact, it's a powerful memory check tool. I remember valgrind can be simply installed in unbuntu using apt-get. You can referenced to this website for more details. Don't be annoyed when you see a lot of English words, please be patient to read it through.

valgrind

It's quite easy to use this tool, just compile you code and then run the program with a system argument which should be your executable program name.

valgrind ./a.out

or you can have more options, like to check full memory leak.

valgrind --leak-check=full ./a.out

or

valgrind -v ./a.out

check valgrind -h for more details.

you can see a lot of information after running the program.

Be patient when you meet errors. This is a test that must be experienced before you become a supper programmer in c++.

If you have any questions, please comment below. I do not open see my QQ because there are too many messages.

Or you can send me a email which is also filled with unread emails....

If you have any experience, you are more than welcome to make an issue.

Happy coding...

jianjieluo commented 8 years ago

Let me be the first one to leave some commets to +7 ta. Recently I modified my hosts file in my win7 os with ipv6 files and was able to access to google.hk. However, I can't make it in my VMware Ubuntu systerm. Can you tell me what should I do? (there are maybe some English grammer faults but I think +7 ta can understands it clearly :))

ghostbody commented 8 years ago

Well, I met the same problem, too. The reason is that the network configure of your VMware is not correct.

image

ipv6 does not support nat in ipv4, because ipv6's is a network protocol which promise that each device can get an ip address rather than NAT.

Solution: Share the same ip address with you host machine. Or just use a linux system without virtual.

@longjj

jianjieluo commented 8 years ago

OK, thanks, I will try it in the following several days!

@ghostbody

mgsweet commented 8 years ago

@longjj Can you follow me? just want to have some followers 23333 damn it! how to use emoji...

jianjieluo commented 8 years ago

Interesting...maybe you can also let +7 to follow you hhhh..... Ok, I have already followed you . _> Enjoy coding guy!

@mgsweet

mgsweet commented 8 years ago

@ghostbody why the document C is different when i push it to github?

2016-04-08 11 44 13
KatharinaLin commented 8 years ago

eden died.I'm so sad~

ghostbody commented 8 years ago

@mgsweet Well, I have not known what the icon mean yet, but it looks so wired. Maybe an empty folder, I guess....

ghostbody commented 8 years ago

@KatharinaLin It works now.

mgsweet commented 8 years ago

@ghostbody

are you sure node& operator=(const node &another) { this->data = another.data; this->next = another.next; } in the list.hpp don't need to have a return?

ghostbody commented 8 years ago

@mgsweet Because I don't need a cascade call.

KatharinaLin commented 8 years ago

I want to ask a question:why the report of memory is different? virtualbox_25_10_04_2016_21_42_32

KatharinaLin commented 8 years ago

It's OK now .But I don't know why.

ghostbody commented 8 years ago

@KatharinaLin I have met this problem, too. And I think it's a problem of eden. Because I use valgrind to check and no problem found using "all checking mode". Maybe you can use --leak-check=full to try again.

KatharinaLin commented 8 years ago

o,thank you~

t617 commented 8 years ago

so nice~

KatharinaLin commented 8 years ago

I have met this problem before, but this time it seems different from the last time.I can't find the problem.Can you help me ? 16 05 10 2232_3 16 05 10 2231_2 16 05 10 2231_1

KatharinaLin commented 8 years ago

I don't know why... virtualbox_25_11_05_2016_17_52_58 virtualbox_25_11_05_2016_17_54_41 virtualbox_25_11_05_2016_17_55_12 virtualbox_25_11_05_2016_17_55_21