473,804 Members | 3,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memory aligment problem

Hi group,

I'm making an application in C++ with has different behavior if I use
a buffer in the main function or at global scope :

int buff[256];

int main()
{
callFunctionTha tUseBuffer(buff );
return 1;
}

the function called has different behavior (and a bad behavior) if i'm
doing

int main()
{
int buff[256];
callFunctionTha tUseBuffer(buff );
return 1;
}

How to explain that ?

Thanks for your help.

Best regards,

S.

May 4 '07 #1
6 1583
ti*********@gma il.com wrote:
Hi group,

I'm making an application in C++ with has different behavior if I use
a buffer in the main function or at global scope :

int buff[256];

int main()
{
callFunctionTha tUseBuffer(buff );
return 1;
}

the function called has different behavior (and a bad behavior) if i'm
doing

int main()
{
int buff[256];
callFunctionTha tUseBuffer(buff );
return 1;
}

How to explain that ?
1. Odds are it's not a memory alignment problem.
2. See FAQ 5.8
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
3. Odds are that callFunctionTha tUseBuffer() overruns its input
argument, but since you didn't give us enough information (see point 2
above), we can't tell.

May 4 '07 #2
On 4 mai, 22:41, red floyd <no.s...@here.d udewrote:
timor.su...@gma il.com wrote:
Hi group,
I'm making an application in C++ with has different behavior if I use
a buffer in the main function or at global scope :
int buff[256];
int main()
{
callFunctionTha tUseBuffer(buff );
return 1;
}
the function called has different behavior (and a bad behavior) if i'm
doing
int main()
{
int buff[256];
callFunctionTha tUseBuffer(buff );
return 1;
}
How to explain that ?

1. Odds are it's not a memory alignment problem.
2. See FAQ 5.8http://www.parashift.c om/c++-faq-lite/how-to-post.html#faq-5.8
3. Odds are that callFunctionTha tUseBuffer() overruns its input
argument, but since you didn't give us enough information (see point 2
above), we can't tell.
sorry, but i can't post the code of the function, it's in a lib
I'm using g++ to compile
the lib creator told me that it comes from a memory aligment

but, everything is OK with a buffer at global scope, i have tested it.

May 4 '07 #3
ti*********@gma il.com wrote:
Hi group,

I'm making an application in C++ with has different behavior if I use
a buffer in the main function or at global scope :

int buff[256];

int main()
{
callFunctionTha tUseBuffer(buff );
return 1;
}

the function called has different behavior (and a bad behavior) if i'm
doing

int main()
{
int buff[256];
callFunctionTha tUseBuffer(buff );
return 1;
}

How to explain that ?
callFunctionTha tUseBuffer is corrupting memory beyond the end of the
buffer buff. When buff is a global, memory following buff is probably
unused or not not used before the program is terminated and so the
effect of the corruption is not visible. In the second case, where the
memory for buff is usually allocated on the program stack along with
return addresses and stack pointers etc, corruption of those variables
gets noticed before you return from main() (or as you return from main).
Stack overrun is employed by virus writers to gain control of your
program. (security note) If you every make an array on the stack, make
absolutely sure that nothing is ever going to be able to write past the
bounds of the array.

You would see the problem with this code:

int main()
{
int * buff = new int[256];
callFunctionTha tUseBuffer(buff );
delete [] buff;
return 1;
}

.... the day you released your code to your most important customer.

However, if you ran the code above under valgrind, you would be told
exactly where your error was.
May 4 '07 #4
ti*********@gma il.com wrote:
On 4 mai, 22:41, red floyd <no.s...@here.d udewrote:
>timor.su...@gm ail.com wrote:
>>Hi group,
I'm making an application in C++ with has different behavior if I use
a buffer in the main function or at global scope :
int buff[256];
int main()
{
callFunctionTha tUseBuffer(buff );
return 1;
}
the function called has different behavior (and a bad behavior) if i'm
doing
int main()
{
int buff[256];
callFunctionTha tUseBuffer(buff );
return 1;
}
How to explain that ?
1. Odds are it's not a memory alignment problem.
2. See FAQ 5.8http://www.parashift.c om/c++-faq-lite/how-to-post.html#faq-5.8
3. Odds are that callFunctionTha tUseBuffer() overruns its input
argument, but since you didn't give us enough information (see point 2
above), we can't tell.

sorry, but i can't post the code of the function, it's in a lib
I'm using g++ to compile
the lib creator told me that it comes from a memory aligment
Nonsense, if the function parameter is a pointer to int, it can't be an
alignment problem. If it isn't, post it.
but, everything is OK with a buffer at global scope, i have tested it.
Within a very very narrow definition of OK. Just wait until something
changes in an update to the compiler or its runtime.

