调试/发布时的C malloc奇怪行为
我试图在简单程序中使用C malloc/free函数调试/发布时的C malloc奇怪行为,c,debugging,gcc,malloc,C,Debugging,Gcc,Malloc,我试图在简单程序中使用C malloc/free函数 void* alloc_array( const int size ) { void* p; p = malloc( size ); if( p ) { //memset(p, 0, size); return( p ); } else return NULL; } void free_array( void* p ) { if( p
void* alloc_array( const int size )
{
void* p;
p = malloc( size );
if( p )
{
//memset(p, 0, size);
return( p );
}
else
return NULL;
}
void free_array( void* p )
{
if( p )
{
free( p );
p = NULL;
}
}
void** alloc_array_ptr( const int nPointers )
{
void** p;
unsigned int size = AMOUNT_TO_ALLOC(nPointers, void*);
p = malloc( size );
if( p )
{
return( p );
}
else
return( NULL );
}
void free_array_ptr( void** p, const int nPointers )
{
unsigned int i;
if( p )
{
for( i = 0; i < nPointers; i++ )
if( p[i] )
{
free( p[i] );
p[i] = NULL;
}
free(p);
p = NULL;
}
}
int main(int argc, char* agv[]) {
// builds the string with the location of the ini file
int inifile_size = 0;
char* inifile;
// gets the buffer size nedeed for the buffer
inifile_size = GetCurrentDirectory(0, NULL);
// allocates memory for the buffer
inifile = (char*) alloc_array( AMOUNT_TO_ALLOC(inifile_size, char) );
// fills the buffer with the current directory
if( GetCurrentDirectory( inifile_size, inifile ) == 0 )
{
printf("GetCurrentDirectory failed! (ErrorCode: %u)\n", GetLastError());
return( !MY_ERROR_OK );
}
strcat(inifile, "\\");
strcat(inifile, "test.ini");
printf("INI File: %s\n", inifile);
char** sessions;
int nSessions = 0;
int i;
nSessions = getNumberOfSubkeys(str);
printf("\nOK (%d sessions found)\n", nSessions);
// allocating memory for the sessions array of char*
sessions = (char**) alloc_array_ptr( nSessions );
if( sessions == NULL )
{
printf("ERROR_IN_MEM_ALLOC (SESSIONS)\n");
return( MY_ERROR_MEM_ALLOC );
}
else
printf("sessions: 0x%p\n", sessions);
// allocating memory for each one of the char* in sessions
for( i = 0; i < nSessions; i++ )
{
sessions[i] = (char*) alloc_array( AMOUNT_TO_ALLOC(MAX_KEY_LENGTH, char) );
printf("sessions[%d]: 0x%p\n", i, sessions[i]);
if( sessions[i] == NULL )
{
printf("ERROR_IN_MEM_ALLOC (SESSIONS[%d])\n", i);
return( MY_ERROR_MEM_ALLOC );
}
}
printf("\nSessions found:\n");
if( readSubkeys( str, sessions ) == MY_ERROR_OK )
{
for( i = 0; i < nSessions; i++ )
{
printf("\t%s: ", sessions[i]);
if( convert2ini(inifile, sessions[i]) == MY_ERROR_OK )
printf("OK\n");
else
{
printf("ERROR\n");
return( !MY_ERROR_OK );
}
}
}
else
{
printf("ERROR\n");
return( MY_ERROR_OPEN_REGISTRY );
}
// free the inifile array
free_array(inifile);
// free the array of char*
free_array_ptr( (void**)sessions, nSessions );
// returns to OS
return( MY_ERROR_OK );
}
void*alloc_数组(常量整数大小)
{
void*p;
p=malloc(尺寸);
如果(p)
{
//memset(p,0,size);
回报率(p);
}
其他的
返回NULL;
}
无空数组(void*p)
{
如果(p)
{
自由基(p);
p=零;
}
}
void**alloc\u array\u ptr(常量int npoints)
{
无效**p;
无符号整数大小=金额到分配(nPointers,void*);
p=malloc(尺寸);
如果(p)
{
回报率(p);
}
其他的
返回(空);
}
无空洞的数组(空洞**p,常量)
{
无符号整数i;
如果(p)
{
对于(i=0;i
这是一个将windows注册表中的一些程序设置转换为ini文件(Windows7,64位,4Gb RAM)的小工具
readSubkeys使用从str中的注册表项获取的会话名称填充char*的预分配会话数组
getNumberOfSubkeys返回给定注册表中的子项数
代码的其余部分与此无关。问题是,当我为for循环中的每个char*分配内存时,malloc失败。我使用CodeLite编写这个程序,奇怪的是,当我一步一步地调试代码时,malloc并没有失败
这是一个正在运行的is程序的图像。
谁能给我一些建议吗
奇怪的是,当我一步一步地调试代码时,malloc并没有失败
此语句可能意味着代码中的其他地方存在内存分配错误,导致此块(例如malloc()调用或断言)失败。我们没有足够的信息在没有更多代码的情况下完全调试它。很抱歉,我不得不将此作为答案发布。我仍然没有发表评论的特权。 在第一条malloc语句中,您正在将malloc的返回类型转换为指针对指针。 因此,在for循环中,当您说sessions[0]时,它将包含第一个位置0x00701760的地址(在第一个malloc之后)。 将for循环增加到会话[1]。这在技术上是不可能的。因为会话中没有下一个值。实际上,您需要增加会话[0]中包含的值。 它可能类似于*sessions++。我想说的是,您应该将0x00701760增加到0x00701762,并对其执行malloc。因此0x00701762现在包含新分配内存的地址。接下来是0x00701764,然后是0x00701766。 增加会话[]将导致错误或至少访问无效内存。 希望这个建议有帮助 谢谢
再见谢谢大家的提示 问题是我在程序的早期没有正确分配内存。奇怪的是,这部分软件工作正常,但随后的内存分配失败。我要解决的是,我计算了malloc的正确内存大小,包括几个strcat调用来添加分隔符和文件名。然后,软件工作得很好。我重写了有问题的部分,看起来是这样的
// builds the string with the current directory
// gets the size nedeed for the buffer
current_path_size = GetCurrentDirectory(0, NULL);
// allocates memory for the buffer
current_path = (char*) alloc_array( AMOUNT_TO_ALLOC(current_path_size, char) );
// gets the current directory
if( GetCurrentDirectory( current_path_size, current_path ) == 0 )
{
printf("GetCurrentDirectory failed! (ErrorCode: %u)\n", GetLastError());
return( !MY_ERROR_OK );
}
// builds the string with the location of the ini file
// calculates the total amount of memory to alloc
total_size = current_path_size + strlen(CHAR_SEPARATOR) + strlen(PUTTY_FILENAME) + 1;
// allocates memory for the full path + filename of the ini file
ini_file = (char*) alloc_array(AMOUNT_TO_ALLOC(total_size, char));
// fills the buffer with the path + separator + filename + leading '\0'
strcat(ini_file, current_path);
strcat(ini_file, CHAR_SEPARATOR);
strcat(ini_file, PUTTY_FILENAME);
ini_file[total_size - 1] = '\0';
没有响应的一件事是为什么调试器没有使代码崩溃。我还需要对此做进一步的研究。在for循环中,malloc失败了,你的意思是它返回
NULL
?在我的计算机上工作。可能是getNumberOfSubkeys(str)有问题代码>-