I am working with cortex-m4 microcontroller from TI TM4C123GH6PM. I am trying to allocate the memory space by following c syntax
char *target; int size=10; target=(char *)malloc(size*sizeof(char)); //allocating size for string if(target==0)red; else green; free(target);
The line where I am trying to assign the allocated space address to the char pointer cause Reset Handler to trigger at line LDR R0, =SystemInit
Disassembly 0x0000090c BEAB 0xAB
commenting that line cause to glow red led, means rest of the lines are executing perfectly. Same with the calloc function. I don't have any idea about this error please help.
Have you specified any heap, so malloc() have somewhere to allocate memory?
Another thing - what do you think sizeof(char) is? Do you see any situation where sizeof(char) might have a different value?
By the way. Are you saying that
red;
is enough to turn on a red LED?
and
green;
is enough to turn on a green LED?
No function call needed? And no assign needed? Are "red" and "green" some "interesting" #define'd symbols?
Yes red and green are macros and they work just fine. I often used this method in ATMEL studio to interface GSM900, GPS, Bluetooth modules with ATmega16.
Here is the complete code, since characters are 1 byte I removed the sizeof()
#include <stdint.h> #include <stdlib.h> #include <TM4C123GH6PM.h> #include "UART0.h" #include "my_headers/gpio.h" //Library file where red, green and other colors are defined int main(){ SystemInit(); PORTF_init(); //function where onboard components are initialized char *target; int size=10; target=(char *)malloc(size); //allocating size for string if(target==0)red; green; free(target); }
That's what I used to do with AVR. Problem still persist. and I have no idea about specifying any heap. Please share me the reference.
The size of the heap is usually described in startup_xyz.s
You should also check "Use MicroLIB"
"Yes red and green are macros and they work just fine."
Don't you see a problem with a #define'd symbol that on one hand doesn't tell what it does? And doesn't indicate that there is an expansion? And is used in a way where it looks like if there is zero side effect?
A statement that visually isn't a function call, and doesn't make an assign and doesn't include any operator with a side effect should not produce any side effect.
The following is an example of a meaningless statement, that doesn't make use of the value of a variable, and doesn't change the value - but in your code the same construct is expected to have a side effect and turn on the red LED (and possibly also turn off the green LED):
int red; red;
This code indicates that maybe something named "red" is turned on:
red = On;
and this code indicates that something named "red" might have become more red:
red++;
If "red" means "turn on the red LED" and "green" means "turn on the green LED" then the open question would be "how to turn off the red LED?" "how to turn off the green LED?" "how to turn off both the red and the green LED?" "how to know if 'red' also affects the green LED?" "how to know if 'green' also affects the red LED?" "how to know if the red LED is lit?" "how to know if the green LED is lit?" "will 'black' turn off just the red LED?" "will 'black' turn off just the green LED?" "will 'black' turn off both the red and the green LED?"
The cost to maintain code, and the number of bugs, is greatly affected by how easy it is to read and understand the code. So the shortest code is seldom the quickest code to produce - typing the code lines is a small part of designing software.
Note that a compiler implementation where "char" has 16 bits, would still have sizeof(char) == 1.
So sizeof(char) isn't 1 because the char is one octet large, but because "char" is the unit type for memory sizes.
So sizeof(double) doesn't tell the number of octets, but how many "char units" that is needed to store a double.
Problem has been resolved. What I needed to do was ALLOCATE HEAP SIZE. I defined the size of heap in startup file and its working perfectly as I wanted.
Thanks to all for their attention.