LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-04-2012, 02:32 PM   #1
sevs
Member
 
Registered: Jul 2008
Location: Ukraine, Kharkov
Distribution: debian, knoppix, mandriva, asplinux, altlinux
Posts: 110

Rep: Reputation: 20
Question malloc vs new and pointers compare question and debug symbols.


Hi there!

I've got three questions. I already know answers for them but I'm unsure if these answers are right. Though I will not say what answers I think of.

First question is about 'malloc' and 'new' difference:
It is not about difference of "... new calls constructor and malloc does not...".
Simplificated, OS has its memory map with flags for each block saying is it free or not. Does 'malloc' and 'new' refer only to this table or they have such table (translated to OS table) for them only? Saying "for them only" means "separate table for each of 'malloc' and 'new'".

Second question:
Can compare operation on pointers (e.g. code like 'if (p1 == p2)', where p1 and p2 are pointers to different types) be reason for segmentation fault?

Third question:
I debug my application with gdb to catch a place wich calls segfault. gdb tells me 'Your app has segfaulted!' with number of line in source code. Thing is that I have very much of comments int the source saying what is to be done. When I compiled the app are commented lines counted into numeration of source code lines or not?

------- UPD:
P.S.: addition to second question:
I have output in function (a_ok) that calls function (b_bug) wich makes segfault as gdb says. But I have printf ending a newline (test_output_a) in 'a_ok' just before call to 'b_bug' and similiar test output (test_output_b) just before the line said by gdb is buggy. 'b_bug' even has test output (test_start_b), ended with newline at the begining of 'b_bug'.
Thing is: 'test_output_a' is printed. Neither 'test_output_b' nor 'test_start_b' is printed.

------- UPD2:
At some point some other application crashes. But if I put a 'printf' it appears to not crash at that point. Is there any serious reason?

Thanks in advance sorry for bad English.

Last edited by sevs; 09-04-2012 at 04:09 PM. Reason: Forgot to say: and once more.
 
Old 09-04-2012, 06:16 PM   #2
abarclay
LQ Newbie
 
Registered: Aug 2003
Posts: 25

Rep: Reputation: 5
malloc has nothing to do with the OS.... malloc (and free) are library calls. When a program calls malloc, malloc looks for an area on the programs heap which is large enough to hold the requested amount of memory. If it finds such a region, it will return a pointer to it. If it does not, it will call the brk() system call to ask the OS to allocate more memory to the program. All that free() does is add the allocated block to the "free list" which malloc uses. The memory is never returned to the OS until the program exits. I end up arguing with customers all the time when they state - "hey, your program has a memory leak because I show it using 1 gb of RAM when it first starts, but then it grows to 2 GB by the end of the week.". This is not a leak....

