NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Malloc Issues
July 08, 2010 by Starwarslegokid |
Hello everyone, Im working on a new project and im trying to use the malloc function to allocate space for nodes in a linked list. I just recently took a data structures class and saw it would be very usefull structure and decided to do it, but im having allot of issues in just allocating the space. I haven't gotten to the list making yet. I wrote some simple code just to see if malloc is working but it keeps failing and returning the null pointer. Can anyone help me on this one? Im using the Atmega168 and the nerdkits bootloader with the default fuse and clock settings I have tried a whole bunch of combinations with the malloc function but they all compiled and returned the null pointer. I have two of them in the code, ones commented out.
|
---|---|
July 08, 2010 by bretm |
I seem to recall something about malloc not being available by default with Nerdkits makefiles because they only upload the .text section and not the .data section. By uploading the .data section it includes the initialization code that allows malloc to do its stuff. Look for "-j .text" in the makefile and add "-j .data" to the same command. Or something like that...I don't have files in front of me to look at right now. |
July 08, 2010 by Starwarslegokid |
lol what ya know, that was it! Thanks bretm! My next question, is there any specific reason or added benefit for leaving the -j .data out of the makefile? Thanks! Scott |
July 09, 2010 by bretm |
Two main reasons: 1) the initialization code takes space and time, and many apps don't need it, and 2) it's often better to use program space instead of SRAM for large, static data structures. Because the Atmega has a lot more program space than SRAM, it's more efficient to use program space for large data structures whenever possible. For example if you have a large list of strings or a font or other data, you'll want to put it in program space. That's both because the SRAM space is more scarce, but also because if the data doesn't change during program execution you don't want to burn the limited SRAM read/write cycles. Programs for small microcontrollers are much less likely to need malloc or other dynamic heap access when compared to apps built for a larger systems. If you only have a few kilobytes, you shouldn't just use malloc out of habit. It's often easier to design the application in a different way. Explicitly requiring .data can prevent bad habits from forming for people new to microcontrollers. Clearly some applications do require it, but the .data initialization code should only be there when the app actually needs it, as it takes up some space. |
October 30, 2010 by kle8309 |
This was a lot of help since I need to allocate mem for my struct pointer thanks |
October 30, 2010 by kle8309 |
// calipers.c // for Caliper DRO with ATmega328p // Kelvin Le // 10-29-2010 define F_CPU 14745600include <stdio.h>include <stdlib.h>include <avr/io.h>include <avr/interrupt.h>include <avr/pgmspace.h>include <inttypes.h>include "../libnerdkits/delay.h"include "../libnerdkits/lcd.h"include "../libnerdkits/uart.h"// PC5 is clock led // PC3 is data led // PC0 is power led // PB1 DATA in // PB2 CLK in // PB3 power switch // these are the data types for data storage // bit 9-16 is 16 bits and 17-24 is 32 bits struct multi_Arr
{ }; // this is the hard part uint8_t read(void){
} void digital_read_out(struct multi_Arr *DRO){
} void caliper_power_on() {
// SET PINS AS INPUT
DDRB |= 0x00;
// SET PINS AS OUTPUT
DDRC |= 0xff;
DDRB |=(1<<PB3);
// PULL UP PINS 1 2 3
PORTB|=(1<<PB1)|(1<<PB2)|(1<<PB3); // turning off and on again will eliminate start up error
PORTB&=~(1<<PB3);
delay_ms(500);
PORTB|=(1<<PB3); // Light Indicator PORTC|=(1<<PC0); } //////////////////////////////MAIN////////////////////////////////////// int main() { // start lcd
lcd_init();
FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
lcd_clear_and_home(); // update time uint16_t n=300; //turn on caliper caliper_power_on(); // init DRO structure pointer struct multi_Arr *DRO; // allocate memory memory using calloc DRO=(struct multi_Arr *)calloc(1,sizeof(struct multi_Arr)); // skip first dro
while(--n>1){
lcd_line_four();
fprintf_P(&lcd_stream, PSTR("Begin in:%5u"),n);
delay_us(500);
}
lcd_clear_and_home(); while(1){
return 0; } |
Please log in to post a reply.
Did you know that you can follow NerdKits on Facebook, YouTube, and Twitter? Learn more...
|