--
Ian Collins.
May 4 '07 #5
ti*********@gma il.com wrote in
news:11******** *************@e 65g2000hsc.goog legroups.com:
On 4 mai, 22:41, red floyd <no.s...@here.d udewrote:
>timor.su...@gm ail.com wrote:
Hi group,
I'm making an application in C++ with has different behavior if I
use a buffer in the main function or at global scope :
int buff[256];
int main()
{
callFunctionTha tUseBuffer(buff );
return 1;
}
the function called has different behavior (and a bad behavior) if
i'm doing
int main()
{
int buff[256];
callFunctionTha tUseBuffer(buff );
return 1;
}
How to explain that ?

1. Odds are it's not a memory alignment problem.
2. See FAQ
5.8http://www.parashift.c om/c++-faq-lite/how-to-post.html#faq-5.8
3. Odds are that callFunctionTha tUseBuffer() overruns its input
argument, but since you didn't give us enough information (see point
2 above), we can't tell.

sorry, but i can't post the code of the function, it's in a lib
I'm using g++ to compile
the lib creator told me that it comes from a memory aligment
There is not enough information provided to come to that conclusion. And
is also probably wrong. Both arrays will be correctly aligned to hold
the 256 ints. As others have mentioned, it's far more likely that
something inside callFunctionTha tUseBuffer has messed up. Probably ran
off of the end of the array.
>
but, everything is OK with a buffer at global scope, i have tested it.
You haven't tested it enough. You are likely experiencing Undefined
Behaviour. Its effects may be many and varied, running from formatting
your hard drive, to crashing your program, even to possibly seeming to
work perfectly. It's still bad.
May 4 '07 #6
<ti*********@gm ail.comwrote in message
news:11******** **************@ y80g2000hsf.goo glegroups.com.. .
Hi group,

I'm making an application in C++ with has different behavior if I use
a buffer in the main function or at global scope :

int buff[256];

int main()
{
callFunctionTha tUseBuffer(buff );
return 1;
}

the function called has different behavior (and a bad behavior) if i'm
doing

int main()
{
int buff[256];
callFunctionTha tUseBuffer(buff );
return 1;
}

How to explain that ?

Thanks for your help.
Most likely as others have said, buffer overflow. Fairly easy way to test
this. If hte callFunctionTha tUseBuffer expects 256 ints, try to purposely
make it way more and see if the problem goes away. I.E.

int main()
{
int buff[512];
callFunctionTha tUseBuffer(buff );
return 1;
}

If you are also passing the size, then pass it the 256. If the program
doesn't do this bad behavior, then it is almost certainly buffer overflow.

Almost every time a program behaves differntly when you move around where
variables are defined it's buffer overflow.
May 5 '07 #7

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

Similar topics

0
2049
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 the thread is over a month old, I decided to start a new one with my response. Please see my comments inline.
4
4496
by: Amadeus | last post by:
Hello Everybody! I have a problem with MySQL servers running RedHat 9 (smp kernel 2.4.20) on Intel and MySQL server 4.0.14 (problem also appears on binary distr 4.0.15 and on 4.0.15 I bilt myself from source). I have few big tables with BLOBS and regular table 4.2 and 2.7 Gb respectively, plust several smaller tables. Every time I run query against this tables MySQL uses all available memory on server (I have 3Gb RAM on server) and it...
32
3866
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my code I only define constructor for my class, and do not define destructor, will it cause memory leak?
2
3383
by: Paul_Huang | last post by:
OK, I tried it with a piece of sample code to test the memory padding and alignment and get some weird results. I would appreciate if anybody can help to give a explain. Below is my sample code: *************************************************** #include <iostream> using namespace std; #include <Windows.h>
1
2161
by: Jeff | last post by:
Dear all, I was reading the ISO/IEC 9899 , and see the following thing: 6.7.1 10 . An implementation may allocate any addressable storage unit large enough to hold a bit- field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps...
7
6938
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports the status of the machines. Upon we follow the .NET object lifetime recommendations the application is constantly consuming more memory! The problem is on the ManagementObjectSearch, upon we Dispose the object it seems that is not releasing the...
9
9230
by: Bruno Barberi Gnecco | last post by:
I'm using PHP to run a CLI application. It's a script run by cron that parses some HTML files (with DOM XML), and I ended up using PHP to integrate with the rest of the code that already runs the website. The problem is: it's eating more memory than a black hole. It eats the current limit of 256MB set in php.ini, in an application that would hardly consume 4MB if written in C. I don't care if this application takes much longer to run...
9
4220
by: jeungster | last post by:
Hello, I'm trying to track down a memory issue with a C++ application that I'm working on: In a nutshell, the resident memory usage of my program continues to grow as the program runs. It starts off at a nice 4% of memory, then slowly grows up to 50% and beyond. This translates to around 2 gigs of physical memory, and that's really way more memory than this program should be taking up.
17
8490
by: frederic.pica | last post by:
Greets, I've some troubles getting my memory freed by python, how can I force it to release the memory ? I've tried del and gc.collect() with no success. Here is a code sample, parsing an XML file under linux python 2.4 (same problem with windows 2.5, tried with the first example) : #Python interpreter memory usage : 1.1 Mb private, 1.4 Mb shared #Using http://www.pixelbeat.org/scripts/ps_mem.py to get memory information
0
9587
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.