Solved

Dynamic allocation for a 2-dimensional array

Posted on 2001-06-27
7
210 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_
  • 4
  • 3
7 Comments
 
LVL 2

Author Comment

by:_marko_
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 2

Author Comment

by:_marko_
Comment Utility
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_
Comment Utility
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_
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now