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 12-13-2017, 11:47 AM   #1
zazamama
LQ Newbie
 
Registered: Feb 2010
Location: France
Distribution: Debian STRETCH 9.1
Posts: 10

Rep: Reputation: 0
*** Error in `./a.out': free(): invalid next size (fast): 0x0000560569d5d830 ***


Hello,
my program: (compiled with : gcc -lm prog1.c)
Code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>

// Returns the exponent of the largest power of 2 less than x.

int pow2(int x)
{
	int i;
	for(i=0; pow(2, i)<=x; i++)
		;
	return i-1;
}

int main()
{
	int a, n, k, i;

	printf("give a positif integer \n");
	scanf("%d", &a);
	k = n = pow2(a);
	printf("n = %d\n", k);
	int *tab = (int *)malloc((n+1)*sizeof(int));
	for(i=0; i<=n; i++)
		tab[i]=0;

	for(i=0; i<=n; i++)
	{
		tab[n-k]=1;
		a=a-pow(2, k);
		k=pow2(a);
	}

	for(i=0; i<=n; i++)
		printf("%d ", tab[i]);
	
	printf("\n");

	free(tab);

	return 0;
}
all work fine until 31 but for 32 I get:

32
n = 5
1 0 0 0 0 0
*** Error in `./a.out': free(): invalid next size (fast): 0x0000560569d5d830 **

if in the line : int *tab = (int *)malloc((n+1)*sizeof(int));
I replace n+1 with n+2 the problem disappears:

255
n = 7
1 1 1 1 1 1 1 1

I do not understand why n + 1 does not work beyond 31 ?

Thanks in advance for your help

Last edited by zazamama; 12-14-2017 at 04:22 PM.
 
Old 12-13-2017, 12:02 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
If you allocate space for 'n' elements, the indexes are 0..n-1
If you allocate for 'n+1' elements, the indexes are 0..n
If you use an index outside the range, the results are unpredictable.
 
Old 12-13-2017, 12:21 PM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
you may try to use ddd (data display debugger) to check how your program works.
 
Old 12-13-2017, 02:50 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Put some print statements in. Whenever you subscript an array, just print out what you're doing, eg: printf("tab[%d]=1\n",n-k);

In this case, this is the result for 31:
Code:
$ ./a.out 
give a positif integer                                                                              
31                                                                                                     
n = 4                                                                                                  
tab[0]=1                                                                                                  
tab[1]=1                                                                                                     
tab[2]=1                                                                                                        
tab[3]=1                                                                                                        
tab[4]=1                                                                                                           
1 1 1 1 1
And for 32:
Code:
$ ./a.out                                                                                     
give a positif integer                                                                                                 
32                                                                                                                          
n = 5                                                                                                                       
tab[0]=1                                                                                                                    
tab[6]=1                                                                                                                        
tab[6]=1                                                                                                                        
tab[6]=1                                                                                                                        
tab[6]=1                                                                                                                            
tab[6]=1                                                                                                                            
1 0 0 0 0 0
You're trying to assign values to tab[6], which doesn't exist.

Last edited by suicidaleggroll; 12-13-2017 at 02:54 PM.
 
Old 12-13-2017, 04:54 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.
 
Old 12-14-2017, 03:59 PM   #6
zazamama
LQ Newbie
 
Registered: Feb 2010
Location: France
Distribution: Debian STRETCH 9.1
Posts: 10

Original Poster
Rep: Reputation: 0
*** Error in `./a.out': free(): invalid next size (fast): 0x0000560569d5d830 ***

Thank you to everyone who answered.
suicidaleggroll, your explanation is crystal clear and now I understand why n+2 did the trick.
with gdb I understood that it's at line 40 that the problem was occurring with free but I did not understand why.

Thanks again to all

PS Excuse me for forgetting
Code:
 ..
Gerard.
 
  


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
free(): invalid next size (fast) Error dns Programming 3 03-09-2012 03:41 AM
[SOLVED] Why do I get corrupted memory? Error: free(): invalid next size (fast) C++arl Programming 3 04-19-2011 08:22 AM
C++ -- glibc detected *** a.out: free*(): invalid next size (fast): Eikcuhc Programming 2 11-07-2010 02:16 AM
*** glibc detected *** free(): invalid next size (fast): 0x0804a1a8 *** bartonski Programming 1 03-08-2010 02:57 AM
"free(): invalid next size (fast)" C error homer_3 Programming 2 06-01-2009 09:41 AM

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

All times are GMT -5. The time now is 09:09 AM.

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