>>
for( i=0; i < 1000; i++, t++, f++)
memcpy((void *)t, (void *)f,sizeof(unsigned long));
>>
didn't work as intended, with gcc on linux.
Main Topics
Browse All TopicsCan anyone tell me what I am doing wrong ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
unsigned long *f = (unsigned long *)malloc( 1000 * sizeof(unsigned long));
unsigned long *t = (unsigned long *)malloc( 1000 * sizeof(unsigned long));
int i ;
for(i=0;i < 1000 ;i++) f[i]=i;
for( i=0; i < 1000; i++, t++, f++)
memcpy(t,f,sizeof(unsigned
for(i = 0; i < 1000; i++)
printf("%u\n",t[i]);
free(f);
free(t);
return 0;
}
------------------
I know the efficient way would be to use
memcpy(t, f, 1000*sizeof(unsigned long) [ which works fine btw]
instead of
for( i=0; i < 1000; i++, t++, f++)
memcpy(t,f,sizeof(unsigned
but for some odd reason i want to call memcpy 1000 times . I am sure it must be some stupid mistake I am making.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>for( i=0; i < 1000; i++, t++, f++)
You change t and f in the loop ... by the time you reach end of the loop, these pointers are pointing to the last memory locations which you allocated ... printing from there onwards would get you junk or if you are lucky, seg fault ... freeing them is also dangerous as you are not freeing what you allocated ...
save the starting address of allocated memory for future use!!!!!
Yes, memcpy(t, f, 1000*sizeof(unsigned long)) is definitely the way to go.
But your logic bug is independent of the inefficiencies. The problem is that
after the middle for loop, t no longer points at the beginning of the allocated block.
You have incremented the pointer 1000 times (t++), so it now points at
the end of the allocated block. When you attempt to print out the values
in the last for loop, you run off the end of the allocated block.
Sunny you are right. I think we can change the code like
int main(){
unsigned long *f = (unsigned long *)malloc( 1000 * sizeof(unsigned long));
unsigned long *t = (unsigned long *)malloc( 1000 * sizeof(unsigned long));
int i ;
for(i=0;i < 1000 ;i++) f[i]=i;
for( i=0; i < 1000; i++)
memcpy((void *)&t[i], (void *)&f[i],sizeof(unsigned long));
for(i = 0; i < 1000; i++)
printf("%u\n",t[i]);
free(f);
free(t);
return 0;
}
I hope this should work in all the environ.
Business Accounts
Answer for Membership
by: arun_vipPosted on 2005-06-28 at 20:47:09ID: 14325234
Hi, long));
As you rightly pointed out that
memcpy(t, f, 1000*sizeof(unsigned long)
is a efficient way.
If you need to use like
for( i=0; i < 1000; i++, t++, f++)
memcpy(t,f,sizeof(unsigned
then
can you try
for( i=0; i < 1000; i++, t++, f++)
memcpy((void *)t, (void *)f,sizeof(unsigned long));