[prog] Definining an array in Visual C++

Jacinta Richardson jarich at perltraining.com.au
Thu Mar 27 12:49:55 EST 2003


On Tue, 25 Mar 2003, Terri Oda wrote:

> Effectively, I'm doing this:
> 	int nSize;
> 	// ...
> 	// nSize is set based on an array we read in
> 	// ...
> 	CostStruct L_data[nSize][nSize];
> 
> And the compiler dies when I hit that last line, saying that it wants a
> constant.  I tried making a little temporary constant variable 
> 	const int nSize_const = nSize;
> but that didn't seem to help.
> 
> Now, I know I could just malloc the arrays I want, but it seems to me that I
> *should* be able to do it this way, and I'm loathe to go into ifdefs if I
> don't *have* to.  
> 
> Can anyone think of something I can do to make visual c++ take array
> definitions similar to those I used in gcc?  Heck, even a cross-compiler
> might work, although it's less than ideal.

Visual C++ isn't 100% ANSI compliant, but it's close in some regards.  If
you turn ANSI compatibility on in gcc, then gcc won't allow you to
dynamically set the size of an array at compile time either.  This makes a
lot of sense if you think about it:

void function(int a)
{
     char array[a];
}

Now let's just pretend that 'a' gets read in from the user.  How on earth
are we going to ask the OS for more space now, since C code is
precompiled?  This isn't allowed in ANSI code, because the it was (if I
understand correctly) some what of a Unix hack back before ANSI.  MS took
the reasonable approach of requiring you to specify the full and proper
length of your arrays if you were going to expect it to do your memory
mangagement.  The other choice is that you manage your own memory.

A quick word of warning about managing your own memory in VC++.  Just
pretend you've got code similar to this

while(curr != NULL)
{
     if(curr->value == find)
     {
          prev->next = curr->next;
          free(curr);
          curr = NULL;    /* *** */
     }
     else
     { 
          prev = curr;
          curr = curr->next;
     }
}

Or whatever it should look like, make sure you do that *** line.  It is
essential.  Of course, it's also good programming, but under many Unixes
you can get away without it sometimes and people forget about that.  In
VC++ if you don't set the pointer who's memory you've just freed to NULL
then it won't be NULL and bad things could happen.  

So the answer to your question is, so far as I understand it:  you can't.
Either use #defines or use malloc:

	#define nSize 10;

	CostStruct L_data[nSize][nSize];

is fine, as is:

	int nSize;

	CostStruct **L_data;  /* or is it just 1 star? */

	L_data = malloc(sizeof(CostStruct) * nSize * nSize);
	if(L_data == NULL)
	{
    		fprintf(STDERR, "oops, failed to allocate memory\n");
    		exit 1;
	}

I hope these help,

	Jacinta


--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson	    |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001 	    |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |



More information about the Programming mailing list