Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12600 Discussions

Reboot when running program with two .c files

Altera_Forum
Honored Contributor II
1,098 Views

I wrote a very simple test program that runs just fine: 

 

int main(int argc, char *argv) {  unsigned char *p = (unsigned char *) malloc(2048*sizeof(unsigned char));  printf("Allocation via malloc() succeeded\n");  free(p); } 

 

I am not using the NIOS II IDE to build the application, rather I use a Makefile and build from the command line. I altered this test program every so slightly, by simply moving the code that appears in main to its own separate .c file, as shown below. 

 

test_malloc.c

 

extern void foo(void); int main(int argc, char *argv) {  foo(); } 

 

foo.c

 

#include <unistd.h># include <stdlib.h># include <stdio.h> void foo() {  unsigned char *p = (unsigned char *) malloc(2048*sizeof(unsigned char));  printf("Allocation via malloc() succeeded\n");  free(p); } 

 

This builds, but when I run it I think I&#39;m getting a crash. uClinux reboots - this is very strange, and I am wondering if it has something to do with my makefile and how I am linking things. Perhaps my arguments to elf2flt are incorrect? Any help would be most appreciated, and my Makefile follows. 

 

include ../Rules.mak VERSION=$(shell cat ../../VERSION) CFLAGS += $(INCLUDES) -O2 -nostdinc -Wall -DLINUX -g SRC = test_malloc.c foo.c OBJ = $(addsuffix .o, $(basename $(SRC))) LIBDIRS += -L/cygdrive/c/Altera/kits/nios2_51/bin/eclipse/plugins/com.microtronix.nios2linux.uClibc_1.4.0/lib LDFLAGS += -elf2flt LIBS += -lc all: test_malloc.exe test_malloc.exe: $(OBJ) Makefile#       $(CC) -o $@ $(OBJ) $(LIBDIRS) $(LIBS)#       $(CC) $(CFLAGS) $< -o $@ install: all        cp test_malloc.exe /cygdrive/c/opt/toga/linux_root/home/ clean:        rm -f test_malloc.exe $(OBJ) .PHONY: all install uninstall clean
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
353 Views

hi, 

 

--first thing :u try to allocate 2048 of unsigned char which is equal to 2 Mb, I think it&#39;s to much ! regardless your RAM size ? 

 

--seconde thing : to know that malloc() succeded u must do a test like :  

if (p =! NULL) then ... 

 

so I advice u to reduce the allocation size (something like 512 bytes). http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/rolleyes.gif  

 

best regards.
0 Kudos
Altera_Forum
Honored Contributor II
353 Views

 

--- Quote Start ---  

originally posted by sdhnain@May 30 2006, 11:01 AM 

hi, 

 

--first thing :u try to allocate 2048 of unsigned char which is equal to 2 mb, i think it&#39;s to much ! regardless your ram size ? 

 

--seconde thing : to know that malloc() succeded u must do a test like :  

if (p =! null) then ... 

 

so i advice u to reduce the allocation size (something like 512 bytes).  http://forum.niosforum.com/work2/style_emoticons/<#emo_dir#>/rolleyes.gif  

 

best regards. 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=15693)</div> 

--- Quote End ---  

 

 

Well I sure hope 2K bytes isn&#39;t 2 MB, I believe that would be 2 KB. Regardless, no matter what amount I try and malloc() uClinux reboots. Note that when I "inline" the code into a single .c file, everything works just fine, which leads me to believe that this is a linker problem of some sort. 

 

Also, I am aware that I should be checking the value of the returned pointer from malloc(), but in the second case (test_malloc.c and foo.c), I never even get that far, my board resets (seg faults?) right when the call to foo() is made. 

 

-SQ
0 Kudos
Altera_Forum
Honored Contributor II
353 Views

Never mind - I figured out the problem. It was indeed a linker problem, or rather a build problem. I fired up Eclipse and created a project in the IDE, using one of the busybox Makefiles as a template. My test program, consisting of two .c files, then ran correctly. 

 

Thanks again, 

 

-SQ
0 Kudos
Altera_Forum
Honored Contributor II
353 Views

 

--- Quote Start ---  

originally posted by squreshi@May 31 2006, 01:37 PM 

never mind - i figured out the problem.  it was indeed a linker problem, or rather a build problem.  i fired up eclipse and created a project in the ide, using one of the busybox makefiles as a template.  my test program, consisting of two .c files, then ran correctly. 

 

thanks again, 

 

-sq 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=15708) 

--- quote end ---  

 

--- Quote End ---  

 

If you work on Linux and install the binary toolchain, 

http://nioswiki.jot.com/wikihome/operating...binarytoolchain (http://nioswiki.jot.com/wikihome/operatingsystems/binarytoolchain

 

Then it is easy to compile the test program without even a Makefile. 

http://nioswiki.jot.com/wikihome/operating...ms/compilehello (http://nioswiki.jot.com/wikihome/operatingsystems/compilehello

Just, 

nios2-linux-uclibc-gcc test_malloc.c foo.c -o test_malloc -elf2flt
0 Kudos
Reply