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

Function calls takes up memory

1
Hi,

I am writing a c++ program for a microprocessor, and I encountered a weird problem.

I have a simple function, which does nothing else than swapping around some bytes in a 32bit integer, which is given as argument for the function.
The function looks something like this:

int swapbytes(int bts)
{
int swap_ret;

bitpnt = (char*)&bts;
swappnt = (char*)&swap_ret;

swappnt[3] = bitpnt[0];
... etc...
}

The function works fine, but the problem is, that everytime I call this function, I can see that it takes up more memory, which must mean that the compiler makes a copy of the function each time it is called. Even though it is a very small function, it takes around 250bytes extra for each time I call it.

An example:
swap1 = swapbytes(0x01020304);
swap2 = swapbytes(0x10203040);
swap3 = swapbytes(0x05060708);

This takes up 2x250 bytes more memory, than if I just called the function 1 time.

I am using a GNU compiler.
Is it a compiler problem, or do anyone know what I am dealing with here? ;-)

Thanks in advance!
Regards,
KimPiX
Apr 7 '08 #1
1 1521
weaknessforcats
9,208 Expert Mod 8TB
swap1 = swapbytes(0x01020304);
swap2 = swapbytes(0x10203040);
swap3 = swapbytes(0x05060708);
This code creates three stack frames for swapbytes. One for each call. Stack frames are invalid after the function returns, but there is no guarantee that the stack frame is deleted before the next call is made. Some compilers tend to clean up only every so often so you may have stack frames hanging around that just haven't been deleted yet.

Check your calling convention. The default convention for C is that the caller cleans up the stack frames. That means those stack frames may not be deleted until the calling function itself is complete. Microsoft has a __stdcall convention that toggles their compiler to make the called function responsibile to delete its own stack frame upon completion. Perhaps gnu has an equivalent.

These are the Microsoft calling conventions:
Expand|Select|Wrap|Line Numbers
  1. int __cdecl swap(int);         /* ANS C. Caller cleans up */
  2. int __stdcall swap(int);       /* Microsoft. Callee cleans up */
  3.  
The Microsoft __cdecl does not need to be part of the funcition prototype but the __stdcall does. In their world, __stdcall has been defined as WINAPI to make it stand out as Microsoft specific.

In any case, I guarantee that in C copies of the function are not being made.
Apr 7 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
8
by: Tweaxor | last post by:
Hey, I was trying to figure out was it possible in C to pass the values in an array from one function to another function. Is the possible in C? ex. y is the array that holds seven values If...
4
by: Hermann Maier | last post by:
hi, i need to find out the memory usage of a specific function that i use in my program. this function does some recursive calculations and i want my program to display the amount of memory the...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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:
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.