public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: Static memory allocation
@ 2004-09-24 14:21 Huber, George K RDECOM CERDEC STCD SRI
  0 siblings, 0 replies; 7+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-09-24 14:21 UTC (permalink / raw)
  To: 'Ankit Jain', gcc, linux prg

Ankit wrote:

>[ankit@Ankit fft]$ cat try2.c
>#include <stdio.h>
>int main()
>{
>  double a[1450][1450];
>  a[1450][0]=999.999;
>  printf("%lf\n",a[1450][0]);
>  return 0;
>}
>[ankit@Ankit fft]$ gcc try2.c
>[ankit@Ankit fft]$ ./a.out
>Segmentation fault
>[ankit@Ankit fft]$

This is a classic `off-by-one' error.  When you allocate an
array int a[10] you are asking for ten storage locations that 
are numbered 0 -- 9. (not 10).

In your program, the legal range for each index is 0-1449 (not 1450).

George

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Static memory allocation
       [not found]   ` <20040925091401.39110.qmail@web52907.mail.yahoo.com>
@ 2004-09-25 21:56     ` Patrick Percot
  0 siblings, 0 replies; 7+ messages in thread
From: Patrick Percot @ 2004-09-25 21:56 UTC (permalink / raw)
  To: linux-c-programming, gcc-help

On Sat, 25 Sep 2004 10:14:01 +0100 (BST), Ankit Jain <ankitjain1580@yahoo.com> wrote

Hello Ankit,

> sorry boss but this also gives problem
> 
> [ankit@Ankit fft]$ cat try2.c
> #include <stdio.h>
> int main()
> {
> double a[1450][1450];
>  a[149][0]=999.999;
>  printf("%lf\n",a[149][0]);
>  return 0;
> }
> [ankit@Ankit fft]$ gcc try2.c
> [ankit@Ankit fft]$ ./a.out
> Segmentation fault
> 

Sure,  Claudio Bley  gave  you the  solution  (look at  ulimit). I  just
pointed you on another potential problem in your code.

[..Zappé 94 lignes et 2428 caractères..]

À+
PP
-- 
Groupe Morbihannais d'Utilisateurs de Logiciels Libres http://www.tuxbihan.org
GPG fingerprint = 1A4F E154 3D2C A20E E4CA  A543 7951 C5C2 E44A A0B5

Patrick Percot.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Static memory allocation
  2004-09-24 10:57 Ankit Jain
  2004-09-24 12:05 ` Claudio Bley
  2004-09-24 12:51 ` Patrick Percot
@ 2004-09-25 10:03 ` Jon Masters
  2 siblings, 0 replies; 7+ messages in thread
From: Jon Masters @ 2004-09-25 10:03 UTC (permalink / raw)
  To: Ankit Jain; +Cc: gcc, linux prg

Hi Ankit,

It might be worth asking your friend which architecture and kernel
they are using - if it's Linux then they'll have to be using something
that automatically extends the stack quite a bit - or they're just
very lucky (or using Windows 98).

Cheers,

