Solved

Dynamic allocation for a 2-dimensional array

Posted on 2001-06-27
7
217 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:_marko_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 2

Author Comment

by:_marko_
ID: 6231459
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
 
LVL 2

Accepted Solution

by:
obg earned 100 total points
ID: 6231469
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
 
LVL 2

Expert Comment

by:obg
ID: 6231510
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:_marko_
ID: 6231771
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
 
LVL 2

Author Comment

by:_marko_
ID: 6231799
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
 
LVL 2

Author Comment

by:_marko_
ID: 6232353
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
 
LVL 2

Expert Comment

by:obg
ID: 6234210
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question