473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

strange seg fault on stack

I inherited some code and got a segmentation fault on the following
line:

unsigned short localImage[panelRows * panelCols];

It's really baffling and I can't think of what could cause this
problem. The following are debug code I inserted:

cerr << "unsigned short size: " << sizeof (unsigned short) << endl;
cerr << "panelRows: " << panelRows << endl;
cerr << "panelCols: " << panelCols << endl;
cerr << "product: " << panelRows * panelCols << endl;
unsigned short localImage[panelRows * panelCols]; // what's the
problem?
cerr << "sizeof: " << sizeof (localImage) << endl;

DEBUG OUTPUT:

unsigned short size: 2
panelRows: 1024
panelCols: 1024
product: 1048576
Segmentation fault

STACK LIMIT DEBUG CODE:

struct rlimit limit;
getrlimit (RLIMIT_STACK, &limit);
cerr << "Soft Limit: " << limit.rlim_cur << endl;
cerr << "Hard Limit: " << limit.rlim_max << endl;

STACK LIMIT OUTPUT:

Soft Limit: 4294967295
Hard Limit: 4294967295

Anyone see anything obvious that my mind just can't focus on???

Chieh
--
Camera Hacker - http://www.CameraHacker.com/

Sep 8 '06 #1
8 2551
gy*******@yahoo.com wrote:
I inherited some code and got a segmentation fault on the following
line:

unsigned short localImage[panelRows * panelCols];

It's really baffling and I can't think of what could cause this
problem. The following are debug code I inserted:

cerr << "unsigned short size: " << sizeof (unsigned short) << endl;
cerr << "panelRows: " << panelRows << endl;
cerr << "panelCols: " << panelCols << endl;
cerr << "product: " << panelRows * panelCols << endl;
unsigned short localImage[panelRows * panelCols]; // what's the
problem?
cerr << "sizeof: " << sizeof (localImage) << endl;
This is non-standard code. The Stadnard language does not have arrays
with run-time sizes. You need to allocate it dynamically to make it
Standard-compliant, something like

