Segmentation Faults


 
Thread Tools Search this Thread
Top Forums Programming Segmentation Faults
# 1  
Old 02-17-2006
Segmentation Faults

Hello...I am developing a code that wil deal with the string manipulation functions on char arrays which have been declared as char *.

Now whenever I try to use these variables in functions like strcat().strcpy() ...etc,it gives me a segmentation fault error...

I got a way to work around this problem by using malloc() to allocate some memory beforehand.....it works even if a use a statement like

" x=(char*)malloc(0) "

Sometimes i get the correct output even without using malloc...what could be the reason for this erratic occurances of "segmentation fault" in my code...?
# 2  
Old 02-17-2006
Segmentation faults occur when the program tries to access some memory that it isn't supposed to. Often happens when you have a loop that doesn't end when it is supposed to and goes on to try and access unavailable memory.
# 3  
Old 02-17-2006
Code:
/*line 1 */
char *x;  <- this pointer aimed nowhere in particular;
/* line 2 */
char *y=NULL; <- this is better
/* line 3*/
x=malloc(10);  <- if malloc succeeds, x is now aimed somewhere reliable.
/* line * 9999 */
free(x);

When you call a function, the variables you declare at the top of a function (automatic variables) are allocated on the stack. They are plopped on there right over whatever was in memory there before, with no regard to what was there.

Line 1 - x can literally be "pointed" anywhere in memory, because of the fact that it was plopped into already used memory. If by some luck x is aimed at memory you own and can write, then it works -- BUT -- it's extremely likely to corrupt something else.
This is a bad thing - it's called a wild pointer.

Line 2 - y is just fine. It's set to an initial value of NULL, so it's not pointing randomly into memory.

Line 3 - x (assuming malloc does not fail, which it doesn't very often - you should learn to check how to find out if malloc failed) now pints somewhere that you can use.

Line 9999 - be sure the last thing you do is call free - in this case x - for any variable that was malloc-ed earlier.
# 4  
Old 02-18-2006
Quote:
Originally Posted by jim mcnamara
Line 9999 - be sure the last thing you do is call free - in this case x - for any variable that was malloc-ed earlier.
Just a question, it it a good practice to assign NULL to any pointer that you free()? Or does it not really matter?
# 5  
Old 02-18-2006
Quote:
Originally Posted by blowtorch
Just a question, it it a good practice to assign NULL to any pointer that you free()? Or does it not really matter?
After you use free(), you should not dereference a pointer pointing to the area you just freed. The area that was freed is not returned to the OS, it remains available to be reused by the next malloc(). Some people will malloc and then free immediately in a function. Then they go ahead a use the area malloc'ed and figure that they don't need to free() at each return point. I feel this is poor technique.

So I believe that you should never dereference a pointer to an area that has been freed. But what purpose is served by setting the pointer to NULL? You should never dereference a NULL pointer either. I guess it might be argued that, like superfluous parentheses in complex expressions, it makes things a bit clearer to the reader. I rarely will set a pointer to NULL when I'm done with it, but I often initialize a pointer to NULL. I think it is mostly a matter of style.

But it is critical to realize that correct code will absolutely never dereference a NULL pointer.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

2. Solaris

Page faults on OS

Hi guys, I have a zone on a M5000 server running solaris 10. The zone has an SAP application running on it and facing some performance issues. As part of the troubleshooting, I've been recommended to look for any paging on the OS. Please advise how to look for the paging. I've been looking at... (4 Replies)
Discussion started by: frum
4 Replies

3. AIX

AIX 7.1 high page faults

hi guys i hope you can help me with this situation. i have 2 lpar with aix 7.1 and oracle 11gr2 in grid mode. when i start nmon to check the current system health i notice that page fault are over 3000/s. than i have opened a case with ibm and they say that the problem is not paging nor... (10 Replies)
Discussion started by: gullio23
10 Replies

4. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

5. AIX

Lots of page faults on AIX mySQL lpar

Hi, OS = AIX 5.3 Large number of page faults recently start to occure on AIX 5.3 lpar with mysql database installed. I need help in setting AIX OS parameter to solve the paging problem and some guidance on interpreting my stats t Code: # vmstat... (5 Replies)
Discussion started by: crosys
5 Replies

6. AIX

Lots of page faults and free memory

Hello, I've been reading your forums for quite a while and the great amount of information I find here always come in hand.This time however, I need some specific help... I have a doubt with an AIX server which I'm failing to understand as I'm new to its concept of memory management... ... (8 Replies)
Discussion started by: flpgdt
8 Replies

7. AIX

High Page Faults

Sorry my poor english In 570 pseries nmon shows excessive page faults, ascents of something more than 30000 Page faults. System: AIX 5.2 ML5 Processor Type: PowerPC_POWER5 Number Of Processors: 2 Processor Clock Speed: 1656 MHz CPU Type: 64-bit Kernel Type: 64-bit Memory Size: 2816 MB ... (1 Reply)
Discussion started by: daviguez
1 Replies

8. HP-UX

Intransient blocking page faults

Hi, Will anybody tell me what is this 'intransient blocking page faults' in HP-UX, it is in the structure _pst_vminfo in the header file /ust/include/sys/pstat/vm_pstat_body.h? (4 Replies)
Discussion started by: sushaga
4 Replies

9. HP-UX

Copy on Write page faults

Hello Please can you tell me how to access COPY ON WRITE page faults in HP -UNIX. I found the structure in /usr/include/sys/vmmeter with the structure name vmmeter. Please tell me the function to fill the values to this structure and also the arguments for function.:: (5 Replies)
Discussion started by: manjunath
5 Replies
Login or Register to Ask a Question