内存分配问题! [英] Memory allocation problem!

查看:94
本文介绍了内存分配问题!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有以下代码片段,旨在帮助我添加

调试跟踪到我的程序(我想纯粹使用C ++代码,但

我知道如何做这样的事情的方法是使用宏,所以请

不要对我大喊大叫):

#include< iostream>

#include< cstdarg>

#include< iomanip>


#define MainDisplay(s,...)Show(__FILE __,\

__PRETTY_FUNCTION __,\

__LINE __,\

s,\\ \\ / $
## __VA_ARGS__);

使用命名空间std;


void显示(const char *文件,const char * fcn ,int line,const char *

display,...)

{

//构建显示字符串

va_list va;

va_start(va,display);

char display2 [256];

char linestr [10];

vsprintf(display2,display,va);

va_end(va);

sprintf(linestr,"%d", line);


string s =(file + string(" [")+ linestr + string("]:"));


//输出到控制台

cout<< s;

int ssize = s.size();

if(ssize< = 45)cout<< setw(50-ssize)<< "" << display2;

else cout<< endl<< setw(50)<< "" << display2;

}

现在我希望它显示一个很长的字符串消息(这个字符串有

548个字符):


INT主(INT的argc,字符* argv的[])

{


MainDisplay(QUOT; 0000000101010101010101010101010101010 10101010101010101010101010101010101010101010101010 10101010101111111111111111111111111111111111111100 01000000101100010001000001100111101000110001010011 10011010011100110100110110111111111111111111111111 11111111111111011010100110011100101100000000101010 10101010101010101010101010101010101010101010101010 10101010101010101010101010101010101111111111111111 11111111111111111111110001000000101100010001000001 10011110100011000101001110011010011100110100110110 11111111111111111111111111111111111111011010100110 01110010110");


返回EXIT_SUCCESS;

}


当我运行此代码我得到一个分段错误,并且GDB在

之后喷出:


程序收到信号SIGSEGV,分段错误。

0x00002aaaab06f6a0 in strlen()来自/lib64/tls/libc.so.6

(gdb)backtrace

#0 0x00002aaaab06f6a0 in strlen()from /lib64/tls/libc.so.6

#1 0x0000000000401775 in std :: char_traits< char> :: length

( __s = 0x3130313031303130<地址0x3130313031303130超出范围>)

char_traits.h:143

std :: allocator< char> > (__lhs = 0x3130313031303130<地址

0x3130313031303130超出界限>,__ rhs = @ 0x7fffffffe9b0)

basic_string.tcc:692

#3显示中的0x0000000000401232(文件= 0x3130313031303130<地址

0x3130313031303130超出范围>,fcn = 0x3130313031303130<地址

0x3130313031303130超出范围>,行= 825241904,

display = 0x3130313031303130<地址0x3130313031303130超出范围>)

/home/prashant/Development/test2/src/test2.cpp:20

#4 0x3131303131313131在?? ()

#5 0x3131303031303130在? ()

#6 0x3130303131313030在? ()

#7 0x00002a0030313130 in ?? ()

#8 0x0000000000000000 in ?? ()

#9 0x0000000000503028 in ?? ()

#10 0x0000000000400e1b in _init()

#11 0x00007fffffffed78 in ?? ()

#12 0x0000000100401801 in ?? ()

#13 0

前一帧内部到此帧(损坏的堆栈?)

x00002aaaaabc0c60 ?? ()来自/lib64/ld-linux-x86-64.so.2

#14 0x00002aaaaabc0c60 in ?? ()from /lib64/ld-linux-x86-64.so.2

#15 0x00000000004017e0 in __libc_csu_fini()

#16 0x00002aaaab00e1d8 in ?? ()来自/lib64/tls/libc.so.6

#17 0x00007fffffffed78 in ?? ()

#18 0x00000001ffffed88 in ?? ()

#19 0x0000000000401490在Show()中

/home/prashant/Development/test2/src/test2.cpp:27

(gdb)第0帧

#0 0x00002aaaab06f6a0在strlen()中来自/lib64/tls/libc.so.6

如果我缩短字符串,我发现我得到了没有分段错误

少于313个字符。我的假设是这是一个

''新''或malloc内存限制。 ulimit -a返回以下内容:


核心文件大小(块,-c)0

数据段大小(kbytes,-d)无限

文件大小(块,-f)无限

最大锁定内存(kbytes,-l)32

最大内存大小(千字节,-m)无限制

打开文件(-n)1024

管道大小(512字节,-p)8

堆栈大小(kbytes,-s)无限制

cpu time(秒,-t)无限

最大用户进程(-u)8191

虚拟内存(kbytes,-v)无限制

我在Suse Linux 9.3上运行(但我已经能够在9.0上重现这个

问题)而且我正在使用gcc 3.4.1 。


有什么想法吗?


Prashant

Hi,

I have the following piece of code that is designed to help me add
debug traces to my program (I wanted to use purely C++ code, but the
only way I know how to do something like this is with macros, so please
don''t yell at me):
#include <iostream>
#include <cstdarg>
#include <iomanip>

#define MainDisplay(s, ...) Show( __FILE__, \
__PRETTY_FUNCTION__,\
__LINE__, \
s, \
## __VA_ARGS__);
using namespace std;

void Show(const char* file, const char* fcn, int line, const char*
display, ...)
{
//build display string
va_list va;
va_start(va, display);
char display2[256];
char linestr[10];
vsprintf(display2, display, va);
va_end(va);
sprintf(linestr, "%d", line);

string s = (file + string(" [") + linestr + string("]:"));

//output to console
cout << s;
int ssize = s.size();
if (ssize <= 45) cout << setw(50-ssize) << "" << display2;
else cout << endl << setw(50) << "" << display2;
}
Now I want it to display a very long string message (this string has
548 characters in it):

int main(int argc, char *argv[])
{

MainDisplay("0000000101010101010101010101010101010 10101010101010101010101010101010101010101010101010 10101010101111111111111111111111111111111111111100 01000000101100010001000001100111101000110001010011 10011010011100110100110110111111111111111111111111 11111111111111011010100110011100101100000000101010 10101010101010101010101010101010101010101010101010 10101010101010101010101010101010101111111111111111 11111111111111111111110001000000101100010001000001 10011110100011000101001110011010011100110100110110 11111111111111111111111111111111111111011010100110 01110010110");

return EXIT_SUCCESS;
}

When I run this code I get a segmentation fault, and GDB spews the
following:

Program received signal SIGSEGV, Segmentation fault.
0x00002aaaab06f6a0 in strlen () from /lib64/tls/libc.so.6
(gdb) backtrace
#0 0x00002aaaab06f6a0 in strlen () from /lib64/tls/libc.so.6
#1 0x0000000000401775 in std::char_traits<char>::length
(__s=0x3130313031303130 <Address 0x3130313031303130 out of bounds>) at
char_traits.h:143
#2 0x00000000004015ea in std::operator+<char, std::char_traits<char>,
std::allocator<char> > (__lhs=0x3130313031303130 <Address
0x3130313031303130 out of bounds>, __rhs=@0x7fffffffe9b0) at
basic_string.tcc:692
#3 0x0000000000401232 in Show (file=0x3130313031303130 <Address
0x3130313031303130 out of bounds>, fcn=0x3130313031303130 <Address
0x3130313031303130 out of bounds>, line=825241904,
display=0x3130313031303130 <Address 0x3130313031303130 out of bounds>)
at /home/prashant/Development/test2/src/test2.cpp:20
#4 0x3131303131313131 in ?? ()
#5 0x3131303031303130 in ?? ()
#6 0x3130303131313030 in ?? ()
#7 0x00002a0030313130 in ?? ()
#8 0x0000000000000000 in ?? ()
#9 0x0000000000503028 in ?? ()
#10 0x0000000000400e1b in _init ()
#11 0x00007fffffffed78 in ?? ()
#12 0x0000000100401801 in ?? ()
#13 0
Previous frame inner to this frame (corrupt stack?)
x00002aaaaabc0c60 in ?? () from /lib64/ld-linux-x86-64.so.2
#14 0x00002aaaaabc0c60 in ?? () from /lib64/ld-linux-x86-64.so.2
#15 0x00000000004017e0 in __libc_csu_fini ()
#16 0x00002aaaab00e1d8 in ?? () from /lib64/tls/libc.so.6
#17 0x00007fffffffed78 in ?? ()
#18 0x00000001ffffed88 in ?? ()
#19 0x0000000000401490 in Show () at
/home/prashant/Development/test2/src/test2.cpp:27
(gdb) frame 0
#0 0x00002aaaab06f6a0 in strlen () from /lib64/tls/libc.so.6
If I shorten the string, I find that I get no segmentation fault for
anything less than 313 characters. My assumption is that this is a
''new'' or malloc memory limit. ulimit -a returns the following:

core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) unlimited
cpu time (seconds, -t) unlimited
max user processes (-u) 8191
virtual memory (kbytes, -v) unlimited
I''m running on Suse Linux 9.3 (but I''ve been able to reproduce this
problem on 9.0) and I''m using gcc 3.4.1.

Any Ideas?

Prashant

推荐答案



jo *********** @ gmail.com 写道:
#define MainDisplay(s,...)Show(__FILE __,\
__ PRETTY_FUNCTION __,\
__LINE __,\
s,\
## __VA_ARGS__ );
使用命名空间std;

void显示(const char *文件,const char * fcn,int line,const char *
display,...)
{
//构建显示字符串
va_list va;
va_start(va,display);
char display2 [256];
char linestr [10];
vsprintf(display2,display,va);



写入256字节缓冲区。


va_end(va);
sprintf(linestr," ;%d",line);

string s =(file + string(" [")+ linestr + string("]:"));

//输出到控制台
cout<< s;
int ssize = s.size();
if(ssize< = 45)cout<< setw(50-ssize)<< "" << display2;
else cout<< endl<< setw(50)<< "" << display2;
}

现在我希望它显示一个很长的字符串消息(这个字符串中有548个字符):

int main (INT的argc,字符* argv的[])
{

MainDisplay(QUOT; 0000000101010101010101010101010101010 10101010101010101010101010101010101010101010101010 10101010101111111111111111111111111111111111111100 01000000101100010001000001100111101000110001010011 10011010011100110100110110111111111111111111111111 11111111111111011010100110011100101100000000101010 10101010101010101010101010101010101010101010101010 10101010101010101010101010101010101111111111111111 11111111111111111111110001000000101100010001000001 10011110100011000101001110011010011100110100110110 11111111111111111111111111111111111111011010100110 01110010110");



将548个字节发送到256字节缓冲区


返回EXIT_SUCCESS;
}

当我运行此代码时得到一个分段错误,GDB吐出
跟随ing:


[snip]

任何想法?
#define MainDisplay(s, ...) Show( __FILE__, \
__PRETTY_FUNCTION__,\
__LINE__, \
s, \
## __VA_ARGS__);
using namespace std;

void Show(const char* file, const char* fcn, int line, const char*
display, ...)
{
//build display string
va_list va;
va_start(va, display);
char display2[256];
char linestr[10];
vsprintf(display2, display, va);

writing to a 256-byte buffer.

va_end(va);
sprintf(linestr, "%d", line);

string s = (file + string(" [") + linestr + string("]:"));

//output to console
cout << s;
int ssize = s.size();
if (ssize <= 45) cout << setw(50-ssize) << "" << display2;
else cout << endl << setw(50) << "" << display2;
}
Now I want it to display a very long string message (this string has
548 characters in it):

int main(int argc, char *argv[])
{

MainDisplay("0000000101010101010101010101010101010 10101010101010101010101010101010101010101010101010 10101010101111111111111111111111111111111111111100 01000000101100010001000001100111101000110001010011 10011010011100110100110110111111111111111111111111 11111111111111011010100110011100101100000000101010 10101010101010101010101010101010101010101010101010 10101010101010101010101010101010101111111111111111 11111111111111111111110001000000101100010001000001 10011110100011000101001110011010011100110100110110 11111111111111111111111111111111111111011010100110 01110010110");


sending 548 bytes into a 256 byte buffer

return EXIT_SUCCESS;
}

When I run this code I get a segmentation fault, and GDB spews the
following:
[snip]

Any Ideas?



你的缓冲区溢出。


-shez-


you are overflowing your buffer.

-shez-


jois.de.vi ... @ gmail.com写道:
jois.de.vi...@gmail.com wrote:


我有以下代码片段,旨在帮助我将调试跟踪添加到我的程序中(我想使用纯粹的C ++代码,但是
只有我知道如何做这样的事情才是宏,所以请不要对我大吼大叫):


对于未来,你应该发布信息comp.lang.c ++ for

C ++程序,或者如果由于某种原因你需要C语言输入,

将你的问题减少到可编译的C程序并发布到那里。 />

#include< iostream>
#include< cstdarg>
#include< iomanip>

#define MainDisplay(s, ...)显示(__FILE __,\
__ PRETTY_FUNCTION __,\
__LINE __,\
s,\
## __VA_ARGS__);
使用命名空间std;

void显示(const char *文件,const char * fcn,int line,const char *
display,...)
//构建显示字符串
va_list va;
va_start(va,display);
char display2 [256];
char linestr [10 ];
vsprintf(display2,display,va);
Hi,

I have the following piece of code that is designed to help me add
debug traces to my program (I wanted to use purely C++ code, but the
only way I know how to do something like this is with macros, so please
don''t yell at me):
For the future, you should either post in comp.lang.c++ for
C++ programs, or if for some reason you need C language input,
reduce your problem to a compilable C program and post there.


#include <iostream>
#include <cstdarg>
#include <iomanip>

#define MainDisplay(s, ...) Show( __FILE__, \
__PRETTY_FUNCTION__,\
__LINE__, \
s, \
## __VA_ARGS__);
using namespace std;

void Show(const char* file, const char* fcn, int line, const char*
display, ...)
{
//build display string
va_list va;
va_start(va, display);
char display2[256];
char linestr[10];
vsprintf(display2, display, va);




为什么你认为如果你投入
$ b会有效$ b多了256个字节进入display2?它会超出内存。

您需要使display2大于任何可能的输入

(你怎么能这样做?)或使用vsnprintf和truncate,可能是一个

更好的选择。


-David



Why do you think that this will work if you are putting in
more that 256 bytes into display2? It will overrun memory.
You either need to make display2 bigger than any possible input
(how can you do that?) or use vsnprintf and truncate, probably a
better bet.

-David


jo *********** @ gmail.com 写道:


我有以下代码片段,旨在帮助我将调试跟踪添加到我的程序中(我想使用纯粹的C ++代码,但
只有我知道如何使用做这样的事情就是宏,所以请不要对我大吼大叫):


也许你没注意到这是comp.lang.c ,而不是comp.lang.c ++。

无论如何,看看你的vspintf:vsprintf(display2,display,va);


display2是char [256] 。显示是548(正如你在主要部分所说,我做了

不计算它们)。看到有什么不对吗?你正在超越

缓冲区!

尝试vsnprintf。


#include< iostream>
#include< cstdarg>
#include< iomanip>

#define MainDisplay(s,...)显示(__FILE __,\
__ PRETTY_FUNCTION __,\\ \\ _> __LINE __,\
s,\
## __ VA_ARGS__);
使用命名空间std;

void显示(const char *文件, const char * fcn,int line,const char *
display,...)
// build build string
va_list va;
va_start(va,显示);
char display2 [256];
char linestr [10];
vsprintf(display2,display,va);
va_end(va);
sprintf (linestr,"%d",line);

string s =(file + string(" [")+ linestr + string("]:"));
cout<< s;
int ssize = s.size();
if(ssize< = 45)cout<< setw(50-ssize)<< "" << display2;
else cout<< endl<< setw(50)<< "" << display2;
}

现在我希望它显示一个很长的字符串消息(这个字符串中有548个字符):

int main (INT的argc,字符* argv的[])
{

MainDisplay(QUOT; 0000000101010101010101010101010101010 10101010101010101010101010101010101010101010101010 10101010101111111111111111111111111111111111111100 01000000101100010001000001100111101000110001010011 10011010011100110100110110111111111111111111111111 11111111111111011010100110011100101100000000101010 10101010101010101010101010101010101010101010101010 10101010101010101010101010101010101111111111111111 11111111111111111111110001000000101100010001000001 10011110100011000101001110011010011100110100110110 11111111111111111111111111111111111111011010100110 01110010110");

返回EXIT_SUCCESS;
}

当我运行此代码时,我得到一个分段错误,GDB发出以下信息:

程序收到信号SIGSEGV,分段错误。
0x00002aaaab06f6a0来自/lib64/tls/libc.so.6
(gdb)backtrace
来自/lib64/tls/libc.so.6的strlen()中的#0x00000000aaaab06f6a0 />#1 0x0000000000401775 in std :: char_traits< char> :: length
(__s = 0x3130313031303130<地址0x3130313031303130超出范围>)
char_traits.h:143
#2 0x00000000004015ea在std :: operator +< char,std :: char_traits< char>,
std :: allocator< char> > (__lhs = 0x3130313031303130<地址
0x3130313031303130超出范围>,__ rhs = @ 0x7fffffffe9b0)
basic_string.tcc:692
#3 0x0000000000401232 in Show(file = 0x3130313031303130< Address
0x3130313031303130超出范围>,fcn = 0x3130313031303130<地址
0x3130313031303130超出界限>,line = 825241904,
display = 0x3130313031303130<地址0x3130313031303130超出界限>)
at / home / prashant /开发/ test2 / src / test2.cpp:20
#4 0x3131303131313131在? ()
#5 0x3131303031303130在? ()
#6 0x3130303131313030在? ()
#7 0x00002a0030313130在? ()
#8 0x0000000000000000 in ?? ()
#9 0x0000000000503028 in ?? ()
#10 0x0000000000400e1b in _init()
#11 0x00007fffffffed78 in ?? ()
#12 0x0000000100401801 in ?? ()
#13 0
上一帧内部到这个帧(腐败堆栈?)
x00002aaaaabc0c60在? ()来自/lib64/ld-linux-x86-64.so.2
#14 0x00002aaaaabc0c60 in ?? ()来自/lib64/ld-linux-x86-64.so.2
#15 0x00000000004017e0 in __libc_csu_fini()
#16 0x00002aaaab00e1d8 in ?? ()来自/lib64/tls/libc.so.6
#17 0x00007fffffffed78 in ?? ()
#18 0x00000001ffffed88 in ?? ()
#19 0x0000000000401490在Show()中
/home/prashant/Development/test2/src/test2.cpp:27
(gdb)第0帧
#0 0x00002aaaab06f6a0在strlen()中来自/lib64/tls/libc.so.6

如果我缩短字符串,我发现没有任何分段错误
任何少于313个字符。我的假设是这是一个
''new''或malloc内存限制。 ulimit -a返回以下内容:

核心文件大小(块,-c)0
数据段大小(kbytes,-d)无限
文件大小(块,-f )无限
最大锁定内存(kbytes,-l)32
最大内存大小(千字节,-m)无限制打开文件(-n)1024
管道大小(512字节) ,-p)8
堆栈大小(kbytes,-s)无限
cpu时间(秒,-t)无限
最大用户进程(-u)8191
虚拟内存( kbytes,-v)无限

我在Suse Linux 9.3上运行(但我已经能够在9.0上重现这个问题)而且我正在使用gcc 3.4 .1。

任何想法?

Prashant
Hi,

I have the following piece of code that is designed to help me add
debug traces to my program (I wanted to use purely C++ code, but the
only way I know how to do something like this is with macros, so please
don''t yell at me):
Perhaps you didn''t notice this is comp.lang.c, not comp.lang.c++.
Anyways, look at your vspintf: vsprintf(display2, display, va);

display2 is char[256]. display is 548 (as you said in the main, i did
not count them). See anything wrong with that? You''re overrunning the
buffer!
try vsnprintf.



#include <iostream>
#include <cstdarg>
#include <iomanip>

#define MainDisplay(s, ...) Show( __FILE__, \
__PRETTY_FUNCTION__,\
__LINE__, \
s, \
## __VA_ARGS__);
using namespace std;

void Show(const char* file, const char* fcn, int line, const char*
display, ...)
{
//build display string
va_list va;
va_start(va, display);
char display2[256];
char linestr[10];
vsprintf(display2, display, va);
va_end(va);
sprintf(linestr, "%d", line);

string s = (file + string(" [") + linestr + string("]:"));

//output to console
cout << s;
int ssize = s.size();
if (ssize <= 45) cout << setw(50-ssize) << "" << display2;
else cout << endl << setw(50) << "" << display2;
}
Now I want it to display a very long string message (this string has
548 characters in it):

