Mail Archives: djgpp/1997/11/02/19:50:08
john kismul wrote:
>
> I've got some trouble with functions like malloc() and memcpy().
>
> Does anyone have some source code that shows how to use them or know
> about a place at the WWW where I can get it?
>
> And DON'T tell me to read the info files as I already has.
Unless this question is specifically related to DJGPP, it's better asked
on a more appropriate forum, such as comp.lang.c. However...
It would be a better idea to explain exactly what you are trying to do
with them so we can give you specific advice. Best case: post the code
that gives you trouble and we'll tell you what's wrong with it. You
want examples? Just look at any program that uses malloc(), which is
probably most of them. :) memcpy() is also used fairly extensively,
but it's the simplest thing I can think of: give it an address to copy
to, an address to copy from, and the number of bytes to copy. What's so
hard?
Quick example of both, just 'cuz I'm nice:
#include <stdio.h>
#include <stdlib.h> /* malloc and free */
#include <string.h> /* memcpy */
int main( void )
{
int *list1, *list2;
int size;
printf( "Allocate how many integers? " );
scanf( "%d", &size );
if ( size <= 0 )
exit( 0 );
list1 = (int *) malloc( size * sizeof(int) );
list2 = (int *) malloc( size * sizeof(int) );
if ( list1 == NULL || list2 == NULL )
{
fprintf( stderr, "Out of memory.\n" );
exit( 1 );
}
for ( i = 0; i < size; i++ )
list1 + i = i;
/* list1[i] = i; */
memcpy( list2, list1, size * sizeof(int) );
/* print lists if you want */
free( list1 );
free( list2 );
return 0;
}
If you're trying to do things like write to VGA memory with memcpy(),
please read chapters 10 and 18 of the DJGPP FAQ (v2/faq210b.zip from
SimTel or online at http://www.delorie.com/djgpp/v2faq/) for
instructions on how to perform hardware operations in protected mode
programs.
hth
--
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I | mailto:fighteer AT cs DOT com |
| Plan: To find ANYONE willing to | http://www.cs.com/fighteer |
| play Descent 2 on DWANGO! | Tagline: <this space for rent> |
---------------------------------------------------------------------
- Raw text -