unsigned short *localImage = new unsigned short[...
DEBUG OUTPUT:

unsigned short size: 2
panelRows: 1024
panelCols: 1024
product: 1048576
Segmentation fault

STACK LIMIT DEBUG CODE:

struct rlimit limit;
getrlimit (RLIMIT_STACK, &limit);
cerr << "Soft Limit: " << limit.rlim_cur << endl;
cerr << "Hard Limit: " << limit.rlim_max << endl;

STACK LIMIT OUTPUT:

Soft Limit: 4294967295
Hard Limit: 4294967295

Anyone see anything obvious that my mind just can't focus on???
The only obvious thing is that you're not writing C++ as we know it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 8 '06 #2
gy*******@yahoo.com wrote:
I inherited some code and got a segmentation fault on the following
line:

unsigned short localImage[panelRows * panelCols];

It's really baffling and I can't think of what could cause this
problem. The following are debug code I inserted:

cerr << "unsigned short size: " << sizeof (unsigned short) << endl;
cerr << "panelRows: " << panelRows << endl;
cerr << "panelCols: " << panelCols << endl;
cerr << "product: " << panelRows * panelCols << endl;
unsigned short localImage[panelRows * panelCols]; // what's the
problem?
cerr << "sizeof: " << sizeof (localImage) << endl;

DEBUG OUTPUT:

unsigned short size: 2
panelRows: 1024
panelCols: 1024
product: 1048576
Segmentation fault

STACK LIMIT DEBUG CODE:

struct rlimit limit;
getrlimit (RLIMIT_STACK, &limit);
cerr << "Soft Limit: " << limit.rlim_cur << endl;
cerr << "Hard Limit: " << limit.rlim_max << endl;

STACK LIMIT OUTPUT:

Soft Limit: 4294967295
Hard Limit: 4294967295

Anyone see anything obvious that my mind just can't focus on???
Despite what your OS-specific function getrlimit() is telling you, you
may not be able to allocate that much space on the stack. Try
allocating it dynamically (preferably with a container; cf.
http://www.parashift.com/c++-faq-lit...html#faq-34.1). Or,
try posting on a group related to your platform (cf. the list at
http://www.parashift.com/c++-faq-lit....html#faq-5.9).

Cheers! --M

Sep 8 '06 #3
Despite what your OS-specific function getrlimit() is telling you, you
may not be able to allocate that much space on the stack. Try
Sorry, I forgot to mention that this software is compiled on Linux
using GNU G++ compiler 3.4.3 on Intel x86. If it's any help.
allocating it dynamically (preferably with a container; cf.
http://www.parashift.com/c++-faq-lit...html#faq-34.1). Or,
try posting on a group related to your platform (cf. the list at
http://www.parashift.com/c++-faq-lit....html#faq-5.9).
Thanks, I appreciate it. I understand the good programming philosophy
and have always followed it. However, like I said, I inherited the code
from someone that is no longer around. My task is to make
what-used-to-work work. Or make an assessment on what it would take to
make it work. I appreciate the suggestions. Willing to hear others.
Thanks.

Chieh
--
Camera Hacker - http://www.CameraHacker.com/

Sep 8 '06 #4

Victor Bazarov wrote:
gy*******@yahoo.com wrote:
unsigned short localImage[panelRows * panelCols];

This is non-standard code. The Stadnard language does not have arrays
with run-time sizes. You need to allocate it dynamically to make it
Standard-compliant, something like

unsigned short *localImage = new unsigned short[...
Anyone see anything obvious that my mind just can't focus on???

The only obvious thing is that you're not writing C++ as we know it.
Thanks. Is that a true statement with the standard that GNU G++ 3.4.3
is based on?
We have thought about using the heap as one solution, though it is the
last solution at this time. At this point, we can not make the
determination that the dynamic static allocation is the root of the
cause, because it works find with a smaller number, such as 512000 and
even 1000000.

Is it a bug that GNU G++ allows dynamic static allocation to go through
compilation? Or is it simply not explicitly verified as part of the
standard? Perhaps another way to word the question, is this an
ambiguity in the C++ standard?

Chieh
--
Camera Hacker - http://www.CameraHacker.com/

Sep 8 '06 #5
gy*******@yahoo.com schrieb:
Victor Bazarov wrote:
>The only obvious thing is that you're not writing C++ as we know it.

Thanks. Is that a true statement with the standard that GNU G++ 3.4.3
is based on?
We have thought about using the heap as one solution, though it is the
last solution at this time. At this point, we can not make the
determination that the dynamic static allocation is the root of the
cause, because it works find with a smaller number, such as 512000 and
even 1000000.
What is 'dynamic static allocation'? Is it an oxymoron?

Well, arrays have to be compile-time constant sized in C++.
In C, since C99, there are variable length arrays (VLA), and gcc also
supports this as an extension to C++.

So you have to use std::vector, new[] and friends for this.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 8 '06 #6

Thomas J. Gritzan wrote:
What is 'dynamic static allocation'? Is it an oxymoron?
It's my re-word of Victor's "arrays with run-time sizes" phrase, or
what you call "variable length array" declaration.
Well, arrays have to be compile-time constant sized in C++.
In C, since C99, there are variable length arrays (VLA), and gcc also
supports this as an extension to C++.
So you have to use std::vector, new[] and friends for this.
It looks like I confused you all by introducing variable in array size
declaration.
Let me put the problem in new lights. We've found that using the
following constant numbers, we get different results:

unsigned short localImage [524288]; // does not crash
unsigned short localImage [1048576]; // seg faults

Yes. The number are actually numeric constants as what you see above.
Anyone experienced this interesting phenomenon before?

Chieh
--
Camera Hacker - http://www.CameraHacker.com/

Sep 8 '06 #7
gy*******@yahoo.com wrote:
It looks like I confused you all by introducing variable in array size
declaration.
Let me put the problem in new lights. We've found that using the
following constant numbers, we get different results:

unsigned short localImage [524288]; // does not crash
unsigned short localImage [1048576]; // seg faults

Yes. The number are actually numeric constants as what you see above.
Anyone experienced this interesting phenomenon before?
As mlimber wrote, you might not be able to allocate all the stack memory
that a platform specific function (getrlimit) might report to you.

Since 2 MB is definitly much space for automatic storage, you have two options:

1) Use heap.
2) Ask in a newsgroup about your platform. Perhaps there you'll get a
better answer, what the return value of getrlimit really says, and how to
increase stack size.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 8 '06 #8
<gy*******@yahoo.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>Despite what your OS-specific function getrlimit() is telling you, you
may not be able to allocate that much space on the stack. Try

Sorry, I forgot to mention that this software is compiled on Linux
using GNU G++ compiler 3.4.3 on Intel x86. If it's any help.
>allocating it dynamically (preferably with a container; cf.
http://www.parashift.com/c++-faq-lit...html#faq-34.1). Or,
try posting on a group related to your platform (cf. the list at
http://www.parashift.com/c++-faq-lit....html#faq-5.9).

Thanks, I appreciate it. I understand the good programming philosophy
and have always followed it. However, like I said, I inherited the code
from someone that is no longer around. My task is to make
what-used-to-work work. Or make an assessment on what it would take to
make it work. I appreciate the suggestions. Willing to hear others.
Thanks.

Chieh
--
Camera Hacker - http://www.CameraHacker.com/
Your stack just isn't that big. Your call is returning 4294967295 as stack
size, and that's just the largest unsigned 4 byte int. Think about it, do
you *really* think that your stack is 4gb? Heck, my machine would only go
up to 2gb max (I'm currently running 1gb).

Since the stack is not that big, you have two options. 1. Use a smaller
size. 2. Use free store.

Since you're modifying the code, it seems fairly obvious that you can't use
a smaller size, so you'll need to use new[] and delete[].
Sep 9 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: songfire | last post by:
Hi everyone, This isn't so much a c++ problem as it is a linux problem, but I am hoping someone can point me in the right direction. I have a program that requires a moderately large matrix. I...
7
by: Koen | last post by:
Hi, I am trying to track a segmentation fault (EXC_BAD_ACCESS) that's happening in a program I use on Mac OS X (not my own code). The reason for the fault is that the default stack on Mac OS X...
13
by: tomy | last post by:
Hi all: There is an amazing coredump in my C code, I was thinking it over and over... The code is simple as below: //************************************* const unsigned long int len =...
0
by: Mullai | last post by:
0Hi, My exe comes out with two types of errors like : 1.PG1609VV caused an invalid page fault in module KERNEL32.DLL at 017f:bff9dfff. Registers: EAX=07fbfe38 CS=017f EIP=bff9dfff...
7
by: Sheldon | last post by:
Hello, Can someone please tell me why this program causes a segmentation fault: #include <stdio.h> #include <stdlib.h> #define ROW 1215 #define COL 1215 #define FILENAME...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
5
by: Jeff | last post by:
Okay, I'm still new to vb.net 2005 - throught this was a hardware problem, but now I don't know. (I'm having some problem with my newgroup provider, so hopefully this will go through) This...
17
by: Matt | last post by:
Hello. I've got a very strange problem. Basically I have a programme where I wish to view all the strings in the argv array so I can see what arguments are being passed to the programme. ...
3
by: ciccio | last post by:
Hi, Recently some people I know stumbled upon a strange problem with the following piece of code. This is reduced as far as possible. The idea of the code is simple. There are four structs...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.