Hello,

While compiling my code, I'm receiving a "Data access misaligned address violation" error. I tried to find the reason for the error, but am unable to pin point the location. I am trying to manipulate the amplitude envelope of an audio file, but this error occurs when i'm trying to multiply an array with float datatype with an array of short datatype. My code is as follows.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	FILE *fr, *fw;
	short *buf, *buf1, buffer[110516];
	float mul[55257];
	int j;
	if((fr=fopen("/var/tmp/test2.wav", "rb"))==NULL)
	{
		printf("\nCannot open wav file\n");
	}
	if((fw=fopen("/var/tmp/tmsg2.wav", "wb"))==NULL)
	{
		printf("\nCannot open tmsg2 file-to write\n");
	}
	
	mul[0]=0;
	for(j=1;j<55257;j++)
	{
		mul[j]=mul[j-1]+0.00002;  //generate a linearly increasing array
	}
	buf=(short *)malloc(sizeof(short));
	for(j=0;j<110514;j++)
	{
		fread(buf, sizeof(short), (size_t)1, fr);
		buffer[j]=*buf; //create a buffer with amplitude values samples 
	}
	free(buf);
	for(j=0; j<55257; j++)
	{
		buffer[j]=buffer[j]*mul[j]; //manipulate amplitude so that the volume increases gradually
	}
       buf1=&buffer[0];
	for(j=0;j<110514;j++)
		{
			fwrite(buf1+j, sizeof(short), (size_t)1, fw);
		}
	fclose(fr);
	fclose(fw);
	return 0;
}

Recommended Answers

All 3 Replies

Well if you compile with debug, say

gcc -g prog.c

Then try
gdb a.out

Followed by 'run' at the debugger prompt, it will catch the fault and point you at the offending line of code.

Thank you for the suggestion. I did try using the technique you've suggested and the program exited normally. The error normally happens when I load it on a microprocessor and run it from there. Thanks again for the tip, i'll figure something out.

Well if you compile with debug, say

gcc -g prog.c

Then try
gdb a.out

Followed by 'run' at the debugger prompt, it will catch the fault and point you at the offending line of code.

Well for a start, you could check all your malloc calls, to make sure they're returning a valid pointer.

>The error normally happens when I load it on a microprocessor and run it from there.
This is absolutely essential information!
How are we to know that?
Questions like
- which one?
- which compiler?
- which OS (if any)?
- how much memory does it have?
and so on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.