Dynamic allocation for a 2-dimensional array

Hi!

The program logic is this:

The user runs the program with 4 parameters (2 optional):
SAMPLE_COUNT BITS [FILENAME] [DEBUG-MODE]

The program will produce a file which contains an array of bits which is SAMPLE_COUNT * BITS in size. So if the user would run the program with the following options:

the_prog 10 4

the result would be as follows (written into a file):
0000
0010
0101
0111
1001  <--- these are all char * datatypes
1011
1100  FYI: this is a partial sine wave
1101
1110
1111
1111

I have a struct as the one below:

typedef struct VHDLPACKAGE {
      int width;
      int bits;
      char name[20];
      char rom[1][1];
} VHDLPACKAGE;

What can I do to define the sizes of char rom[][] at runtime? It should be char[SAMPLE_COUNT][BITS]. Is this even possible? If not, is there a way to resize arrays after one knows the size they are going to be? Or should I give them some sort of limited sizes and terminate the array with '\0' after it's been built?

Here is the whole code I'm using right now:

#include <stdio.h>
#include <string.h>

/* Have to use typedef to define VHDLPACKAGE */
typedef struct VHDLPACKAGE {
      int width;
      int bits;
      char name[20];
      char rom[1][1]; // <-- I'd like to define sizes to these at runtime
} VHDLPACKAGE;

/* Here you used references which are C++ feature */
/* You should use pointers instead of references  */
int make_package ( VHDLPACKAGE *p );
int print_package ( VHDLPACKAGE *p );


int main ( argc,argv )
  char *argv[];
  int argv;
{

      VHDLPACKAGE package = {0,0,"ROM",{""}};
      make_package ( &package );
      print_package ( &package );

      return 0;
}

int print_package ( VHDLPACKAGE *p )
{

      printf ( "\n\n\n\nwidth: \t%d", p->width );
      printf ( "\n\n\n\nbits: \t%d", p->bits );
      printf ( "\n\n\n\nname: \t%s" , p->name );

      return 0;
}

int make_package ( VHDLPACKAGE *p )
{

      p->width = 15;
      p->bits = 16;

      strcpy ( p->name , "MyROM" );

      return 0;
}

Is this worth more than 100 points?

Regards,

Marko
LVL 2
_marko_Asked:
Who is Participating?
 
obgConnect With a Mentor Commented:
No problem. If I understand correctly it would not require more than 100 pts. What you need to do is declare rom as char **rom; Se this as an array of arrays. You will first need to alloc the main array like

rom = malloc(sizeof(char *) * SAMPLE_COUNT);

Then you'll need to allocate each row like

for (i = 0; i < SAMPLE_COUNT; i++)
  rom[i] = malloc(BITS);

After that you can access rom[y][x] where 0 <= y < SAMPLE_COUNT and 0 <= x < BITS.
0
 
_marko_Author Commented:
And before anyone asks: No, this is not a school assignment.

My girlfriend is an ASIC designer and she complained about having to rewrite this one library all the time so she asked me if I could write a program for her which generates the library with given parameters.

The code will produce a partial sine wave with the accuracy of SAMPLE_COUNT * BITS.

Marko
0
 
obgCommented:
Furthermore, you'll need to get the values SAMPLE_COUNT and BITS from the command line:

In main, add
  SAMPLE_COUNT = atoi(argv[1]);
  BITS = atoi(argv[2]);

And you should have some check that argc is 3 or 5 before you do this.

