Sponsored Content
Top Forums Programming ./match_pattern.out: malloc(): memory corruption: 0x0000000013a11600 *** Post 302553850 by shoaibjameel123 on Friday 9th of September 2011 02:59:17 AM
Old 09-09-2011
Thanks a lot. I spent hours debugging my code and found that the problem was with the chomp() function. SO, I changed the chomp() function and it worked. Here's my code. And yes, I was wrong about memory corruption.

Code:
//Match patterns from several files.

#define _GNU_SOURCE

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<inttypes.h>

char *chomp ( char * );
void read_file ( char * , int32_t * );

int32_t main ( int32_t argc , char ** argv )
{
	char *dat_line = NULL;
	char *txt_line = NULL;
	char *file_name_txt = NULL;
	char *file_name_dat = NULL;
	char *entire_dat_file = NULL;
	char *line_from_txt_file = NULL;
	char *chomped_line = NULL;


	unsigned long int summation = 0;
	unsigned long int value = 1;
	float average = 0.0;
	float scope = 0;

	unsigned long int dat_len = 0;
	unsigned long int read;
	unsigned long int txt_len = 0;
	unsigned long int number_of_characters = 0;
	unsigned long int number_of_words = 0;
	unsigned long int i = 0;

	FILE *open_dat_file = NULL;
	FILE *open_txt_file = NULL;
	FILE *output = NULL;

	//let get the TXT files...
	system ("ls -1 *.txt > text_files.tmp" );
	
	//lets get the DAT files...
	system ( "ls -1 *.dat > dat_files.tmp" );

	//Outer look is for the DAT files and inner loop is for the TXT
	//files...

	FILE *txt_pointer = NULL;
	txt_pointer = fopen ( "text_files.tmp" , "r" );
	if ( txt_pointer == NULL )
	{
		fprintf ( stderr , "The file list for text files does not exist\n" );
	}

	FILE *dat_pointer = NULL;
	dat_pointer = fopen ( "dat_files.tmp" , "r" );
	if ( dat_pointer == NULL )
	{
		fprintf ( stderr , "The file list for the dat files does not exist\n" );
	}


	output = fopen ( "average_values.res" , "a" );
	if ( output == NULL )
	{
		fprintf ( stderr , "File append error\n" );
	}


	while ( ( read = getline ( &dat_line , &dat_len , dat_pointer ) ) != -1 )
	{
		file_name_dat = chomp ( dat_line );
		open_dat_file = fopen ( file_name_dat , "r" );

		( void ) fseek ( open_dat_file , 0L , SEEK_END );
		number_of_characters = ftell ( open_dat_file );

		entire_dat_file = ( char * ) malloc ( ( number_of_characters + 1 ) * sizeof ( char ) );
		if ( entire_dat_file == NULL )
		{
			fprintf ( stderr , "malloc() memory allocation failure in entire_dat_file\n" );
		}

		rewind ( open_dat_file );

		read_file ( file_name_dat , &number_of_words );

		fgets ( entire_dat_file , number_of_characters , open_dat_file );

		while ( ( read = getline ( &txt_line , &txt_len , txt_pointer ) ) != -1 )
		{
			file_name_txt = chomp ( txt_line );
			open_txt_file = fopen ( file_name_txt , "r" );

			//Now read to read the txt files one by one and search for the pattern...
		
			while ( ( read = getline ( &line_from_txt_file , &txt_len , open_txt_file ) ) != 1 && !feof ( open_txt_file ) )
			{
				chomped_line = chomp ( line_from_txt_file );
				if ( strstr ( entire_dat_file , chomped_line ) != NULL )
				{
					summation = summation + value;
				}
				value ++;
			}
			value = 1;
			fclose ( open_txt_file );
			memset ( file_name_txt , 0 , strlen ( file_name_txt ) );
		}

		average = ( float ) summation / ( number_of_words + 1 );
		average = - average;
		scope = powf ( 2.71828183 , average );
		fprintf ( output , "%f\n" , scope );
		number_of_words = 0;
		summation = 0;
		value = 0;
		average = 0;
		scope = 0;
		number_of_characters = 0;

		rewind ( txt_pointer );
		free ( entire_dat_file );
		fclose ( open_dat_file );
		memset ( file_name_dat , 0 , strlen ( file_name_dat ) );
	}

	fclose ( output );

	if ( txt_line )
	{
		free ( txt_line );
	}

	if ( dat_line )
	{
		free ( dat_line );
	}

	fclose ( dat_pointer );
	fclose (txt_pointer );

	return ( EXIT_SUCCESS );
}


char *chomp ( char *s )
{
	unsigned long int len = 0;
	len = strlen ( s );
	if ( s [ len - 1 ] == '\n' )
	{
		s [ len - 1 ] = 0;
	}
	return ( s );
}


