#include<stdio.h>
#include<string.h>
#define USER_MEM (10*1024)
typedef struct {
unsigned short int vol_level;
int mute_stat;
}audio_state;
static audio_state aud_stat;
static unsigned char user_mem[USER_MEM];
void aud_read(unsigned char * data)
{
unsigned short pos =0;
memcpy(data,&user_mem[pos],sizeof(data));
printf("The Read data is:%c",*data);
}
void aud_write(unsigned char * data)
{
unsigned short pos =0;
memcpy(&user_mem[pos],data,sizeof(user_mem[pos]));
printf("The written data is:%s",*data);
}
int main()
{
aud_stat.vol_level=10;
aud_stat.mute_stat=20;
aud_write((unsigned char*)&aud_stat);
aud_read((unsigned char*)&aud_stat);
}
This program is throwing a segmentation fault. I wanted to read some bytes of data as well as to write some bytes of data. I have written the above code but it’s throwing an error as seg fault. Please help me to resolve this issue.
EDITED
#include<stdio.h>
#include<string.h>
#define USER_MEM (10*1024)
typedef struct {
unsigned short int vol_level;
int mute_stat;
}audio_state;
static audio_state aud_stat;
static unsigned char user_mem[USER_MEM];
void read(unsigned char * data,unsigned short num)
{
printf("Into Read!\n");
unsigned short pos =0;
memcpy(data,&user_mem[pos],num);
printf("The Read data is:%c",*data);
}
void write(unsigned char * data,unsigned short num)
{
printf("Into Write!\n");
unsigned short pos =0;
memcpy(&user_mem[pos],data,num);
printf("The written data is:%c",*data);
}
int main()
{
aud_stat.vol_level=10;
aud_stat.mute_stat=20;
write((unsigned char*)&aud_stat,sizeof(audio_state));
read((unsigned char*)&aud_stat,sizeof(audio_state));
}
output:
First, your use of
read()andwrite()shadows the system-suppliedread(2)andwrite(2)routines. This is a giant mistake. (You can replace the system-supplied system call wrappers but you had better make sure you do as good a job programming them as the system C library authors did in the first place. Yours aren’t even close to what the system-suppliedread(2)andwrite(2)functions do.) Yourprintf(3)call will try to usewrite(2)internally to write your output and will find your implementation instead. Because yours handles its parameters very differently than thewrite(2)implementation, it’ll probably die on thatmemcpy()call — you’ve dereferenced the first argument towrite()as if it were a pointer, butprintf(3)will call it with an integer like1. Dereferencing1is a sure-fire way to segfault.Second, you cannot use
sizeofon an array passed into a function as a parameter. Arrays passed as parameters decay to pointers — your function cannot determine if it was called with an array or a character pointer, andsizeofis going to calculate (at compile time!) the size of a pointer. Huge difference. Either pass array sizes in the parameters or use compile-time#definesto make them the same across the whole project.Third:
This has the effect of passing a single character to
printf(3)but your format string suggested you were going to pass a “string”. C strings areNUL-terminatedchararrays — who knows when the next'\0'byte in the input you’ve given it is going to come.Fourth:
You’re making dangerous (and needless) casts away from your structure type to a completely unrelated type. Your new
write()replacement should look something more likevoid write_aud(audio_state *a), so you can work with your objects directly.I strongly recommend reading The C Programming Language by Kernighan and Ritchie before spending much more time on this program — trying to debug this one into existence is going be a painfully slow way to learn C.