www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/12/10/05:14:30

Date: Sat, 10 Dec 94 01:38 MST
From: mat AT ardi DOT com (Mat Hostetter)
To: john hoeschele <john AT umdsun2 DOT umd DOT umich DOT edu>
Subject: Re: Malloc
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu
References: <Pine DOT SUN DOT 3 DOT 91 DOT 941209233855 DOT 16887A-100000 AT umdsun2 DOT umd DOT umich DOT edu>

>>>>> "john" == john hoeschele <john AT umdsun2 DOT umd DOT umich DOT edu> writes:

    john> typedef struct sBuf { UCHAR vmbuf[1728]; }tBuf

You're missing a semicolon at the end.  Please try to post real code
if you want us to look at it; how else can we hope to spot your bug?

    john> tBuf *Bptr[1600];

    john> inside main:

    john> if(Bptr[0] = (tBuf *)malloc(1728*sizeof(tBuf)))
    john>    printf("There has been an error is allocating memory!!");
    john>    exit(1);

Your code prints out the message and quits if malloc() returns a
nonzero pointer.  But you _want_ malloc() to return a nonzero pointer,
so your test is backwards.  Furthermore, without curly braces around
the two statements following the "if", your code will ALWAYS execute
the exit(1).  And I assume you really meant to allocate 1600 *
sizeof(tBuf) bytes?  A newline at the end of the printf string would
also be a good idea, or use puts().

    john> I thought about just using a two dimensional array
    john> (array[1728][1600]) but I wanted to use the malloc function.
    john> Can anyone give me any advice or input on what could a
    john> solution???

Using a two dimensional array will result in faster code, since the
compiler won't have to generate an extra indirection to figure out the
target address.  And surely you meant "array[1600][1728]".

    john> Would it be better to allocate each 1728 chunk of memory
    john> separate than the whole thing all at once??

All at once, since most malloc()'s round up the size of each chunk
they allocate.

-Mat

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019