C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 22nd, 2005, 04:54 PM
Kenji Miyamoto Kenji Miyamoto is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 433 Kenji Miyamoto User rank is Private First Class (20 - 50 Reputation Level)Kenji Miyamoto User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 21 h 30 m 13 sec
Reputation Power: 5
Glibc: "Invalid Next Size"

I'm getting this error from glibc:
Code:
*** glibc detected *** free(): invalid next size (fast): 0x0804b090 ***
With the following code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <curses.h>
#define DEFAULT 0
#define PREC 1
// HELP
#define ARGERR "USAGE:\n\
equ op n1 [n2 ... nX]\n\
n1: First parameter\n\
n2: Second parameter\n\
nX: Xth parameter\n\
op:\n"
#define ALLOPTS "OPTIONS:\n\
	linear: f(x)= ax+b\n\
	power: f(x)= a(b)^x\n\
	exponent: quadratic+, highest degree a, first coefficient b\n"
#define LINUS "f(x)= ax+b\n\
USAGE:\n\
equ linear a b\n"
#define POWUS "f(x)= a(b)^x\n\
USAGE:\n\
equ power a b\n"
#define EXPUS "Quadratic and up; highest degree a, first coefficient b\n\
USAGE:\n\
equ exponent a b [c ... ]\n"

enum opType {
	linear,
	power,
	exponent
};
enum set {
	yes,
	no
};
struct ops {
	enum opType type;
	unsigned short int highDeg;
	double * op;
};

char * linearf(double x, double a, double b);
char * powerf(double x, double a, double b);
char * exponentf(double x, unsigned short int highDeg, double * vars);
char * autoRound(double number);
unsigned short int decimal(double number);

int main(int argc, char * argv[]) {
	struct ops vars;
	enum set funct;
	double base = DEFAULT, xv;
	short int count;
	int ch;
	char * ras;

	if(argc < 3) {
		fprintf(stderr, ARGERR);
		fprintf(stderr, ALLOPTS);
	}
	else {
		if(!strcmp("exponent", argv[1])) {
			vars.highDeg = atof(argv[2]);
			if(argc < (int) vars.highDeg + 4) {
				fprintf(stderr, EXPUS);
			}
			else {
				vars.type = exponent;
				vars.op = (double *) malloc(sizeof(double) * ((int) vars.highDeg + 1));
				for(count = 3; count <= argc - 1; count++) {
					printf("%d..", (int) count);
					vars.op[(int) count - 2] = atof(argv[(int) count]);
					printf("OK\n");
				}
				printf("111\n");
				funct = yes;
			}
		}
		else {
			fprintf(stderr, ALLOPTS);
			funct = no;
		}
		if(funct == yes) {
			printf("222\n");
			xv = 0;
			printf("333\n");
			for(count = 0; count <= 5; count++) {
				printf("444\n");
				ras = exponentf(xv, vars.highDeg, vars.op);
				printf("%s\n", ras);
				printf("555\n");
				free(ras);
				printf("666\n");
				xv += 1;
			}
			free(vars.op);
		}
	}
	return 0;
}
char * exponentf(double x, unsigned short int highDeg, double * vars) {
	printf("1\n");
	char print[64], * ret, *xc, *y;
	printf("2\n");
	double b = 0;
	printf("3\n");
	short int count;
	printf("4\n");
	xc = autoRound(x);
	printf("5\n");
	for(count = (short int) highDeg; count >= 0; count--) {
		printf("	%d\n", (int) highDeg - (int) count);
		b += vars[(int) highDeg - (int) count] * pow(x, (int) count);
	}
	printf("6\n");
	y = autoRound(b);
	printf("7\n");
	sprintf(print, "f(%s)=  %s", xc, y);
	printf("8\n");
	ret = (char *) malloc(strlen(print) + 1);
	printf("9\n");
	strcpy(ret, print);
	printf("10\n");
	free(xc);
	printf("11\n");
	free(y);
	printf("12\n");
	return ret;
}
char * autoRound(double number) {
	char * ret, print[32];
	unsigned short int count, loc = decimal(number);
	enum set found = false;
	sprintf(print, "%lf", number);
	for(count = loc + 6; count >= loc; count--) {
		if(print[(int) count] != '0') {
			if(print[(int) count] == '.') {
				count -= 1;
			}
			ret = (char *) malloc((int) count + 2);
			strncpy(ret, print, (int) count + 1);
			ret[(int) count + 1] = '\0';
			found = true;
			break;
		}
	}
	if(found == false) {
		ret = (char *) malloc(strlen(print) + 1);
		strcpy(ret, print);
	}
	return ret;
}
unsigned short int decimal(double number) {
	char string[32];
	unsigned short int count;
	sprintf(string, "%lf", number);
	for(count = 0; count <= strlen(string) - 1; count++) {
		if(string[(int) count] == '.') {
			break;
		}
	}
	return count;
}
According to the markers I've placed throughout the source, The errors occur at the first "free(ras);". What does this error mean?

Reply With Quote
  #2  
Old December 25th, 2005, 04:26 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: Israel
Posts: 217 Shadow Wizard User rank is Sergeant (500 - 2000 Reputation Level)Shadow Wizard User rank is Sergeant (500 - 2000 Reputation Level)Shadow Wizard User rank is Sergeant (500 - 2000 Reputation Level)Shadow Wizard User rank is Sergeant (500 - 2000 Reputation Level)Shadow Wizard User rank is Sergeant (500 - 2000 Reputation Level)  Folding Points: 478881 Folding Title: Super Ultimate Folder - Level 1Folding Points: 478881 Folding Title: Super Ultimate Folder - Level 1Folding Points: 478881 Folding Title: Super Ultimate Folder - Level 1Folding Points: 478881 Folding Title: Super Ultimate Folder - Level 1Folding Points: 478881 Folding Title: Super Ultimate Folder - Level 1Folding Points: 478881 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Day 11 h 56 m 39 sec
Reputation Power: 16
can't see any problem with the code... it looks like you're not alone:
http://sources.redhat.com/ml/glibc-...1/msg00052.html
however, judging from logic and from this thread:
http://softwareforums.intel.com/ids...message.id=2785
I believe the problem is that somewhere along your program, you write into memory
that you should not write into, most likely array overflow. in the above link there is
advice that might help you debug this problem:
Quote:
The first thing to try is building with -CB and see if you get any array bounds errors

sorry to be bringer of bad news and good luck!

Reply With Quote
  #3  
Old December 25th, 2005, 09:31 AM
salem's Avatar
salem salem is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2005
Posts: 2,026 salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 2 h 50 m 24 sec
Reputation Power: 839
1. Your multi-line #defined strings should be written like this.
Code:
#define ARGERR "USAGE:\n"   \
    "equ op n1 [n2 ... nX]\n"   \
    "n1: First parameter\n"     \
    "n2: Second parameter\n" \
    "nX: Xth parameter\n"      \
    "op:\n"

Long line folding within string constants isn't that well defined AFAIK.

Code:
      if(argc < (int) vars.highDeg + 4) {
        vars.op = (double *) malloc(sizeof(double) * ((int) vars.highDeg + 1));
        for(count = 3; count <= argc - 1; count++) {
          vars.op[(int) count - 2] = atof(argv[(int) count]);

I see fudge factors of 1, 2, 3, 4 in this code - there's sure to be an off-by-1 error in all of this lot.

Also, since this is C, there is no need to cast the return result of malloc.
All those (int) casts when accessing array elements seems overly pedantic to me.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Glibc: "Invalid Next Size"


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT