cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted
Observer
Observer
11,772 Views
Registered: ‎06-16-2009

Array and xil_printf problem with Microblaze and ML505

Jump to solution
Hello,

I'm using the Virtex-5 ML505 board. I've loaded Microblaze onto a single-processor system. And written the following as the application code.

#include "xparameters.h"
#include "stdio.h"
#include "xbasic_types.h"
#include "xgpio.h"
#include "gpio_header.h"
#include "xsysace.h"
#include "sysace_header.h"
#include "uartlite_header.h"

int main (void) {
    Xuint32 i, *arr1;
    for(i=0;i<40;i++)
        arr1[i]=i;
    print("\r\n ARR1 ");
    print("\r\n **** ");
    for(i=0;i<40;i++)
        xil_printf("\r\n %d > %d", i, arr1[i]);
    return 0;
}

I think I should get the values 0-39 in the respective indices of the array. But I don't get the correct output on Hyperterminal. I get the outputs like 0, 1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 13... (4, 9, 14 etc. skipped!) and some junk numbers in the later locatins (above 30 or so). Please help me troubleshooting it.

0 Kudos
1 Solution

Accepted Solutions
Highlighted
Xilinx Employee
Xilinx Employee
14,003 Views
Registered: ‎01-18-2008
arr1 is not initialized, so you are writing to invalid locations. Instead of defining it as *arr1, use arr1[40]

View solution in original post

11 Replies
Highlighted
Xilinx Employee
Xilinx Employee
14,004 Views
Registered: ‎01-18-2008
arr1 is not initialized, so you are writing to invalid locations. Instead of defining it as *arr1, use arr1[40]

View solution in original post

Highlighted
Observer
Observer
11,763 Views
Registered: ‎06-16-2009
I tried to use expressions like *(arr1+i) and arr1[i] both interchangeably and consistently. But, the output was the same as before.
Tags (4)
0 Kudos
Highlighted
Historian
Historian
11,751 Views
Registered: ‎02-25-2008

jishnukd wrote:
I tried to use expressions like *(arr1+i) and arr1[i] both interchangeably and consistently. But, the output was the same as before.

You miss vsiva's point.

 

When you declare the variable as 

 

    Xuint32 *arr1;

 

you are not reserving any space for anything. All you've declared is a pointer to an unsigned integer. But that pointer isn't pointing to anything.

 

So when you do your for loop, and you access arr1[i], you are accessing garbage because  you never initialized arr1.

 

The suggestion to declare that array variable as arr1[40] is one you should heed.

 

-a

 

----------------------------Yes, I do this for a living.
Highlighted
Participant
Participant
11,749 Views
Registered: ‎06-11-2009

 

exactly do as suggested in the previous post...else check that uart settings match with the  hyperterminal connection settings, dat also sometimes causes the problem of no output.

0 Kudos
Highlighted
Historian
Historian
11,747 Views
Registered: ‎02-25-2008

wasiqnaeem wrote:

 

else check that uart settings match with the  hyperterminal connection settings, dat also sometimes causes the problem of no output.


That may be true but that's not his problem.

 

-a

----------------------------Yes, I do this for a living.
0 Kudos
Highlighted
Observer
Observer
11,718 Views
Registered: ‎06-16-2009

Yes, I missed vsiva's point. So, I'll try to use malloc for initialization so that the pointer doesn't point to garbage.

 

Thanks! I'll get back to you all asap.

0 Kudos
Highlighted
Observer
Observer
11,715 Views
Registered: ‎06-16-2009
Or I'll use arr1[40] (as an array) throughout.. Then I think I would't have to initialize it.
0 Kudos
Highlighted
Historian
Historian
11,701 Views
Registered: ‎02-25-2008

jishnukd wrote:
Or I'll use arr1[40] (as an array) throughout.. Then I think I would't have to initialize it.

This is the better solution. In general, in an embedded system with limited memory, you really want to avoid dynamic allocation (malloc and its friends). If you use static allocations (arr1[40]), the toolchain knows how much memory is required and after compiling and linking, they will tell you if your program will fit in your memory!

 

-a

----------------------------Yes, I do this for a living.
Highlighted
Observer
Observer
11,688 Views
Registered: ‎06-16-2009
Ok. I got it. Thanks! :)
0 Kudos
Highlighted
2,752 Views
Registered: ‎09-29-2011

Hello,

How can i send a float to UART if I mapped the xil_printf function to RS 232 IP?(Note:sprintf has a too large memory footprint ).

Thank you!

0 Kudos
Highlighted
Moderator
Moderator
2,741 Views
Registered: ‎11-10-2010
This is an entirely different question and should be in its own thread. Locked.
0 Kudos