Hello,

I have a query on dynamic allocation of memory in C. I am storing 4 seconds of audio data in a buffer allocated with sufficient memory using malloc function. If I wanted to record another 4 seconds of audio in the same buffer, I would have to increase the size of the buffer. Is using realloc function a better way and then using memcpy to store the audio in the buffer? My goal is to store 4 seconds of audio twice in the buffer.

Thank you.

If you know the buffer will need 8 seconds of data then why not just allocate it all at the same time? Calling realloc() will work too, but will be more time consuming than allocating it all at the same time.

I believe I couldn't explain my problem clearly enough. I apologise for that. I have allocated my buffer to store 8 seconds worth of data, but the audio file is 4 seconds in length, so I wan't to copy the data twice in the same buffer so that I could have two copies of audio data in the same buffer. I tried to call memcpy twice but it stored the 1st copy of 4 seconds correctly but not the other copy. I was assuming that once data is copied in the buffer, its memory address automatically points to the next memory block so I could copy another 4 seconds from there on. But that didn't happen. I would appreciate you suggestion on this issue.

Thank you.

If you know the buffer will need 8 seconds of data then why not just allocate it all at the same time? Calling realloc() will work too, but will be more time consuming than allocating it all at the same time.

I tried to call memcpy twice but it stored the 1st copy of 4 seconds correctly but not the other copy. I was assuming that once data is copied in the buffer, its memory address automatically points to the next memory block

The pointers don't automatically 'adjust' themselves, no matter what you do. You simply have to specify the memory location where the write is to take place. So, in the second memcpy() , adjust the destination pointer so that it points to the first byte past the first block you've written.

I tried to adjust the destination pointer by adding the (size+1) of the audio buffer to the allocated destination address, but I receive a segmentation error. Following is the code snipped.

aud_data=(short *)malloc(i); // i is the file size in bytes
	cp_aud_data=(short *)malloc((i*2)+1);
	
	fread(aud_data, sizeof(short), (size_t)(i/2), fr);
	memcpy(cp_aud_data, aud_data, i);
	memcpy(cp_aud_data+(i+1), aud_data, i);

Please do point out as to how should I point the destination address correctly.

Thank you.

The pointers don't automatically 'adjust' themselves, no matter what you do. You simply have to specify the memory location where the write is to take place. So, in the second memcpy() , adjust the destination pointer so that it points to the first byte past the first block you've written.

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.