LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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-22-2010, 04:03 PM   #1
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
C malloc() runtime assertion problems (gcc v4.4.5, target: x86_68-linux-gnu)


Hello all,

I had a C programme which was throwing strange errors at runtime - the smallest example which I have found to throw this error is as follows:

Code:
#include <stdlib.h>

int main(){
	unsigned short int* myIntArray;
	myIntArray = malloc(sizeof(unsigned short int)*200);
	int d;
	for(d=0;d<sizeof(unsigned short int)*103;d++){  // ***
		myIntArray[d] = 0;
	}
	
	char* myCharArray;
	myCharArray = malloc(sizeof(char)*2); //Error thrown by this line
	return 0;
}
The error says:

Code:
mallocErroc: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof
 (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, 
fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 
0)' failed.
[1]    19239 abort      ./mallocErroc
The strange thing is, if I change the line marked *** to:
Code:
for(d=0;d<sizeof(unsigned short int)*102;d++){
then no error is thrown. Using gdb, I've looked at the memory at myIntArray[101], myIntArray[102], myIntArray[103] and myIntArray[104], and all of them are 0. My guess is I'm missing something in the code which results in me writing off the end of myIntArray, but I can't see what it is for the life of me...

Thanks in advance
 
Old 12-22-2010, 04:20 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,386

Rep: Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808
Quote:
Originally Posted by Snark1994 View Post
My guess is I'm missing something in the code which results in me writing off the end of myIntArray
Remember that when indexing arrays, the index refers to element number, not byte number: myIntArray has 200 shorts, but d goes up to 206.
Code:
    for (d = 0; d < 200; d++)  // OK
       myIntArray[d] = 0;

    for (d = 0; d < 200*sizeof(short); d++) // OK
       ((char*)myIntArray)[d] = 0;
 
1 members found this post helpful.
Old 12-22-2010, 05:33 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 452Reputation: 452Reputation: 452Reputation: 452Reputation: 452
Quote:
Originally Posted by ntubski View Post
Remember that when indexing arrays, the index refers to element number, not byte number: myIntArray has 200 shorts, but d goes up to 206.
Code:
    for (d = 0; d < 200; d++)  // OK
       myIntArray[d] = 0;

    for (d = 0; d < 200*sizeof(short); d++) // OK
       ((char*)myIntArray)[d] = 0;
Good style is not to use literal numbers, but rather to act according to http://c-faq.com/aryptr/arraynels.html .
 
1 members found this post helpful.
Old 12-23-2010, 05:18 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Original Poster
Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Thank you very much, ntubski I had thought that array[5] accessed the 5th byte after array, not the 5th element after array. And thanks for the tip Sergei; I've come from a python/java-ey background and this is my first real experience with low-level languages, so I appreciate the help
 
Old 12-23-2010, 08:32 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 452Reputation: 452Reputation: 452Reputation: 452Reputation: 452
Quote:
Originally Posted by Snark1994 View Post
... I've come from a python/java-ey background and this is my first real experience with low-level languages, so I appreciate the help
Well, then I'd highly recommend to read about CPP ("C" preprocessor): http://gcc.gnu.org/onlinedocs/cpp/ .
 
1 members found this post helpful.
Old 12-24-2010, 12:12 AM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Original Poster
Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Ah, thanks again
 
  


Reply

Tags
malloc


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
MySQL upgrade - "malloc: ../bash/hashcmd.c:111: assertion botched" viktor_smk Linux - Server 0 02-05-2010 04:26 AM
GNU Make and GNU GCC DEBUG vs RELEASE compiler options aryan1 Programming 1 01-12-2010 12:29 PM
No rule to make target `powerpc-linux-gnu-gcc', needed by 'xxx.plugin.so' shaiva Linux - Newbie 4 11-22-2009 12:13 AM
Plz Help me to find GNU C++ Runtime Library smartgupta Linux - Software 1 09-22-2008 09:32 AM
malloc assertion botched ??? jpbarto Linux - Hardware 1 05-15-2003 01:16 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration