LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-09-2008, 03:01 AM   #1
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
C malloc results


Hi, i'm using the xyssl library to generate some hash's of a file, then there run through this function to convert to hex, before all of them being simultaneously being printed:

Code:
char *funcs_ustr2hex(unsigned char *sum)
{
	int i;
	int len = strlen((char*)sum);
	char *sret="";
	sret = (char *)malloc((len*2)+1);
	//for( i = 0; i < len-1; i++ )
	for( i = 0; i < 16; i++ )
		sprintf( sret, "%s%02x", sret, sum[i] );
	return sret;
}
my output looks like this:

Quote:
md5: ��X5dd39cab1c53c2c77cd352983f9641e1
md2: `�Xb5fcc4d4c05a2c6d25bda588c26663b0
md4: P�Xd4c5dab3ccfcc1d76d58382450f26ed2
I'm assuming it's this function because I don't yet do any conversion along the route (hence still unsigned), and these results look like memory problems.
 
Old 02-09-2008, 07:16 AM   #2
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
sret is not initialized, i.e. it points to random values after the malloc (which will confuse sprintf).

To put a

Code:
sret[0] = '\0';
before the printing loop will most likely solve this problem.
 
Old 02-09-2008, 07:40 AM   #3
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Original Poster
Rep: Reputation: 33
fan dabby double dozy! rock on tommy... (hope you know the reference! Basically Cheers!)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
malloc anoosh Programming 1 03-15-2006 04:41 PM
malloc eagle683 Programming 6 05-22-2005 02:40 PM
malloc in c programming saiz66 Programming 18 10-04-2004 02:00 PM
malloc() vijeesh_ep Programming 4 08-25-2004 03:50 PM
about malloc eshwar_ind Programming 11 02-18-2004 03:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration