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

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()
{
callFunctionThatUseBuffer(buff);
return 1;
}

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

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

How to explain that ?

Thanks for your help.

Best regards,

S.

May 4 '07 #1
6 1573
ti*********@gmail.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()
{
callFunctionThatUseBuffer(buff);
return 1;
}

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

int main()
{
int buff[256];
callFunctionThatUseBuffer(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 callFunctionThatUseBuffer() 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.dudewrote:
timor.su...@gmail.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()
{
callFunctionThatUseBuffer(buff);
return 1;
}
the function called has different behavior (and a bad behavior) if i'm
doing
int main()
{
int buff[256];
callFunctionThatUseBuffer(buff);
return 1;
}
How to explain that ?

1. Odds are it's not a memory alignment problem.
2. See FAQ 5.8http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
3. Odds are that callFunctionThatUseBuffer() 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*********@gmail.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()
{
callFunctionThatUseBuffer(buff);
return 1;
}

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

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

How to explain that ?
callFunctionThatUseBuffer 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];
callFunctionThatUseBuffer(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*********@gmail.com wrote:
On 4 mai, 22:41, red floyd <no.s...@here.dudewrote:
>timor.su...@gmail.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()
{
callFunctionThatUseBuffer(buff);
return 1;
}
the function called has different behavior (and a bad behavior) if i'm
doing
int main()
{
int buff[256];
callFunctionThatUseBuffer(buff);
return 1;
}
How to explain that ?
1. Odds are it's not a memory alignment problem.
2. See FAQ 5.8http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
3. Odds are that callFunctionThatUseBuffer() 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*********@gmail.com wrote in
news:11*********************@e65g2000hsc.googlegro ups.com:
On 4 mai, 22:41, red floyd <no.s...@here.dudewrote:
>timor.su...@gmail.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()
{
callFunctionThatUseBuffer(buff);
return 1;
}
the function called has different behavior (and a bad behavior) if
i'm doing
int main()
{
int buff[256];
callFunctionThatUseBuffer(buff);
return 1;
}
How to explain that ?

1. Odds are it's not a memory alignment problem.
2. See FAQ
5.8http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
3. Odds are that callFunctionThatUseBuffer() 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 callFunctionThatUseBuffer 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*********@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.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()
{
callFunctionThatUseBuffer(buff);
return 1;
}

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

int main()
{
int buff[256];
callFunctionThatUseBuffer(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 callFunctionThatUseBuffer expects 256 ints, try to purposely
make it way more and see if the problem goes away. I.E.

int main()
{
int buff[512];
callFunctionThatUseBuffer(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
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: 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...
32
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...
2
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:...
1
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...
7
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...
9
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...
9
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...
17
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...
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: 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
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
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...
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.