int main(int argc, char *argv[])
{

MainDisplay("0000000101010101010101010101010101010 10101010101010101010101010101010101010101010101010 10101010101111111111111111111111111111111111111100 01000000101100010001000001100111101000110001010011 10011010011100110100110110111111111111111111111111 11111111111111011010100110011100101100000000101010 10101010101010101010101010101010101010101010101010 10101010101010101010101010101010101111111111111111 11111111111111111111110001000000101100010001000001 10011110100011000101001110011010011100110100110110 11111111111111111111111111111111111111011010100110 01110010110");

return EXIT_SUCCESS;
}

When I run this code I get a segmentation fault, and GDB spews the
following:

Program received signal SIGSEGV, Segmentation fault.
0x00002aaaab06f6a0 in strlen () from /lib64/tls/libc.so.6
(gdb) backtrace
#0 0x00002aaaab06f6a0 in strlen () from /lib64/tls/libc.so.6
#1 0x0000000000401775 in std::char_traits<char>::length
(__s=0x3130313031303130 <Address 0x3130313031303130 out of bounds>) at
char_traits.h:143
#2 0x00000000004015ea in std::operator+<char, std::char_traits<char>,
std::allocator<char> > (__lhs=0x3130313031303130 <Address
0x3130313031303130 out of bounds>, __rhs=@0x7fffffffe9b0) at
basic_string.tcc:692
#3 0x0000000000401232 in Show (file=0x3130313031303130 <Address
0x3130313031303130 out of bounds>, fcn=0x3130313031303130 <Address
0x3130313031303130 out of bounds>, line=825241904,
display=0x3130313031303130 <Address 0x3130313031303130 out of bounds>)
at /home/prashant/Development/test2/src/test2.cpp:20
#4 0x3131303131313131 in ?? ()
#5 0x3131303031303130 in ?? ()
#6 0x3130303131313030 in ?? ()
#7 0x00002a0030313130 in ?? ()
#8 0x0000000000000000 in ?? ()
#9 0x0000000000503028 in ?? ()
#10 0x0000000000400e1b in _init ()
#11 0x00007fffffffed78 in ?? ()
#12 0x0000000100401801 in ?? ()
#13 0
Previous frame inner to this frame (corrupt stack?)
x00002aaaaabc0c60 in ?? () from /lib64/ld-linux-x86-64.so.2
#14 0x00002aaaaabc0c60 in ?? () from /lib64/ld-linux-x86-64.so.2
#15 0x00000000004017e0 in __libc_csu_fini ()
#16 0x00002aaaab00e1d8 in ?? () from /lib64/tls/libc.so.6
#17 0x00007fffffffed78 in ?? ()
#18 0x00000001ffffed88 in ?? ()
#19 0x0000000000401490 in Show () at
/home/prashant/Development/test2/src/test2.cpp:27
(gdb) frame 0
#0 0x00002aaaab06f6a0 in strlen () from /lib64/tls/libc.so.6
If I shorten the string, I find that I get no segmentation fault for
anything less than 313 characters. My assumption is that this is a
''new'' or malloc memory limit. ulimit -a returns the following:

core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) unlimited
cpu time (seconds, -t) unlimited
max user processes (-u) 8191
virtual memory (kbytes, -v) unlimited
I''m running on Suse Linux 9.3 (but I''ve been able to reproduce this
problem on 9.0) and I''m using gcc 3.4.1.

Any Ideas?

Prashant






这篇关于内存分配问题!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