delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/28/14:15:31

From: Sinan_Unur AT mail DOT com (A. Sinan Unur)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Are THESE two statements EQUAL?
Date: 28 Jun 2000 18:08:06 GMT
Organization: Cornell University
Lines: 79
Sender: verified_for_usenet AT cornell DOT edu (asu1 on 128.253.251.163)
Message-ID: <8F618D940ASINANUNUR@132.236.56.8>
References: <5t7klskbj285civf9serk693otnho6t5j0 AT 4ax DOT com>
NNTP-Posting-Host: 128.253.251.163
X-Trace: news01.cit.cornell.edu 962215686 1548 128.253.251.163 (28 Jun 2000 18:08:06 GMT)
X-Complaints-To: usenet AT news01 DOT cit DOT cornell DOT edu
NNTP-Posting-Date: 28 Jun 2000 18:08:06 GMT
User-Agent: Xnews/03.04.11
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

radsmail AT juno DOT com (Radical NetSurfer) wrote in 
<5t7klskbj285civf9serk693otnho6t5j0 AT 4ax DOT com>:

>Can some one please tell me if these two statements
>are corrent  _and_  do the same thing?
>
>THANSK!
>
>BITMAP *Pieces[3234];
>PALLETE PPpal[3234];

this is getting very off topic. questions regarding the C language should 
be addressed to comp.lang.c and comp.lang.c.moderated rather than the 
DJGPP newsgroup.

you can answer your question by asking whether

double* dp[3234];
int i[3234];

do the same thing.

incidentally, the comp.lang.c faq has a good section on pointers/memory 
allocation.

>how would the above statements, be converted into malloc-type
>statements, such that the user would be able to specify ONLY as many
>Bitmaps/Palettes are required...
>
>  Pieces = (BITMAP**)malloc(3234 * sizeof(BITMAP*));

don't cast the return value of malloc if you are using C. this will 
prevent to from noticing if you forgot to include stdlib.h.

once you allocate space for an array of pointers, you need to also make 
sure each element of the array actually points to valid storage.

you can use something along the lines of:

/* untested code */

#include <stdlib.h>

int** arralloc_int(int*** p, size_t obj_count) {
    size_t i;
    
    *p = malloc(obj_count*sizeof(int));
    if( *p == NULL )
	return *p;

    for(i=0; i < obj_count; i++)  {
	printf("%lu\n", i);
	*(*p + i) = malloc(sizeof(int));
    }

    return *p;
}

void arrfree_int(int*** p, size_t obj_count) {
    size_t i;
    
    if( *p != NULL ) {
	for(i=0; i<obj_count; i++) {
	    printf("%lu\n", i);
	    if( *(*p + i) != NULL ) {
		free(*(*p + i));
		*(*p + i) = NULL;
	    }
	}
	free(*p);
	*p = NULL;
    }

    return;
}
-- 
--------------------------------
A. Sinan Unur
http://www.unur.com/

- Raw text -


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