473,385 Members | 1,753 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,385 software developers and data experts.

A quit strange coredump

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 = 102400000;
int array[len];
unsigned long int i;
for(i=0; i<len; i++)
{
array[i] = 0; //The coredump line;
}
//*************************************

with gdb, when variable i came to 0, the "array[i]=0", the process
received segmentation fault and left coredump. Is there any wrong in
THAT line? It seems all right, with the condition i=0.

--tomy

Jun 11 '06 #1
13 1941
tomy schrieb:
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 = 102400000;
int array[len];
unsigned long int i;
for(i=0; i<len; i++)
{
array[i] = 0; //The coredump line;
}
//*************************************

with gdb, when variable i came to 0, the "array[i]=0", the process
received segmentation fault and left coredump. Is there any wrong in
THAT line? It seems all right, with the condition i=0.


Please break your code down to a minimal compiling example that
still exhibits the problem. Post it here and state whether you
are using C99 or C90.

As you seem to be using gcc, turn the warning level up:
gcc -std=c99 -pedantic -W -O yourcode.c -c
Note: Your code cannot be C90 code (-std=c89 or -ansi) as len
is not a compile time constant which is required for array sizes
in C90. (const does not provide compile time constants in C.)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 11 '06 #2
Ico
tomy <to******@gmail.com> wrote:
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 = 102400000;
int array[len];
unsigned long int i;
for(i=0; i<len; i++)
{
array[i] = 0; //The coredump line;
}
//*************************************

with gdb, when variable i came to 0, the "array[i]=0", the process
received segmentation fault and left coredump. Is there any wrong in
THAT line? It seems all right, with the condition i=0.


Please provide a complete, standalone, compilable example, instead of
just a snippet. Your post does not make it clear if array[] is an
automatic variable; if this is the case, it's size of nearly 100 MB
might just be too much on your platform.
--
:wq
^X^Cy^K^X^C^C^C^C
Jun 11 '06 #3
On 2006-06-11, tomy <to******@gmail.com> wrote:
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 = 102400000;
int array[len];
unsigned long int i;
for(i=0; i<len; i++)
{
array[i] = 0; //The coredump line;
}
//*************************************

with gdb, when variable i came to 0, the "array[i]=0", the process
received segmentation fault and left coredump. Is there any wrong in
THAT line? It seems all right, with the condition i=0.


I think this program should be OK, assuming it's C99 and assuming your
machine's stack is big enough. The array is rather a big object to put
on the stack, and the coredump may well be caused indirectly by a stack
overflow.
Jun 11 '06 #4
Ben C <sp******@spam.eggs> writes:
On 2006-06-11, tomy <to******@gmail.com> wrote:
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 = 102400000;
int array[len];
unsigned long int i;
for(i=0; i<len; i++)
{
array[i] = 0; //The coredump line;
}
//*************************************

with gdb, when variable i came to 0, the "array[i]=0", the process
received segmentation fault and left coredump. Is there any wrong in
THAT line? It seems all right, with the condition i=0.


I think this program should be OK, assuming it's C99 and assuming your
machine's stack is big enough. The array is rather a big object to put
on the stack, and the coredump may well be caused indirectly by a stack
overflow.


100 megabytes is bigger than the stacksize limit on the machines I use.

Even though "len" is declared const, len is not a constant expression.
That means that
int array[len];
declares a VLA (variable length array, a new feature in C99). VLAs
can be convenient; their biggest drawback, IMHO, is that no mechanism
is defined for reporting an allocation error. The same is true for
any auto (i.e., local) variable; the problem with VLAs isn't really
new, but it's much more visible because you're more likely to run into
it when you don't know the size at compilation time.

If a VLA is too big, the result is undefined behavior. A segmentation
fault on the first attempt to access the VLA is a plausible way for
the undefined behavior to manifest itself.

