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

memory use

In the below example, I beleive that the char array buffer[40] should
only occupy 40 bytes of the stack within the inner braces.

However the compiler seems to use 40 bytes of the stack througout the
entire function that this example is cut from. Is this normal?

if(midnightflag==1)
{
lcd_clear();
lcd_locate(1,1);
lcd_display("Updating date");
{ unsigned char buffer[40];
daydateyear(buffer);
senduart2(buffer);
senduart2("\r\n");
};
update_date();
midnightflag=0;
};
Nov 14 '05 #1
12 1689
Jim
On 25 Oct 2004 23:34:54 -0700, mi*****@clift.com.au (Michael) wrote:
In the below example, I beleive that the char array buffer[40] should
only occupy 40 bytes of the stack within the inner braces.

However the compiler seems to use 40 bytes of the stack througout the
entire function that this example is cut from. Is this normal?

if(midnightflag==1)
{
lcd_clear();
lcd_locate(1,1);
lcd_display("Updating date");
{ unsigned char buffer[40];
daydateyear(buffer);
senduart2(buffer);
senduart2("\r\n");
};
update_date();
midnightflag=0;
};


The compiler is not even obliged to use a stack!

Jim
Nov 14 '05 #2
>In the below example, I beleive that the char array buffer[40] should
only occupy 40 bytes of the stack within the inner braces.
(1) what stack?
(2) which stack?

Standard C is not required to use a stack.
However the compiler seems to use 40 bytes of the stack througout the
entire function that this example is cut from. Is this normal?
How did you determine this from the code below, or, for that
matter, using any portable code?
And what difference does it make?
if(midnightflag==1)
{
lcd_clear();
lcd_locate(1,1);
lcd_display("Updating date");
{ unsigned char buffer[40];
daydateyear(buffer);
senduart2(buffer);
senduart2("\r\n");
};
update_date();
midnightflag=0;
};


Gordon L. Burditt
Nov 14 '05 #3

"Michael" <mi*****@clift.com.au> wrote
In the below example, I beleive that the char array buffer[40] should
only occupy 40 bytes of the stack within the inner braces.

However the compiler seems to use 40 bytes of the stack througout the
entire function that this example is cut from. Is this normal?

Yes. A compiler can implement automatic variables in any way it likes. Some
small embedded systems don't even use a stack but allocate to fixed areas of
memory.
However typically a compiler will adjust the stack frame on function entry
to hold all variables declared in that function, and reset it on function
exit. This means that your block scope array is treated as if it had
function scope. Your own system may be different, but probably not if it is
a major compiler.
Nov 14 '05 #4
"Malcolm" <ma*****@55bank.freeserve.co.uk> writes:
"Michael" <mi*****@clift.com.au> wrote
In the below example, I beleive that the char array buffer[40] should
only occupy 40 bytes of the stack within the inner braces.

However the compiler seems to use 40 bytes of the stack througout the
entire function that this example is cut from. Is this normal?

Yes. A compiler can implement automatic variables in any way it likes. Some
small embedded systems don't even use a stack but allocate to fixed areas of
memory.


Note that an implementation that doesn't support recursion (and
therefore distinct storage for local variables for each function call)
is not conforming, though it might still be useful.

On the other hand, using a fixed area of memory for a function that's
known not to be called recursively is perfectly legal.

--
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.
Nov 14 '05 #5
mi*****@clift.com.au (Michael) wrote...
In the below example, I beleive that the char array buffer[40] should
only occupy 40 bytes of the stack within the inner braces.
As far as the language specification is concerned automatic data will
only be allocated at the point of declaration, and will expire at the
end of the enclosing block scope.
However the compiler seems to use 40 bytes of the stack througout the
entire function that this example is cut from. Is this normal?

if(midnightflag==1)
{
lcd_clear();
lcd_locate(1,1);
lcd_display("Updating date");
{ unsigned char buffer[40];
daydateyear(buffer);
senduart2(buffer);
senduart2("\r\n");
};
update_date();
midnightflag=0;
};


An implementation can do whatever it likes, so long as it preserves
the semantics of strictly conforming code.

If silently moving the declaration of buffer to the top of the
function block would not change the semantics of your code, then your
compiler can (and likely will) do so in the interests of runtime
efficiency.

But comp.lang.c is not the forum to discus what particular
implementations do under the hood, unless it's to gain an
understanding of C language itself. [Understanding C in terms of
assembler or debugging/profiling tools is not something that clc
encourages, nor should it.]

--
Peter
Nov 14 '05 #6
mi*****@clift.com.au (Michael) wrote:
In the below example, I beleive that the char array buffer[40] should
only occupy 40 bytes of the stack within the inner braces.

However the compiler seems to use 40 bytes of the stack througout the
entire function that this example is cut from. Is this normal?

if(midnightflag==1)
{
{ unsigned char buffer[40];
};
};

The braces dictate that the name "buffer" is available only
within those braces. However the actual 40 bytes can exist at
any time. The compiler could even allocate them at compile-time
if it determined that the function was never called re-entrantly
(I know of a compiler that does this).
Nov 14 '05 #7
go***********@burditt.org (Gordon Burditt) wrote in message news:<cl********@library2.airnews.net>...
In the below example, I believe that the char array buffer[40] should
only occupy 40 bytes of the stack within the inner braces.
(1) what stack?

The stack in an MSP430 microcontroller
(2) which stack? The MSP only has one.

Standard C is not required to use a stack.
However the compiler seems to use 40 bytes of the stack throughout the
entire function that this example is cut from. Is this normal?
How did you determine this from the code below, or, for that
matter, using any portable code?


