Write () bad address

I am trying to write the size in bytes of a string that is defined as

#define PATHA "/tmp/matrix_a"

using code

 rtn=write(data,(strlen(PATHA)*sizeof(char)),sizeof(int)); if(rtn < 0) perror("Writing data_file 2 "); 

I am returning Writing data_file 2 : Bad address

What exactly is this bad address? The data file descriptor is open and correctly written immediately before and after the specified code segment. The data to be written to the data file must be raw, not ASCII.

I also tried to define the string as char [] with the same problem

+4
source share
5 answers

The second argument to write() is the address of the bytes you want to write, but you pass the bytes you want to write yourself. To get the address, you must store these bytes in a variable (you cannot take the address of the result of the expression). For instance:

 size_t patha_len = strlen(PATHA); rtn = write(data, &patha_len, sizeof patha_len); 
+2
source

Arguments for POSIX write() :

 #include <unistd.h> ssize_t write(int fildes, const void *buf, size_t nbyte); 

What a:

  • file descriptor
  • Buffer
  • the size

You passed two sizes instead of an address and a size.

Using:

 rtn = write(data, PATHA, sizeof(PATHA)-1); 

or

 rtn = write(data, PATHA, strlen(PATHA)); 

If you want to write the size of the string as an int , you will need an int variable to go to write() , for example:

 int len = strlen(PATHA); rtn = write(data, &len, sizeof(len)); 

Note that you cannot just use the size_t variable if you do not want to write size_t ; on 64-bit Unix systems, in particular, sizeof(size_t) != sizeof(int) in general, and you need to decide what size you want to write.

You also need to know that some systems are low-risk and other big-endian, and what you write using this mechanism for one type will not be readable for another type (without working with cartography before or after the I / O operation). You can ignore this as a problem, or you can use a portable format (usually called "network order" and is equivalent to big-endian), or you can determine that your code uses the opposite order. You can write code so that the same logic is used on all platforms if you are careful (and all platforms get the same answers).

+4
source

The second argument to write() is the buffer, and the third argument is the size:

 ssize_t write(int fd, const void *buf, size_t count); 

Published code conveys a length that is interpreted as an invalid address. The compiler should have warned about this (do not ignore compiler warnings and compile with the warning level at the highest level).

Change to:

 rtn=write(data, PATHA, strlen(PATHA)); 

Note sizeof(char) guaranteed to be 1 , so it can be excluded from the size calculation.

+3
source

The Bad address error has already answered. If you want to write the size of the string, just use printf .

 printf("Length: %d\n", strlen(data)); 

Either this, or you can write a function that converts an integer to a string and prints it ... I prefer printf :)

+2
source
 rtn = write(data, PATHA, strlen(PATHA)); 

- this is what you want, I think. Arguments must be

  • file descriptor (data)
  • source buffer (your constant PATHA line)
  • The number of bytes to extract from this buffer (measured using strlen () on the same PATHA constant)

Also, to be complete, you should always check rtn for how many characters you wrote. You are not guaranteed that you write () all bytes requested for all types of descriptors. So sometimes you end up writing it in pieces, determined by the number of answers it wrote, and as far as you know, what else do you need to write.

+1
source

All Articles