BTW, I tried a program that incorporated the above code, and it died
with a segmentation fault, though the debugger wasn't very specific
about where it occurred (on one system, it reported that it had
happened on a line containing a printf() before the loop). When I
used a constant expression rather than a const object for the array
length (so it's no longer a VLA), I got the same results.

What you're seeing is probably a stack overflow, and the language
doesn't define a good way to handle those. If you want an object that
big, try to allocate it with malloc(); check the result of malloc() to
see if the object was allocated.o

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 11 '06 #5
tomy wrote:
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 = 102400000;
int array[len];
unsigned long int i;
for(i=0; i<len; i++)
{
array[i] = 0; //The coredump line;
}


You're shoving 390 MB on the stack, and expect it to work ?

Igmar
Jun 12 '06 #6
Many Many Thanks
I am a new guy to the c language, and learn a lots from your posts.

The array is allocated in the stacks, and overflow with segmentation
fault. I try to remove the array to the global scope (or malloc() to
the heap mem.), and it works fine there.

But I still hold a question: What the exactly amount of stack memory
the process can take. On the 32bit machine, the stack's virtual memory
is like 0x4000000 ~ 0xc000000, that's 800M.isn't it? My computer has
500M(add 1G swap memory)physically, however I could only allocate about
110M in the stack. that's funny? And How could i find the fresh memory
to use, without maintain the total amount?

Hope that won't bother you to much. Many Many thanks again.

--tomy

Jun 12 '06 #7
In article <11********************@y43g2000cwc.googlegroups.c om>,
tomy <to******@gmail.com> wrote:
But I still hold a question: What the exactly amount of stack memory
the process can take. On the 32bit machine, the stack's virtual memory
is like 0x4000000 ~ 0xc000000, that's 800M.isn't it?
Depends on the operating system. On the 32 bit machine I'm using
now, I could probably push the stack up to 1.2 Gb, possibly even more.
My computer has
500M(add 1G swap memory)physically, however I could only allocate about
110M in the stack. that's funny?
Operating system dependant. Stacks are sometimes placed below shared
libraries, with the heap being above the shared libraries; on such
systems, the available room for the stack or heap would depend on
the shared library sizes and positions. And of course not all systems
use stacks or heaps.
And How could i find the fresh memory
to use, without maintain the total amount?


If you want to know how much more memory you could allocate, then the
only portable way is to keep allocating memory until you run out.
Unfortunately doing that might have adverse effects on your program or
other things happening on your system, so that's not a very good
method.

Questions about available memory to allocate are often fairly
complex to answer; you really need someone who specializes in
your platform (i.e., in a different newsgroup.)
--
All is vanity. -- Ecclesiastes
Jun 12 '06 #8
I am sorry, just make a mistake. I come to check the allocating amount
again.
As last post mentioned, the VM of stack seems to be 0x40000000 ~
0xc0000000 , which should be 2GB. Right?

And How could you come to the point 390M?

I try the code below
//*****************************************
#define len 10480000 //about 10M

int main()
{
unsigned long int i;
char array[len]; //allocate 10MB
for(i=0; i<len; i++)
{
array[i] = 0; //The coredump line;
}
}
//*******************************************
That works fine! However just 10M, above it, coredump again. OH~~

--tomy

Jun 12 '06 #9
In article <11**********************@j55g2000cwa.googlegroups .com>,
tomy <to******@gmail.com> wrote:
I am sorry, just make a mistake. I come to check the allocating amount
again.
As last post mentioned, the VM of stack seems to be 0x40000000 ~
0xc0000000 , which should be 2GB. Right?
0x40000000 ~ (0xc0000000 - 1) would be 2 gigabytes in a linear
address space, but that might not a relevant limit. For example on
the 32 bit system I am using right now, all addresses from
0x80000000 to 0xFFFFFFFF are reserved for the operating system and
cannot be allocated to user processes. And there might be a lot of other
factors that affect maximum stack size. You really need to consult
specialists in your platform.

And How could you come to the point 390M?


Please quote context. Please see here for information on how to
do so from Google Groups: http://cfaj.freeshell.org/google/

I cannot tell what the 390M figure is in response to. In the
postings that have made it to my system, no-one has mentioned
390Mb until you. If you are asking how you could get 390 Mb allocated
to the stack for your process then you need to read up on the
details for your platform. The C standard does not even have stacks,
so the C standard discussed in this newsgroup cannot answer any
questions about managing stack allocations on your platform.
--
Prototypes are supertypes of their clones. -- maplesoft
Jun 12 '06 #10
"tomy" <to******@gmail.com> writes:
Many Many Thanks
I am a new guy to the c language, and learn a lots from your posts.

The array is allocated in the stacks, and overflow with segmentation
fault. I try to remove the array to the global scope (or malloc() to
the heap mem.), and it works fine there.

But I still hold a question: What the exactly amount of stack memory
the process can take. On the 32bit machine, the stack's virtual memory
is like 0x4000000 ~ 0xc000000, that's 800M.isn't it? My computer has
500M(add 1G swap memory)physically, however I could only allocate about
110M in the stack. that's funny? And How could i find the fresh memory
to use, without maintain the total amount?


Please provide context when you post a followup. Until recently,
Google make this unnecessarily difficult, but they seem to have fixed
that.

The C language doesn't specify how much stack space is available (the
standard doesn't even use the word "stack"). The amount of space
available on the system may not be the same as the amount available to
your program. Your system may provide a way to tell you what its
current limits are. <OT>On Unix or Linux, try "limit" or "ulimit".</OT>
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 12 '06 #11
I see, I see...
Thanks for your quit hlepful responses.

--tomy

Walter Roberson wrote:
In article <11**********************@j55g2000cwa.googlegroups .com>,
tomy <to******@gmail.com> wrote:
I am sorry, just make a mistake. I come to check the allocating amount
again.
As last post mentioned, the VM of stack seems to be 0x40000000 ~
0xc0000000 , which should be 2GB. Right?


0x40000000 ~ (0xc0000000 - 1) would be 2 gigabytes in a linear
address space, but that might not a relevant limit. For example on
the 32 bit system I am using right now, all addresses from
0x80000000 to 0xFFFFFFFF are reserved for the operating system and
cannot be allocated to user processes. And there might be a lot of other
factors that affect maximum stack size. You really need to consult
specialists in your platform.

And How could you come to the point 390M?


Please quote context. Please see here for information on how to
do so from Google Groups: http://cfaj.freeshell.org/google/

I cannot tell what the 390M figure is in response to. In the
postings that have made it to my system, no-one has mentioned
390Mb until you. If you are asking how you could get 390 Mb allocated
to the stack for your process then you need to read up on the
details for your platform. The C standard does not even have stacks,
so the C standard discussed in this newsgroup cannot answer any
questions about managing stack allocations on your platform.
--
Prototypes are supertypes of their clones. -- maplesoft


Jun 12 '06 #12

Keith Thompson wrote:
The C language doesn't specify how much stack space is available (the
standard doesn't even use the word "stack"). The amount of space
available on the system may not be the same as the amount available to
your program. Your system may provide a way to tell you what its
current limits are. <OT>On Unix or Linux, try "limit" or "ulimit".</OT>


Actually for stack space (on Unix and similar) one needs to use
getrlimit()

Jun 12 '06 #13
sp****@gmail.com writes:
Keith Thompson wrote:
The C language doesn't specify how much stack space is available (the
standard doesn't even use the word "stack"). The amount of space
available on the system may not be the same as the amount available to
your program. Your system may provide a way to tell you what its
current limits are. <OT>On Unix or Linux, try "limit" or "ulimit".</OT>


Actually for stack space (on Unix and similar) one needs to use
getrlimit()


<OT>
The "limit" or "ulimit" command is a shell builtin. They probably
call getrlimit().
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 12 '06 #14

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

Similar topics

3
by: Jane Austine | last post by:
>Jane Austine wrote: >>>>from Tkinter import * >>>>>r=Tk() >>>>>b=Button(r,text='Quit',command=r.quit) >>>>>b.pack() >>>>>r.mainloop() >> >> >> And when I press the "Quit" button the mainloop...
2
by: nrhayyal | last post by:
hi guys, i am using AIX 5.2 OS and working on c++ and the compiler is gcc-3.3.2 i have a c++ application which was developed on solaris. now i am porting it to AIX. most of my modules work fine....
2
by: Rafal 'Raf256' Maj | last post by:
Hi, how can I manualy dump a backtrace and/or coredump to a file and/or console? Can I do it without aborting the program? --...
3
by: Bhavya Shah | last post by:
Hello there, I am facing a strange problem. I use office converters for converting Microsoft Word documents to html in my application. I use office automation for the entire process. I open the...
8
by: ChrisBowringGG | last post by:
When you use Application.Quit() on an Excel application, there can still be an instance of Excel running, as seen in Task Manager. You can try following the advice on MSDN: ...
6
by: Max | last post by:
I have the following code on a form that launches Microsoft Outlook and creates a new email message for the user: Outlook.Application oApp = new Outlook.Application(); Outlook.MailItem oMail...
0
by: linbl352 | last post by:
When I executed a MYSQL query command, a coredump cames out. But not all the time, it means the program will appear core dump sometimes. Does anybody can tell me why it happens? The following is the...
3
by: =?Utf-8?B?RkpZ?= | last post by:
Hello All, I met a strange behavior with the system threadpool. The worker thread seems to quit in the middle of processing. Here is the code: I could see the step 1 is finished but step 2 is...
3
by: Aptripathi | last post by:
Am getting the above mentioned error MemoryFault(Coredump)while running an executable compiled with ProC what shud i do? and what can be the cause
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: 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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.