473,327 Members | 2,065 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,327 software developers and data experts.

Array memory allocation

Hi

if i declare globally its get store id BSS / Data
segment .................with size a.out we can verify
where does array is allocated memory when its declared inside a
function ......... i only see a change a little bit in Text ( Code )
Segment Size only

Thanks
Pallav Singh
#include<stdio.h>

char p[40];
static double d;

int i = 10;
static long l = 20;

int main()
{

int arr[1000] = {0};
int array[1000] = {0};

int i =3 , j ,*ip;
ip=(int *)malloc(sizeof(i));

p[5] = i;
d = 2.0 * 20;
return 0;

}
Dec 17 '07 #1
9 1873
On Mon, 17 Dec 2007 00:35:18 -0800 (PST), Pallav singh wrote:
where does array is allocated memory when its declared
inside a function
The stack.

--
Joel Yliluoma - http://iki.fi/bisqwit/
Dec 17 '07 #2
On Dec 17, 2:33 pm, Joel Yliluoma <bisq...@iki.fiwrote:
On Mon, 17 Dec 2007 00:35:18 -0800 (PST), Pallav singh wrote:
where does array is allocated memory when its declared
inside a function

The stack.

--
Joel Yliluoma -http://iki.fi/bisqwit/
Only when the actual function is called...
Dec 17 '07 #3
On Mon, 17 Dec 2007 02:13:09 -0800 (PST), Rahul <sa*****@yahoo.co.in>
wrote in comp.lang.c++:
On Dec 17, 2:33 pm, Joel Yliluoma <bisq...@iki.fiwrote:
On Mon, 17 Dec 2007 00:35:18 -0800 (PST), Pallav singh wrote:
where does array is allocated memory when its declared
inside a function
The stack.
What stack? Can you cite a reference from the C++ language standard
that requires or guarantees the existence of such a stack?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Dec 18 '07 #4
On Mon, 17 Dec 2007 00:35:18 -0800 (PST), Pallav singh
<si**********@gmail.comwrote in comp.lang.c++:
Hi

if i declare globally its get store id BSS / Data
segment .................with size a.out we can verify
The '.' key on your keyboard seems to be broken. In English, there
are places where a single '.' character is used, after an abbreviation
or at the end of a sentence. And there are places where an ellipsis
is used, which is exactly three '.' characters, like this: ...

There is no place in the English language where two or four or
seventeen continuous '.' characters has any meaning at all.

As for your assertion, maybe your particular C++ compiler has things
called "BSS" or "Data" segments. The C++ language has no such things,
leaving the details up to the compiler.
where does array is allocated memory when its declared inside a
function ......... i only see a change a little bit in Text ( Code )
Segment Size only
Automatic objects allocated inside a function go wherever the compiler
implementer decided to put them. The C++ language does not specify.

If you want to know how your particular compiler does these things,
ask in a group that discusses your specific compiler.
Thanks
Pallav Singh
If your compiler accepts the code below, either it is completely
broken, or perhaps it is a C compiler. The code is ill-formed for
C++.
#include<stdio.h>
<stdio.his deprecated in C++ in favor of <cstdio>.
char p[40];
static double d;

int i = 10;
static long l = 20;

int main()
{

int arr[1000] = {0};
int array[1000] = {0};

int i =3 , j ,*ip;
ip=(int *)malloc(sizeof(i));
There is no declaration in scope for malloc(), because you have not
included <cstdlibor even <stdlib.h>. A conforming C++ compiler will
not accept a call to a function without a proper declaration in scope.

The cast will allow older C compilers, prior to the 1999 version of
the C standard, to accept the code without a diagnostic. For versions
of the C standard from 1999 later, calling a function without a
declaration is a constraint violation.

In all versions of C and C++, this code is just plain wrong, and
produces undefined behavior.
p[5] = i;
d = 2.0 * 20;
return 0;

}
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Dec 18 '07 #5
On Dec 18, 5:30 am, Jack Klein <jackkl...@spamcop.netwrote:
On Mon, 17 Dec 2007 02:13:09 -0800 (PST), Rahul <sam_...@yahoo.co.in>
wrote in comp.lang.c++:
On Dec 17, 2:33 pm, Joel Yliluoma <bisq...@iki.fiwrote:
On Mon, 17 Dec 2007 00:35:18 -0800 (PST), Pallav singh wrote:
where does array is allocated memory when its declared
inside a function
The stack.
What stack? Can you cite a reference from the C++ language
standard that requires or guarantees the existence of such a
stack?
§3.7.2 seems clear enough to me; §5.2.2/9 makes it even more
explicit. You can't implement C++ without a stack. (It's true
that in the special case of main, an implementation is not
required to allocate local variables on the stack.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 18 '07 #6
Victor Bazarov wrote:
Ian Collins wrote:
gcc /tmp/x.c /tmp/y.c -ansi -Wall -pedantic
/tmp/x.c: In function `main':
/tmp/x.c:4: warning: implicit declaration of function `f'
/tmp/y.c: In function `f':
/tmp/y.c:3: warning: implicit declaration of function `printf'

