You are not logged in. Please login at www.codechef.com to post your questions!

×

declaring an array of 1000000 size

I was seeing a solution of a problem

in the solution an array is declared as

int vec[1000000]

when i copied the code and tried to run it on my system it gives me error but when i submitted the same code on codechef i got correct answer.

while searching for the cause i found out that i cant declare array of that size because stack memory is limited. i should declare it in heap using dynamic memory allocation but my question is that how i got correct answer on codechef ?

asked 27 Aug '13, 01:22

nishant_25's gravatar image

2★nishant_25
101121526
accept rate: 0%


I'm not saying this will totally work and solve your problem but, if you declare your array globally then, i think even on old gcc compiler this will work.

link

answered 27 Aug '13, 10:37

sobhagya's gravatar image

3★sobhagya
2.7k132747
accept rate: 12%

yes its working. but y?

(27 Aug '13, 16:52) nishant_252★
2

Global variables are allocated in the heap memory, which is the memory where dynamically allocated memory resides. Non-global variables are allocated in the stack which is typically much, much shorter... This a good trick to have present as it might help you even with newer compiler versions... :)

(27 Aug '13, 17:12) kuruma2★

same as @kuruma

(27 Aug '13, 21:53) sobhagya3★

thanks it helped me in clearing some basic concepts..:)

(28 Aug '13, 00:31) nishant_252★

Hello,

It might be possible that your compiler flags are somewhat "ill-defined", in the sense that you don't allow so much stack memory to be used which basically means your stack-size has a different size limit than the one used by Codechef judge, but, I compiled that code in my system and it gave no errors or warnings...

I am using this to compile it:

gcc -Wall -ansi -pedantic -o teste teste.c -lm

This is my gcc version status:

gcc (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

You should re-check your compiler settings, as if the code works fine on my system and on Codechef, then the problem must be on your side...

Hope this helps!!

Best regards,

Bruno

link

answered 27 Aug '13, 01:47

kuruma's gravatar image

2★kuruma
17.5k72143209
accept rate: 8%

when i checked my gcc version it showed

gcc 3.4.2

copyright (c) 2004

i think its too old and that's why its stack memory will be less than its current version. thanks alot

link

answered 27 Aug '13, 03:03

nishant_25's gravatar image

2★nishant_25
101121526
accept rate: 0%

I'm glad I could help you :)

(27 Aug '13, 04:05) kuruma2★

I was having same problem while compiling some problem of codechef, but when i submitted it, got ac...

link

answered 27 Aug '13, 21:35

abcdexter24's gravatar image

2★abcdexter24
309211
accept rate: 3%

@abcdexter24 read my answer first

(27 Aug '13, 21:52) sobhagya3★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×779
×103

question asked: 27 Aug '13, 01:22

question was seen: 12,296 times

last updated: 28 Aug '13, 00:31