(But change these variables to lower case, since upper case are used in #defines by standard convention.) These might even be "width" and "bits" included in the struct, right?
0
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

 
_marko_Author Commented:
I will have to integrate my codes to test this before I can give you the points. But here is the code as I think it should be:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* Have to use typedef to define VHDLPACKAGE */
typedef struct VHDLPACKAGE {
      int width;
      int bits;
      char name[20];
      char **rom;
} VHDLPACKAGE;







/* Here you used references which are C++ feature */
/* You should use pointers instead of references  */
int make_package ( VHDLPACKAGE *p );
int print_package ( VHDLPACKAGE *p );
int init_package ( VHDLPACKAGE *p , int SAMPLE_COUNT , int BITS );
int display_usage ( void );



int main ( argv,argc )
  char *argc[];
  int argv;
{


      int SAMPLE_COUNT = atoi ( argc[2] );
      int BITS = atoi ( argc[3] );
      VHDLPACKAGE package = {0,0,"ROM"};

      if ( argv <=3 ) {
         display_usage ( );
         return 1;
      }

      init_package ( &package , SAMPLE_COUNT , BITS );
      make_package ( &package );
      print_package ( &package );

      return 0;
}

int init_package ( VHDLPACKAGE *p , int SAMPLE_COUNT , int BITS )
{
      int i;
      char **rom;

      rom = malloc ( sizeof ( char * ) * SAMPLE_COUNT);
      for ( i = 0 ; i < SAMPLE_COUNT ; i++ )
           rom[i] = malloc ( BITS );

      p -> rom = rom;
}

int print_package ( VHDLPACKAGE *p )
{

      printf ( "\n\n\n\nwidth: \t%d", p->width );
      printf ( "\n\n\n\nbits: \t%d", p->bits );
      printf ( "\n\n\n\nname: \t%s" , p->name );

      return 0;
}

int display_usage ( void )
{
      printf ( "too few parameters\n" );
}


int make_package ( VHDLPACKAGE *p )
{

      p->width = 15;
      p->bits = 16;

      strcpy ( p->name , "MyROM" );

      return 0;
}


Please tell me if you see some sort of major flaws in this. I'll be back in a while after some integrating and testing. I'm not sure if the declaration of 'rom' in 'struct' is correct.

Marko
0
 
_marko_Author Commented:
Oh, I didn't receive any mail for your last post. I'll change them to lower case. I try to write as conventional code as possible because I personally dislike obfuscated code, but I am unfamiliar with C.

Marko
0
 
_marko_Author Commented:
obg:

Thank you very much! It worked very fine indeed :) I will supply the code in case someone searches the EE database for a problem with two-dimensional arrays.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* Have to use typedef to define VHDLPACKAGE */
typedef struct VHDLPACKAGE {
     int width;
     int bits;
     char *name;
     char **rom;
} VHDLPACKAGE;


/* Here you used references which are C++ feature */
/* You should use pointers instead of references  */
int make_package      ( VHDLPACKAGE *p , char *filename );
int print_package      ( VHDLPACKAGE *p );
int init_package      ( VHDLPACKAGE *p , int sample_count , int bits , char *name );
char *build_filename      ( VHDLPACKAGE *p );
int display_usage      ( void );



int main ( argv,argc )
 char *argc[];
 int argv;
{

     int sample_count = atoi ( argc[1] );
     int bits = atoi ( argc[2] );
     char *name = malloc ( sizeof ( char * ) );
     char *filename = malloc ( sizeof ( char * ) );

     VHDLPACKAGE package = {0,0};

     strcpy ( name , argc [3] );

     if ( argv <=2 ) {
        display_usage ( );
        return 1;
     }

     init_package ( &package , sample_count , bits , name );
     filename = build_filename ( &package );
     make_package ( &package , filename );
     print_package ( &package );

     return 0;
     
}





int init_package ( VHDLPACKAGE *p , int sample_count , int bits , char *name )
{

     int i;
     char **rom;

     rom = malloc ( sizeof ( char * ) * sample_count);
     for ( i = 0 ; i < sample_count ; i++ )
          rom[i] = malloc ( bits );

     if ( strcmp ( name , "" ) == 0 )
          strcpy ( name , "ROM" );

     p -> rom = rom;
     p -> width = sample_count;
     p -> bits = bits;
     p -> name = name;

}

int print_package ( VHDLPACKAGE *p )
{

     printf ( "\n\n\n\nwidth: \t%d", p->width );
     printf ( "\n\n\n\nbits: \t%d", p->bits );
     printf ( "\n\n\n\nname: \t%s" , p->name );

     return 0;
}

int display_usage ( void )
{
     printf ( "too few parameters\n" );
}


char *build_filename ( VHDLPACKAGE *p )
{

     char *filename;
     char *buffer;
     buffer = malloc ( sizeof ( char * ) * (p -> width) );
     
     strcat ( filename , p -> name );
     strcat ( filename , "_" );
     strcat ( filename , itoa ( p -> bits , buffer , 10 ) );
     strcat ( filename , "_" );
     strcat ( filename , itoa ( p -> width , buffer , 10 ) );
     strcat ( filename , ".VHD" );

     printf ( "%s", filename );

     return filename;

}


int make_package ( VHDLPACKAGE *p , char *filename )
{
     


     return 0;
}
0
 
obgCommented:
Thanks for the points! - Nice to be able to make something useful once in a while... ;-)

The code looks nice. Just one minor comment; Usually C-programs have their main-function in the bottom. That's where people (I at least) are used to look for it.
0
All Courses

From novice to tech pro — start learning today.