Jon.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Static memory allocation
  2004-09-24 12:51 ` Patrick Percot
@ 2004-09-24 12:59   ` Sriharsha Vedurmudi
       [not found]   ` <20040925091401.39110.qmail@web52907.mail.yahoo.com>
  1 sibling, 0 replies; 7+ messages in thread
From: Sriharsha Vedurmudi @ 2004-09-24 12:59 UTC (permalink / raw)
  To: gcc

[-- Attachment #1: Type: text/plain, Size: 2012 bytes --]

Well, Patrick,

    Actually the original post of this program referred to a[1449][0] 
element only. I guess it was a typo this time from Ankit (and that was 
the reason I did not bother to go through the program again). Anyway, 
Claudio's answer did clear most of the fog.

Cheers,
Harsha.

Patrick Percot wrote:

>On Fri, 24 Sep 2004 11:57:08 +0100 (BST), Ankit Jain <ankitjain1580@yahoo.com> wrote
>
>  
>
>>hi
>>    
>>
>
>Hi,
>
>  
>
>>well i had tried to enquire about this problem
>>
>>i was not able to find the solution. i know it workds
>>when i declare the array globally. i know if i use
>>malloc it will work and i am using the same in my
>>program
>>
>>but this question is again in my mind that what could
>>be the reason that it dosent work here on my system
>>because last time when i asked this question on some
>>sytem it was able to run which they said that they
>>have less emory then that of my system
>>
>>my system config.: redhat linux 9.0 +512 RAM
>>this is what my terminal displays
>>
>>[ankit@Ankit fft]$ cat try2.c
>>#include <stdio.h>
>>int main()
>>{
>>double a[1450][1450];
>> a[1450][0]=999.999;
>>    
>>
>
>You do not just have a stack  problem : The upper bound is not 1450, but
>1449, because the first index is 0. 
>
>So  a[1449][0] has  more chances  to  work correctly,  but the  problems
>generated  by  such a  fault  can be  insidious  and  very difficult  to
>discover, because the error can appear later in your program.
>
>  
>
>> printf("%lf\n",a[1450][0]);
>> return 0;
>>}
>>[ankit@Ankit fft]$ gcc try2.c
>>[ankit@Ankit fft]$ ./a.out
>>Segmentation fault
>>[ankit@Ankit fft]$
>>
>>i just want to know that if there is some problem
>>related to stack how t oget rid of it
>>
>>thanks
>>
>>ANkit
>>
>>
>>
>>
>>________________________________________________________________________
>>Yahoo! Messenger - Communicate instantly..."Ping" 
>>your friends today! Download Messenger Now 
>>http://uk.messenger.yahoo.com/download/index.html
>>
>>    
>>
>
>À+
>PP
>  
>

[-- Attachment #2: sriharsha.v.vcf --]
[-- Type: text/x-vcard, Size: 393 bytes --]

begin:vcard
fn:Sriharsha Vedurmudi
n:Vedurmudi;Sriharsha
org:Redpine Signals Inc.;Software
adr:Sagar Society, Road #2, Banjara Hills;;Gate #395, Plot # 87, 88;Hyderabad;Andhra Pradesh;500034;India
email;internet:sriharsha.v@redpinesignals.com
title:Software Engineer
tel;work:+91-40-23559911
tel;cell:+91-9849133133
x-mozilla-html:TRUE
url:http://www.redpinesignals.com
version:2.1
end:vcard


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Static memory allocation
  2004-09-24 10:57 Ankit Jain
  2004-09-24 12:05 ` Claudio Bley
@ 2004-09-24 12:51 ` Patrick Percot
  2004-09-24 12:59   ` Sriharsha Vedurmudi
       [not found]   ` <20040925091401.39110.qmail@web52907.mail.yahoo.com>
  2004-09-25 10:03 ` Jon Masters
  2 siblings, 2 replies; 7+ messages in thread
From: Patrick Percot @ 2004-09-24 12:51 UTC (permalink / raw)
  To: ankitjain1580; +Cc: gcc-help, linux-c-programming

On Fri, 24 Sep 2004 11:57:08 +0100 (BST), Ankit Jain <ankitjain1580@yahoo.com> wrote

> hi

Hi,

> 
> well i had tried to enquire about this problem
> 
> i was not able to find the solution. i know it workds
> when i declare the array globally. i know if i use
> malloc it will work and i am using the same in my
> program
> 
> but this question is again in my mind that what could
> be the reason that it dosent work here on my system
> because last time when i asked this question on some
> sytem it was able to run which they said that they
> have less emory then that of my system
> 
> my system config.: redhat linux 9.0 +512 RAM
> this is what my terminal displays
> 
> [ankit@Ankit fft]$ cat try2.c
> #include <stdio.h>
> int main()
> {
> double a[1450][1450];
>  a[1450][0]=999.999;

You do not just have a stack  problem : The upper bound is not 1450, but
1449, because the first index is 0. 

So  a[1449][0] has  more chances  to  work correctly,  but the  problems
generated  by  such a  fault  can be  insidious  and  very difficult  to
discover, because the error can appear later in your program.

>  printf("%lf\n",a[1450][0]);
>  return 0;
> }
> [ankit@Ankit fft]$ gcc try2.c
> [ankit@Ankit fft]$ ./a.out
> Segmentation fault
> [ankit@Ankit fft]$
> 
> i just want to know that if there is some problem
> related to stack how t oget rid of it
> 
> thanks
> 
> ANkit
> 
> 
> 
> 
> ________________________________________________________________________
> Yahoo! Messenger - Communicate instantly..."Ping" 
> your friends today! Download Messenger Now 
> http://uk.messenger.yahoo.com/download/index.html
> 

À+
PP
-- 
Groupe Morbihannais d'Utilisateurs de Logiciels Libres http://www.tuxbihan.org
GPG fingerprint = 1A4F E154 3D2C A20E E4CA  A543 7951 C5C2 E44A A0B5

Patrick Percot.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Static memory allocation
  2004-09-24 10:57 Ankit Jain
@ 2004-09-24 12:05 ` Claudio Bley
  2004-09-24 12:51 ` Patrick Percot
  2004-09-25 10:03 ` Jon Masters
  2 siblings, 0 replies; 7+ messages in thread
From: Claudio Bley @ 2004-09-24 12:05 UTC (permalink / raw)
  To: gcc

Hello Ankit,

On Fri, Sep 24, 2004 at 11:57:08AM +0100, Ankit Jain wrote:
> hi
> 
> well i had tried to enquire about this problem
> 
> i was not able to find the solution. i know it workds
> when i declare the array globally. i know if i use
> malloc it will work and i am using the same in my
> program
> 
> but this question is again in my mind that what could
> be the reason that it dosent work here on my system
> because last time when i asked this question on some
> sytem it was able to run which they said that they
> have less emory then that of my system
> 
> my system config.: redhat linux 9.0 +512 RAM
> this is what my terminal displays
> 
> [ankit@Ankit fft]$ cat try2.c
> #include <stdio.h>
> int main()
> {
> double a[1450][1450];
>  a[1450][0]=999.999;
>  printf("%lf\n",a[1450][0]);
>  return 0;
> }
> [ankit@Ankit fft]$ gcc try2.c
> [ankit@Ankit fft]$ ./a.out
> Segmentation fault
> [ankit@Ankit fft]$
> 
> i just want to know that if there is some problem
> related to stack how t oget rid of it

There is no "problem" per se. It is just that stack space
and memory itself in your computer is limited. 

This has nothing (primarily) to do with the amount of main 
memory you got, rather each process has a limited (but adjustable) 
amount of stack space.

You're trying to put an array of 8*1450^2 bytes = 16MB on
the stack.

E.g. on my RH system the stack is by default limited to 8192KB:

$ ulimit -s
8192
$ ./try2
Segmentation fault

So your program of course segfaults.

Just make sure that you got enough stack space:

$ ulimit -s 17000
$ ./try2
999.999000

ulimit is a Bash builtin function; if you're instead using
another shell you might need to run a different command.

BTW, I think that "static memory allocation" is not the right
phrase to describe what you did in your program, the allocation
happens "automatically" when your program enters the main
function and gets automatically destroyed when returning from
main. That's why I learned that these sorts of variables are
called "automatic variables" (surprise, surprise ;-)).

HTH
-- 
Claudio

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Static memory allocation
@ 2004-09-24 10:57 Ankit Jain
  2004-09-24 12:05 ` Claudio Bley
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Ankit Jain @ 2004-09-24 10:57 UTC (permalink / raw)
  To: gcc, linux prg

hi

well i had tried to enquire about this problem

i was not able to find the solution. i know it workds
when i declare the array globally. i know if i use
malloc it will work and i am using the same in my
program

but this question is again in my mind that what could
be the reason that it dosent work here on my system
because last time when i asked this question on some
sytem it was able to run which they said that they
have less emory then that of my system

my system config.: redhat linux 9.0 +512 RAM
this is what my terminal displays

[ankit@Ankit fft]$ cat try2.c
#include <stdio.h>
int main()
{
double a[1450][1450];
 a[1450][0]=999.999;
 printf("%lf\n",a[1450][0]);
 return 0;
}
[ankit@Ankit fft]$ gcc try2.c
[ankit@Ankit fft]$ ./a.out
Segmentation fault
[ankit@Ankit fft]$

i just want to know that if there is some problem
related to stack how t oget rid of it

thanks

ANkit




________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping" 
your friends today! Download Messenger Now 
http://uk.messenger.yahoo.com/download/index.html

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-09-25 21:56 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-24 14:21 Static memory allocation Huber, George K RDECOM CERDEC STCD SRI
  -- strict thread matches above, loose matches on Subject: below --
2004-09-24 10:57 Ankit Jain
2004-09-24 12:05 ` Claudio Bley
2004-09-24 12:51 ` Patrick Percot
2004-09-24 12:59   ` Sriharsha Vedurmudi
     [not found]   ` <20040925091401.39110.qmail@web52907.mail.yahoo.com>
2004-09-25 21:56     ` Patrick Percot
2004-09-25 10:03 ` Jon Masters

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).