new is almost the same as malloc in that it reserves enough space for the object in memory (it probably even calls malloc under the covers. If it didn't, then calling malloc() and new() in the same program could potentially return the same chunk of memory.
The only difference I can think of is that new() might have to add a reference to the object to some list so that the garbage collector knows it can clean it up when the reference no longer exists.

Comparing two pointers will not cause a segfault.

gdb takes comments into account when printing line numbers.

Last edited by abarclay; 09-04-2012 at 06:21 PM. Reason: add more
 
1 members found this post helpful.
Old 09-04-2012, 06:32 PM   #3
sevs
Member
 
Registered: Jul 2008
Location: Ukraine, Kharkov
Distribution: debian, knoppix, mandriva, asplinux, altlinux
Posts: 110

Original Poster
Rep: Reputation: 20
Thanks. That clears things enough.
Although, I already found the bug. I called free() on to mem I used later for read and write. Dunno if it is ok to read from from free()'d memory but to write is a bad thing.

Anyway, thanks.
 
Old 09-05-2012, 03:47 AM   #4
padeen
Member
 
Registered: Sep 2009
Location: Perth, W.A.
Distribution: Slackware 14, Debian 7, FreeBSD, OpenBSD
Posts: 177

Rep: Reputation: 34
Additionally regarding gdb, you use the 'bt' command to print a backtrace of the crash which shows which function caused it, and you use the 'l LINENUM' command to print out the lines around LINENUM.

About a_ok, might be best to show us the code for both functions.

Last edited by padeen; 09-05-2012 at 03:50 AM.
 
1 members found this post helpful.
Old 09-05-2012, 04:24 AM   #5
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,838

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
Quote:
Originally Posted by sevs View Post
Dunno if it is ok to read from from free()'d memory but to write is a bad thing.
no, it is not safe at all. Probably that memory is already in use again (and it may be used by another process occasionally). In such cases the app will be died.
 
1 members found this post helpful.
Old 09-05-2012, 08:33 AM   #6
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,381

Rep: Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109
The most common reason for segfaulting and such is a "double free." You free a pointer that was never allocated or free the same block of memory twice. Another common cause is a "stale pointer," where a pointer has a non-NULL value but it no longer points to an active block of storage. If that block has been released to the OS, a segfault can occur.
 
1 members found this post helpful.
Old 09-05-2012, 08:37 AM   #7
geox
Member
 
Registered: Jan 2012
Posts: 40

Rep: Reputation: 2
Never use delete on malloc'ed resources or viceversa!

It might work for your specific situation and/or platform but at some point in time it will cause problems. Just dont mix them up.
 
1 members found this post helpful.
Old 09-05-2012, 09:43 AM   #8
abarclay
LQ Newbie
 
Registered: Aug 2003
Posts: 25

Rep: Reputation: 5
Pan,
Can you give an example of how a chunk of memory could be in use by another process after being freed? The only way I can think of is the case of a shared memory segment, but you don't use free on shared memory....

Sundial, I can't think of a way that a double free would cause a seg fault (I just tested it). A segfault happens when a program tries to access memory which is not in its memory space.
Here is an example. Setting j to point at -1 points it at the top end of the address space, then when I try to access it with printf,
it seg faults.
Code:
#include <stdlib.h>
#include <stdio.h>
void main()
{
        int *i=0;
        int *j=-1;

        if (i == j) {
        }
        printf("%d",*j);
}
Geox, what is delete? I'm not familiar with that system/library call.

Last edited by abarclay; 09-05-2012 at 10:08 AM. Reason: address additional concern
 
1 members found this post helpful.
Old 09-05-2012, 10:09 AM   #9
geox
Member
 
Registered: Jan 2012
Posts: 40

Rep: Reputation: 2
Well, there is malloc/free and new/delete
malloc/free is the C way, new/delete the C++ way.

Quote:
Can you give an example of how a chunk of memory could be in use by another process after being freed?
Well, if you have multiple threads for instance. Single threaded...no. But using memory after you free'd it is still a bad idea.


Quote:
I can't think of a way that a double free would cause a seg fault (I just tested it)
It all depends on 1) the order in which you free/alloc new and old areas, and 2) your libc or whatever lib implements your malloc/strbrk routines. But usually an allocated block of memory has some adminstration area at the end of allocated memory. If you free it twice this area most probably holds incorrect information. But, dont take my word for it. xD

And, to answer your questions:
Quote:
First question is about 'malloc' and 'new' difference:
The OS has a completely different view of memory for your application. Malloc/new only work on a small part of that, known as the heap. The heap is somewhere in memory (physical/swapped or even fragmented) but to the application it looks like one contiguous chunk of memory. Malloc keeps its own administration inside this heap. New does the same only slightly different (really depends on the library implementing it).

Quote:
Second question:
Can compare operation on pointers (e.g. code like 'if (p1 == p2)', where p1 and p2 are pointers to different types) be reason for segmentation fault?
Only comparing the pointers themselves, not the contents of it, cannot generate a segfault.

Quote:
Third question:
I debug my application with gdb to catch a place wich calls segfault. gdb tells me 'Your app has segfaulted!' with number of line in source code. Thing is that I have very much of comments int the source saying what is to be done. When I compiled the app are commented lines counted into numeration of source code lines or not?
First off all, your app does not call segfault, segfault calls you
And yes, gdb does keep your comments in mind. Look at .lst files your compiler generates how it does that. So the line numbers in gdb are correct.

Last edited by geox; 09-05-2012 at 10:13 AM.
 
1 members found this post helpful.
Old 09-05-2012, 11:36 AM   #10
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,838

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
Quote:
Originally Posted by abarclay View Post
Pan,
Can you give an example of how a chunk of memory could be in use by another process after being freed?
hm, I wanted to show you an example, but really hard. So here are some links I found:
http://stackoverflow.com/questions/1...cess-to-the-os
and also sbrk can be called with negative parameter: https://www.gnu.org/software/libc/ma...arameters.html, also mentioned, sometimes mmap used automatically instead of malloc. (not to speak about the case when the original memory manager replaced)

But good enough to say: a freed part of the memory can be overwritten also in a single threaded app (for example in another function).
 
1 members found this post helpful.
  


Reply

Tags
gdb, malloc, memory


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
debug symbols dudkosk Linux - Newbie 3 07-28-2011 07:42 AM
[SOLVED] KDE debug symbols igadoter Slackware 2 06-27-2010 02:21 AM
[SOLVED] C - Segfault during malloc. Something to do with how I'm using the pointers.... golmschenk Programming 10 03-23-2010 09:55 PM
malloc array of pointers for platform independence jiml8 Programming 12 08-27-2008 05:53 PM
Building with debug symbols? Omni Programming 3 08-29-2006 08:11 AM


All times are GMT -5. The time now is 05:21 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration