memory aligment problem

T

timor.super

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.
 
R

red floyd

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-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.
 
T

timor.super

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.
 
G

Gianni Mariani

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.
 
I

Ian Collins

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.
 
A

Andre Kostur

(e-mail address removed) wrote in

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.
 
J

Jim Langston

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,297
Messages
2,571,529
Members
48,242
Latest member
BarbMott55

Latest Threads

Top