By viewing the assembly code generated by the compiler.
And what difference does it make?
The imagecraft compiler I am using allocates memory for static and
global variables in the lower part of memory, it then uses the stack
for other variables from the top of memory downwards.

I wish to increase the size of a static array as much as possible
without the stack overwriting it. I placed breakpoints in parts of my
program where i had a lot of variables in scope, to see how low the
stack pointer was.

Sorry my question wasn't very clear, what I wanted to know was:

In the code below (whether using a stack or not), should the char
array buffer[] occupy memory for the entire function which this code
is cut from (even though it is only accessible within the braces), OR,
should it only occupy memory within the braces. I would have thought
the latter, but maybe this is not a requirement of standard C.
if(midnightflag==1)
{
lcd_clear();
lcd_locate(1,1);
lcd_display("Updating date");
{ unsigned char buffer[40];
daydateyear(buffer);
senduart2(buffer);
senduart2("\r\n");
};
update_date();
midnightflag=0;
};


Gordon L. Burditt

Nov 14 '05 #8
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message news:<cl**********@news6.svr.pol.co.uk>...
"Michael" <mi*****@clift.com.au> wrote
In the below example, I beleive that the char array buffer[40] should
only occupy 40 bytes of the stack within the inner braces.

However the compiler seems to use 40 bytes of the stack througout the
entire function that this example is cut from. Is this normal?

Yes. A compiler can implement automatic variables in any way it likes. Some
small embedded systems don't even use a stack but allocate to fixed areas of
memory.
However typically a compiler will adjust the stack frame on function entry
to hold all variables declared in that function, and reset it on function
exit. This means that your block scope array is treated as if it had
function scope. Your own system may be different, but probably not if it is
a major compiler.


Thanks Malcolm.
Nov 14 '05 #9
Michael wrote:
[snip]

Sorry my question wasn't very clear, what I wanted to know was:

In the code below (whether using a stack or not), should the char
array buffer[] occupy memory for the entire function which this code
is cut from (even though it is only accessible within the braces), OR,
should it only occupy memory within the braces. I would have thought
the latter, but maybe this is not a requirement of standard C. Definitely the latter (see section 6.2.4 of the standard). The former
behaviour is often achieved with the alloca() function.

Robert
if(midnightflag==1)
{
lcd_clear();
lcd_locate(1,1);
lcd_display("Updating date");
{ unsigned char buffer[40];
daydateyear(buffer);
senduart2(buffer);
senduart2("\r\n");
};
update_date();
midnightflag=0;
};


Gordon L. Burditt

Nov 14 '05 #10
Robert Harris <ro*****************@blueyonder.co.uk> writes:
Michael wrote:
[snip]
Sorry my question wasn't very clear, what I wanted to know was:
In the code below (whether using a stack or not), should the char
array buffer[] occupy memory for the entire function which this code
is cut from (even though it is only accessible within the braces), OR,
should it only occupy memory within the braces. I would have thought
the latter, but maybe this is not a requirement of standard C.

Definitely the latter (see section 6.2.4 of the standard). The former
behaviour is often achieved with the alloca() function.


The alloca() function is non-standard.

--
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.
Nov 14 '05 #11
On Tue, 26 Oct 2004 23:42:39 UTC, ai***@acay.com.au (Peter Nilsson)
wrote:
mi*****@clift.com.au (Michael) wrote...
In the below example, I beleive that the char array buffer[40] should
only occupy 40 bytes of the stack within the inner braces.


As far as the language specification is concerned automatic data will
only be allocated at the point of declaration, and will expire at the
end of the enclosing block scope.


No. You gets confused with visibility and reserved space. It is
completely legal theat an compuler will allocate the maximum used
space in asingle block for auto variables.

void f(void) {

if (x1)
{ int a[100]; }
if (x2)
{ int a; }
if (x3)
{
struct st {
int a, b, c, d;
double dd[999];
} s[10];
}

}

The compiler may or may not eleminate any unused variable - so giving
f() as above it may even eleminate any call of f() when the definition
of the function is visible, it may eleminate the whole body of the
fuction when it has nothing more as above, reducing it to a simple
return.

Assuming that each inner block has code that works with the variables
defined there it may allocate one single memory block big enough to
hold the biggest of all memory areas only while overlapping the
shorter ones, it may simply add the sizes and reserve the whole
storage or it can do whatever it likes to have the memory needed in
each block available. The only that the compiler will guarantee when
it defines itself as standard compatible is to have each variable only
visible in the block it is defined.

It may change its behavior depending on flags given to it at
compiletime. It may change its behavior between subversions or full
versions of itself.

There must not even a stack available. Any possible memory handling to
reserve a properitary chunk of memory that is used only inside the
function and its inner blocks and making the variables visible only
inside the inner blocks fullyfies the requirements of the standard
perfectly.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation

Nov 14 '05 #12
mi*****@clift.com.au (Michael) wrote in message news:<ec**************************@posting.google. com>...
Thanks again to all who responded.
Nov 14 '05 #13

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

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
4
by: Frank Esser | last post by:
I am using SQL 8 Personal edition with sp2 applied. I set the max server memory to 32MB and leave the min server memory at 0. When my application starts hitting the database hard the memory usage...
4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
9
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but...
22
by: xixi | last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer pool size to accommadate better performance, say size 200000, if i have multiple connection to the same database from...
14
by: Alessandro Monopoli | last post by:
Hi all, I'm searching a PORTABLE way to get the available and total physical memory. Something like "getTotalMemory" and it returns the memory installed on my PC in bytes, and...
1
by: Nick Craig-Wood | last post by:
I've been dumping a database in a python code format (for use with Python on S60 mobile phone actually) and I've noticed that it uses absolutely tons of memory as compared to how much the data...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
1
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after...
5
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store...
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: 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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.