The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
segmentation fault rockgal High Level Programming 8 12-05-2006 09:16 AM
Segmentation Fault compbug UNIX for Dummies Questions & Answers 3 04-21-2006 07:43 AM
segmentation fault wojtyla High Level Programming 3 02-19-2005 11:53 AM
Segmentation fault jshaulis AIX 1 06-01-2004 01:16 PM
segmentation fault omran High Level Programming 2 08-01-2003 05:19 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 05-08-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 250
Unfortunately the libraries in use may confuse the issue for you.
This code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>



void handlesegfault(int sig) {
                 printf("Segv detected..exiting.\n");
                 exit(1);
}

int main(void) {
int y = 0;
char x[12];

               signal(SIGSEGV,handlesegfault);
               while (1) {
                         bzero(x,(y++));
                         printf("%s at %d\n",x,y);
               }
}
..never segfaults under linux/*BSD with recent glibc. instead it overwrites the count variable starting over...Sometimes it's better to show that an implementation is wrong theoretically and logically rather than depend on implementations to fail dramatically.
Reply With Quote
Forum Sponsor
  #9  
Old 05-08-2008
Registered User
 

Join Date: May 2008
Posts: 35
Hi ramen_noodle,

It is a very interesting example. I didn't know this type of glibc "protection". Is there any way to disable this behavior?? perhaps a gcc parameter?

Thanks a lot!
Reply With Quote
  #10  
Old 05-12-2008
Registered User
 

Join Date: May 2008
Posts: 35
Hi

Is there a minimum size of the stack in a C program?
If I have only one variable (type int) in a function, what will be the size of the stack?

I know that it depends on the implementation but, is there an standard size?

Thanks!!!
Reply With Quote
  #11  
Old 05-12-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
The stack frame is one page minimum. the pagesize command is found on a lot of machines, try it. If you do not have it you'll have to run something like this:
Code:
#include <unistd.h>
int main()
{
     printf("%d\n", getpagesize() );
     return 0;
}
if your system does not have getpagesize(void) you can use sysconf(_SC_PAGESIZE).
Reply With Quote
  #12  
Old 05-12-2008
Registered User
 

Join Date: May 2008
Posts: 35
Hi

Thank you very much!! you know a lot of Linux programming!

Do you think that my problem may be because of the size of the stack??

My problem is in the first post of this thread, and the question is "Why 2 bytes are not enough to get a segmentation fault message ?? "

Thanks again!
Reply With Quote
  #13  
Old 05-12-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
In your case SIGSEGV is caused by a stack overflow. The size of each stack frame varies by OS since it is allocated by the processor at runtime. It is not the same as the system pagesize. Max size of a stack frame is determined by the number of bits taken up by the displacement field. On AIX the stu/stwu instruction pushes a frame onto the stack which has 16 (signed) bits allocated giving a max size of 32K. Generate assembly code listing of your C program and figure out which instruction is used to allocate a frame on the stack. If it uses mnemonics like SP (stack pointer) then it might be easy to decode else compiler generated assembly is confusing. Read more about it here
Reply With Quote
  #14  
Old 05-13-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
You can also get stack frame size from cc -s myfile.c by address subtraction.
And one some systems the minimum stack frame is one page - you can actually allocate stackframes for pthreads, if you're willing to suffer through it, as well as set the size of the stack and the address of the stack. eg., pthread_attr_setstacksize(), pthread_attr_setstackaddr(), pthread_attr_getstackaddr()....

glibc backtrace() with some programming examples to print the stack frame in C using glibc:
Stack Backtracing Inside Your Program
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:01 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0