void read_file ( char *path , int32_t *number_of_words )
{
	FILE *pointer = NULL;

	char ch;

	pointer = fopen ( path , "r" );
	if ( pointer == NULL)
	{
		perror ( "File read error " );
	}

	(*number_of_words) = 0;

	while ( !feof ( pointer ) )
	{
		ch = fgetc ( pointer );
		if ( ch == ' ' && ch != EOF )
		{
			(*number_of_words) ++;
		}
	}

	fclose ( pointer );
}

 

10 More Discussions You Might Find Interesting

1. Programming

malloc gives the same memory to two different nodes. How to deal with it?

When allocating memory for two different nodes, the resulting memory are the same. Clearly, this will lead to a mistake. This happened in a function. And the process must be in a function. (gdb) p tree->list $43 = (node *) 0x8be4180 (gdb) p tree->list $44 = (node *) 0x8be4180 At the... (2 Replies)
Discussion started by: cdbug
2 Replies

2. UNIX for Dummies Questions & Answers

'memory corruption' error when using Awk

Hello, everyone. I got the following error when I am using awk to analysis some text file: *** glibc detected *** awk: malloc(): memory corruption: 0x080c67f8 *** ======= Backtrace: ========= /lib/tls/i686/cmov/libc.so.6 /lib/tls/i686/cmov/libc.so.6... (5 Replies)
Discussion started by: kooyee
5 Replies

3. Programming

Why memory allocated through malloc should be freed ?

Actually for a process to run it needs text, stack , heap and data segments. All these find a place in the physical memory. Out of these 4 only heap does exist after the termination of the process that created it. I want to know the exact reason why this happens. Also why the other process need to... (20 Replies)
Discussion started by: karthiktceit
20 Replies

4. Programming

Why does this occur? *** glibc detected *** malloc(): memory corruption: 0x10013ff8 ***

there seems not to be error in this segment. In some computers, it can work well. But in others, it will give a failure. why it ocurrs and how to deal with it? in a function: if( *ver == NULL ) { *ver = (vertex *) malloc(sizeof(vertex)); //this line ... (17 Replies)
Discussion started by: cdbug
17 Replies

5. Programming

readdir and dynamic array memory corruption

Hi everyone I am developing an utility. At some part of it I read directory entries to a dynamic array: struct list It stores pointers to items: list.entries, which are structures: struct entry If a number of files in a directory is greater then number of elements an array was initially... (11 Replies)
Discussion started by: torbium
11 Replies

6. Programming

*** glibc detected *** ./a.out: malloc(): memory corruption (fast):

*** glibc detected *** ./a.out: malloc(): memory corruption (fast): Posted A minute ago M trying to make multiway tree and dont know what happend when this part of code get executed: 01void ins(NODE *ptr) 02{ 03 //working 04 if(ptr!=NULL) 05 { 06 SNODE *var=NULL; 07 var=(SNODE... (3 Replies)
Discussion started by: exgenome
3 Replies

7. Programming

*** glibc detected *** : malloc(): memory corruption (fast)

Hi Friends, while executing the below code, am getting *** glibc detected *** ./ok: malloc(): memory corruption (fast) error, please suggest how to solve this issue. #include <stdio.h> #include <string.h> #include <sqlca.h> #include <alloca.h> /* Define constants for VARCHAR... (2 Replies)
Discussion started by: mpjobsrch
2 Replies

8. Programming

*** glibc detected *** ./a.out malloc() memory corruption

I am facing a problem of memory corruption. The loop runs for the first time but does not go through the second time. What could be the problem? for(int z=0;z<2;z++) { fp=fopen("poly.dat","r"); /*do something which reads this file into a 2D array*/ fclose(fp); ... (10 Replies)
Discussion started by: dare
10 Replies

9. Solaris

Solaris 10 Shared Memory Corruption with X11

I am having a problem with shared memory corruption. I have two 86 servers running Solaris 10 (150400-06). One of the servers is accessed by a Sun Ray thin client Version 11.1.3.0.2.6. I login into server one from the thin client. I then ssh -X to server two. When a process that contains a... (2 Replies)
Discussion started by: salerno
2 Replies

10. Programming

Memory corruption in dynamic array of strings

I put together a C function to add strings to a dynamic array of strings (mostly for educational purpose to explain pointers to my kid). It works, but sometimes one or two strings in the array becomes corrupted. Running example on 64 bit Ubuntu, gcc ver. 4.8.4 Hope my code is self-explanatory: ... (2 Replies)
Discussion started by: migurus
2 Replies
All times are GMT -4. The time now is 01:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy