Malloc question

I am having some problem using malloc pointer array_input correctly.

The problem I am having now is that array_input is already declared as integer pointer due to malloc() within main() function.

It is also highlighted at the last post at http://forum.xillybus.com/viewtopic.php?f=4&t=558#p1470

What do you guy think about how I should take care of the info.addr within that "for loop" ?

Besides, how do I reference the "write_thread" variable buf at https://gist.github.com/promach/3751054163042d255f818724934799ed/1cb05b8194a69719d66589e25fdd2a92c832ddd1#file-fifo-c-L270 from within main() function ?

unsigned char *buf;


https://gist.github.com/promach/3751054163042d255f818724934799ed/1cb05b8194a69719d66589e25fdd2a92c832ddd1#file-fifo-c-L278-L279

1
2
    for (buf = info.addr; do_bytes > 0;
buf += written_bytes, do_bytes -= written_bytes)



https://gist.github.com/promach/3751054163042d255f818724934799ed#file-fifo-c-L291-L292

1
2
    for (array_input = info.addr; do_bytes > 0;
array_input += written_bytes, do_bytes -= written_bytes) 
Last edited on
Hello user123random,

Eventually someone will say this. You are asking a question about a C program on a C++ forum. In addition to that you are asking people to go elsewhere to view your code. Your question is better suited for a C forum not here, so do not expect much response.

When I tried to put your code in my IDE it would not compile because of missing header files: "pthread.h", "semaphore.h", "unistd.h" and "sys/mman.h". For me, I think, these are old header files that are no longer used and not included in my version of the compiler and IDE, VS 2015, Microsoft may have considered these old and no longer needed for C++.


Sorry I could not have been more help for now.

Andy
those are unix headers, though pthreads is portable most don't use that in win programming.

malloc returns a void *.

to use it needs a cast.

char * cp;
cp = (char*)malloc(...)

do not use malloc in c++. The only reason is to use realloc, and vectors should be used instead for that need.


malloc returns a void *.

to use it needs a cast.


This is true only if you're using a C++ compiler. You don't need, nor is it advised, to cast the return value from malloc() when using a C compiler as that cast can mask serious issues.
You don't need, nor is it advised, to cast the return value from malloc() when using a C compiler as that cast can mask serious issues.


@jib , could you explain what you mean by "serious issues" ?
I have the following error when using large argument value with https://paste.ubuntu.com/26350763/ . Why ? Note: I can't paste the whole c code here because the forum restricts input characters more than 9000

(gdb) run 1048576
Starting program: /root/phung/dpoverlay/fifo 1048576
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1".
[New Thread 0xb6bcd470 (LWP 4995)]
[New Thread 0xb63cd470 (LWP 4996)]
[Thread 0xb6bcd470 (LWP 4995) exited]
[New Thread 0xb5bcd470 (LWP 4997)]
[Thread 0xb63cd470 (LWP 4996) exited]
18.000000
[Thread 0xb5bcd470 (LWP 4997) exited] read, 1048576 written

Program received signal SIGSEGV, Segmentation fault.
0x00009466 in main (argc=2, argv=0xbefff7a4) at fifo.c:459
459 if( array_input[i] != array_hardware[i] ){
(gdb) print i
$1 = 0
(gdb) print array_input[i]
Cannot access memory at address 0xb6ed0008
(gdb) run 32
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /root/phung/dpoverlay/fifo 32
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1".
[New Thread 0xb6ed0470 (LWP 4999)]
[Thread 0xb6ed0470 (LWP 4999) exited]
[New Thread 0xb66d0470 (LWP 5000)]
[New Thread 0xb5ed0470 (LWP 5001)]
147.000000
[Thread 0xb66d0470 (LWP 5000) exited]
Test is successful!!
[Thread 0xb5ed0470 (LWP 5001) exited]
[Inferior 1 (process 4998) exited normally]
(gdb)


@jib , could you explain what you mean by "serious issues" ?


https://stackoverflow.com/a/605858
For https://paste.ubuntu.com/26352427/ , why do we need those two malloc() at lines 15 and 16 of https://paste.ubuntu.com/26352434/ ?

phung@UbuntuHW15:~/Documents/fpga_overlay/xillybus_demoapps$ gcc -g fifo_testcase.c -o fifo_testcase
phung@UbuntuHW15:~/Documents/fpga_overlay/xillybus_demoapps$ ./fifo_testcase
Segmentation fault (core dumped)
phung@UbuntuHW15:~/Documents/fpga_overlay/xillybus_demoapps$ gdb
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) file fifo_testcase
Reading symbols from fifo_testcase...done.
(gdb) run
Starting program: /home/phung/Documents/fpga_overlay/xillybus_demoapps/fifo_testcase

Program received signal SIGSEGV, Segmentation fault.
0x00000000004004fe in main () at fifo_testcase.c:20
20 array_input[i] = i;
(gdb) print i
$1 = 0
(gdb) print array_input[i]
Cannot access memory at address 0x0
(gdb)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <stdlib.h>

const int fifo_size = 10;

// pointers to two buffers
unsigned char *array_input;
unsigned char *array_hardware;
int i;  // to address the indexes of memory data pointed by 'array_input' and 'array_hardware' for initialization and comparison purposes

int main(void) {
	// your code goes here
	
	//allocate memory
	//array_input = malloc(fifo_size*sizeof(unsigned char));
	//array_hardware = malloc(fifo_size*sizeof(unsigned char));
	
	// generate inputs and prepare outputs
	for(i=0; i<fifo_size; i++){
	    array_input[i] = i;
	    array_hardware[i] = 0;
	}
	
	return 0;
}


In the code above you have two pointers pointing to address 0 (by virtue of zeroed memory for variables at file scope) and you're trying to use them as if they point to data that you have permission to modify. You don't. This results in undefined behavior.

You can never assume a pointer you haven't assigned a valid address to points to something that you have permission to either read or write.
It won't work the way you try to do it, And I'd recommend to detect those errors as quick as possible. There are programs that help doing that, such as checkmarx and others. But working hard may reduce some good results as well. Keep practicing.
Topic archived. No new replies allowed.