C does not require a function prototype to exist.
Depends on what you mean. Use of a variadic function like printf()
without a prototype in scope is undefined behavior. It doesn't require
a diagnostic.

Brian

Dec 20 '07 #7
Ian Collins wrote:
Victor Bazarov wrote:
C does not require a function prototype to exist. What you have
here are warnings, Bjarnes code is well-formed C, AFAICS. If you
have a doubt, do ask in 'c.l.c'.
I don't, it isn't well formed. Calling a variadic without a
declaration in scope invokes our old friend undefined behaviour.
No, more strictive, it requires a prototype. C still allows function
declarations like:

int printf();
That's why the compiler is required to issue a diagnostic in
conforming mode.
I don't think that's true. Now, for a C99 compiler, use of any function
without a declaration requires a diagnostic. Implicit declarations went
away.

Brian
Dec 20 '07 #8
James Kanze wrote:
On Dec 19, 11:24 pm, Ian Collins <ian-n...@hotmail.comwrote:
Victor Bazarov wrote:
C does not require a function prototype to exist.
I thought someone said they were required in C99.
No, declarations are required, but not prototypes. However, "old style"
is obsolescent.
However, the
discussion was about warnings---I simply assumed that any C++
compiler worth its salt would warn about implicit function
declarations, since it is such an obvious source of error.
I assume you mean C compiler. For C++ that's a required diagnostic (and
for C99 as well).
Apparently, as Bjarne pointed out, I was being overly
optimistic.
In fact, it did warn when in conforming mode, right?

Brian
Dec 20 '07 #9
On Dec 20, 6:37 pm, "Default User" <defaultuse...@yahoo.comwrote:
Ian Collins wrote:
Victor Bazarov wrote:
C does not require a function prototype to exist. What you have
here are warnings, Bjarnes code is well-formed C, AFAICS. If you
have a doubt, do ask in 'c.l.c'.
I don't, it isn't well formed. Calling a variadic without a
declaration in scope invokes our old friend undefined behaviour.
No, more strictive, it requires a prototype. C still allows function
declarations like:
int printf();
Not in this case. Even C90 requires a function prototype to be
in scope when a variadic function is called. Something like the
above would result in undefined behavior if you used printf in
the code.

For non-variadic functions, of course, C90 didn't even require a
declaration if the function, at least if it returned int. And
most C compilers seem to use the C90 rules by default.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Dec 21 '07 #10

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

Similar topics

2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
12
by: Tan Thuan Seah | last post by:
Hi all, I was told this in one of the university course I was doing. In C we may expect good performance for: double a, c, d; for (i=0; i<N; i++) for(j=0; j<N; j++) a = a + c *d;
9
by: pvinodhkumar | last post by:
The number of elemets of the array, the array bound must be constant expression?Why is this restriction? Vinodh
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
21
by: smartbeginner | last post by:
main() { int i; int *a; a=calloc(4,sizeof(*a)); /* The above code I know will not compile .But why cant I allocate space for all 4 integers i need to store */ for(i=0;i<2;i++)...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
18
by: welch.ryan | last post by:
Hi all, Having a problem with addressing large amounts of memory. I have a simple piece of code here that is meant to allocate a large piece of memory on a ppc64 machine. The code is: /*...
3
by: aeo3 | last post by:
Hi All, Now, I am trying to build a project, I need to expand an array of pointer to classes. Moreover, this array includes some elements I want to delete them. So, I create another array, copy the...
4
by: somenath | last post by:
I have a question regarding the memory allocation of array. For example int array; Now according to my understanding 10 subsequent memory location will be allocated of size sizeof(int) *10...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.