dynamic allocation 
Author Message
 dynamic allocation

I have a proc myShell which prints a prompt, polls for input, parses
the input, and runs the specified process and arguements. I'm assuming
a maximum of 32 arguements and an inputline max of 128. I'm not very
experienced with dynamic memory allocation. Could I have some
suggestions as to how to modify this procedure to use dynamically
created arrays so as to avoid the assumptions on max values?

void myShell(void) {
   char inputline[128];  // line to read command
   char *argv[32];       // assume max of 32 arg values

   while(1) {
      cout << "Shell> ";           //print prompt
      cin.get(inputline,128);      //read input
      cin.ignore();                //clear the newline in buffer
      parseInput(inputline,argv);  //split commands into argv array
      runProc(argv);               //run the command
   }

Quote:
}

--



Tue, 12 Jul 2005 11:35:40 GMT  
 dynamic allocation

Quote:

> I have a proc myShell which prints a prompt, polls for input, parses
> the input, and runs the specified process and arguements. I'm assuming
> a maximum of 32 arguements and an inputline max of 128. I'm not very
> experienced with dynamic memory allocation. Could I have some
> suggestions as to how to modify this procedure to use dynamically
> created arrays so as to avoid the assumptions on max values?

> void myShell(void) {
>    char inputline[128];  // line to read command
>    char *argv[32];       // assume max of 32 arg values

>    while(1) {
>       cout << "Shell> ";           //print prompt
>       cin.get(inputline,128);      //read input
>       cin.ignore();                //clear the newline in buffer
>       parseInput(inputline,argv);  //split commands into argv array
>       runProc(argv);               //run the command
>    }
> }

Well, for starters this is C++, not C; and your wishes sound very much
like a job for <string> and <vector>.
--



Wed, 13 Jul 2005 00:39:49 GMT  
 dynamic allocation

wrote in comp.lang.c.moderated:

Quote:
> I have a proc myShell which prints a prompt, polls for input, parses
> the input, and runs the specified process and arguements. I'm assuming
> a maximum of 32 arguements and an inputline max of 128. I'm not very
> experienced with dynamic memory allocation. Could I have some
> suggestions as to how to modify this procedure to use dynamically
> created arrays so as to avoid the assumptions on max values?

> void myShell(void) {
>    char inputline[128];  // line to read command
>    char *argv[32];       // assume max of 32 arg values

>    while(1) {
>       cout << "Shell> ";           //print prompt
>       cin.get(inputline,128);      //read input
>       cin.ignore();                //clear the newline in buffer
>       parseInput(inputline,argv);  //split commands into argv array
>       runProc(argv);               //run the command
>    }
> }

Here's my suggestion:  Post this C++ code to comp.lang.c++ and/or
comp.lang.c++ moderated and let them explain all about C++ string
types and vectors.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
--



Wed, 13 Jul 2005 00:39:53 GMT  
 dynamic allocation

Quote:

> I have a proc myShell which prints a prompt, polls for input,
> parses the input, and runs the specified process and arguements.
> I'm assuming a maximum of 32 arguements and an inputline max of
> 128. I'm not very experienced with dynamic memory allocation.
> Could I have some suggestions as to how to modify this procedure
> to use dynamically created arrays so as to avoid the assumptions
> on max values?

> void myShell(void) {
>    char inputline[128];  // line to read command
>    char *argv[32];       // assume max of 32 arg values

>    while(1) {
>       cout << "Shell> ";           //print prompt
>       cin.get(inputline,128);      //read input
>       cin.ignore();                //clear the newline in buffer
>       parseInput(inputline,argv);  //split commands into argv array
>       runProc(argv);               //run the command
>    }
> }

To start with, this is written in C++, not C, and thus is
off-topic here.  If you correct this failing :-) and eliminate the
// comments, it could be made into acceptable C.

Having done that I suggest you start by getting an unlimited input
line.  Code to do this is available at:

   <http://cbfalconer.home.att.net/download/ggets.zip>

which handles allocation, etc.  You are responsible for freeing
the line when done with it.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!
--



Wed, 13 Jul 2005 00:39:59 GMT  
 dynamic allocation
U can use this code which is given below

main(int argc,char *argv[])
{
        char **arg=NULL;
        int i,j,flag=1;
        char **temp;
        for(;;)
        {      
                i=0;
                printf("mybash_$ ");
                for(;(a=getchar())==' '||a=='\t';)
                        ;
                if(a!='\n')
                {
                        arg=(char **)calloc(1,sizeof(char *));
                        arg[i]=(char *)calloc(60,1);
                        for(j=0;a!='\n';++j)
                        {
                                if(a==' '||a=='\t')
                                {
                                        for(;(a=getchar())==' '||a=='\t';)
                                                ;
                                        if(a=='\n')
                                                break;
                                        j=0;
                                        ++i;
                                        flag=0;                
                                        arg=(char **)realloc(arg,(i+1)*sizeof(char *));
                                        arg[i]=(char *)calloc(60,1);
                                }
                                arg[i][j]=a;
                                a=getchar();
                        }
                        arg=(char **)realloc(arg,(i+2)*sizeof(char *));
                        arg[i+1]=NULL;
                        for(j=0;arg[j]!=NULL;++j)
                                printf("%s ",arg[j]);
                                printf("\n");
                        if(strcmp(arg[0],"exit")==0||strcmp(arg[0],"EXIT")==0)
                                        return;
                        else
                                execute(arg);   ///function to execute command
                        for(j=0;j<i;++j)
                                free(arg[j]);
                        free(arg);

                }
        }

Quote:
}

Here i am assuming u have a execute() function which takes **char argument.

first arg[0] will be the command remaining will be arguments to this commands.

Here i am assuming single argument should not be more then 60 chars...

it can take as many number of arguments.....

hope it will be usefull..

Regards,
Vishnu

Quote:

> I have a proc myShell which prints a prompt, polls for input, parses
> the input, and runs the specified process and arguements. I'm assuming
> a maximum of 32 arguements and an inputline max of 128. I'm not very
> experienced with dynamic memory allocation. Could I have some
> suggestions as to how to modify this procedure to use dynamically
> created arrays so as to avoid the assumptions on max values?

> void myShell(void) {
>    char inputline[128];  // line to read command
>    char *argv[32];       // assume max of 32 arg values

>    while(1) {
>       cout << "Shell> ";           //print prompt
>       cin.get(inputline,128);      //read input
>       cin.ignore();                //clear the newline in buffer
>       parseInput(inputline,argv);  //split commands into argv array
>       runProc(argv);               //run the command
>    }
> }

--



Sun, 17 Jul 2005 10:56:06 GMT  
 dynamic allocation

Quote:

> U can use this code which is given below

> main(int argc,char *argv[])
> {
>         char **arg=NULL;
>         int i,j,flag=1;
>         char **temp;
>         for(;;)
>         {
>                 i=0;
>                 printf("mybash_$ ");
>                 for(;(a=getchar())==' '||a=='\t';)
>                         ;

Don't.  So far main is misdeclared (it returns int), a is
undeclared, printf has neither a \n or a fflush(stdout).
Indentation is excessive, useful blanks are omitted.  Necessary
#includes are missing.  Whatever it is supposed to do, it doesn't.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!
--



Tue, 19 Jul 2005 10:25:28 GMT  
 dynamic allocation

Quote:

> :-) and eliminate the
> // comments, it could be made into acceptable C.

What's wrong with C99?

(// comments are in C99 aren't they?)

-Ed
--



Tue, 19 Jul 2005 10:25:45 GMT  
 dynamic allocation


Quote:
> U can use this code which is given below

> main(int argc,char *argv[])
> {
>    char **arg=NULL;
>    int i,j,flag=1;
>    char **temp;
>    for(;;)
>    {      
>            i=0;
>            printf("mybash_$ ");
>            for(;(a=getchar())==' '||a=='\t';)
>                    ;

and when the stream gets empty the program starts to do some strange
things.

Quote:
>            if(a!='\n')
>            {
>                    arg=(char **)calloc(1,sizeof(char *));

produces undefined behavior when stdlib.h is not included!

Quote:
>                    arg[i]=(char *)calloc(60,1);

produces undefined behavior when stdlib.h is not included.

Quote:
>                    for(j=0;a!='\n';++j)
>                    {
>                            if(a==' '||a=='\t')
>                            {
>                                    for(;(a=getchar())==' '||a=='\t';)
>                                            ;
>                                    if(a=='\n')
>                                            break;

and when the stream gets empty the program does something strange
things.

Quote:
>                                    j=0;
>                                    ++i;
>                                    flag=0;                
>                                    arg=(char **)realloc(arg,(i+1)*sizeof(char *));

more undefined behavior.

Quote:
>                                    arg[i]=(char *)calloc(60,1);
dito.
>                            }
>                            arg[i][j]=a;
>                            a=getchar();
>                    }
>                    arg=(char **)realloc(arg,(i+2)*sizeof(char *));

more undefined behavior
Quote:
>                    arg[i+1]=NULL;
>                    for(j=0;arg[j]!=NULL;++j)
>                            printf("%s ",arg[j]);
>                            printf("\n");
>                    if(strcmp(arg[0],"exit")==0||strcmp(arg[0],"EXIT")==0)
>                                    return;
>                    else
>                            execute(arg);   ///function to execute command
>                    for(j=0;j<i;++j)
>                            free(arg[j]);
>                    free(arg);

>            }
>    }
> }

> Here i am assuming u have a execute() function which takes **char argument.

> first arg[0] will be the command remaining will be arguments to this commands.

> Here i am assuming single argument should not be more then 60 chars...

> it can take as many number of arguments.....

> hope it will be usefull..

> Regards,
> Vishnu

[C++ removed]

--
Tschau/Bye

Herbert Rosenau
http://www.pc-rosenau.de   eComStation Reseller in Germany
eCS 1.1 ist da!
--



Tue, 19 Jul 2005 10:25:49 GMT  
 dynamic allocation

Quote:


> > If you correct this failing ( :-) and eliminate the
> > // comments, it could be made into acceptable C.

> What's wrong with C99?

> (// comments are in C99 aren't they?)

Yes, but not C90, which most compilers still are.  And they are
especially vulnerable to line wrapping, as occurs all to often in
newsgroups.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!
--



Wed, 20 Jul 2005 12:09:58 GMT  
 dynamic allocation

Quote:

>> :-) and eliminate the
>> // comments, it could be made into acceptable C.

>What's wrong with C99?

>(// comments are in C99 aren't they?)

But where are the conforming C99 compilers?

Dan
--
Dan Pop
DESY Zeuthen, RZ group

--



Wed, 20 Jul 2005 12:10:06 GMT  
 dynamic allocation

Quote:



> >> :-) and eliminate the
> >> // comments, it could be made into acceptable C.

> >What's wrong with C99?

> >(// comments are in C99 aren't they?)

> But where are the conforming C99 compilers?

To be honest, // is a feature that many C compilers have supported
since before 1999. I know not all have, and its probably more common
in general purpose ones, but that is never the less my experience.

How good is GCC 3.2, by the way?

-Ed
--



Fri, 22 Jul 2005 09:28:49 GMT  
 dynamic allocation

Quote:



>> >> :-) and eliminate the
>> >> // comments, it could be made into acceptable C.

>> >What's wrong with C99?

>> >(// comments are in C99 aren't they?)

>> But where are the conforming C99 compilers?

>To be honest, // is a feature that many C compilers have supported
>since before 1999.

But not if invoked in conforming mode.

Quote:
>I know not all have, and its probably more common
>in general purpose ones, but that is never the less my experience.

The point is that, if you use them, you can't get a clean compile, with
the compiler invoked in conforming mode and, if you want to be sure your
code is portable, you have to invoke it in conforming mode.

Quote:
>How good is GCC 3.2, by the way?

Still broken as a C99 compiler.  There are no signs that anyone is working
on that.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

--



Sat, 23 Jul 2005 00:31:26 GMT  
 dynamic allocation

Quote:


> >To be honest, // is a feature that many C compilers have supported
> >since before 1999.
> But not if invoked in conforming mode.

Conforming C89 implementations *could* support //-comments if they
are careful to check for '*' immediately following and exempt that
case.
--



Sun, 24 Jul 2005 00:53:36 GMT  
 dynamic allocation

Quote:

> >To be honest, // is a feature that many C compilers have supported
> >since before 1999.

> But not if invoked in conforming mode.

Dang it. Good point.

Quote:
> >I know not all have, and its probably more common
> >in general purpose ones, but that is never the less my experience.

> The point is that, if you use them, you can't get a clean compile, with
> the compiler invoked in conforming mode and, if you want to be sure your
> code is portable, you have to invoke it in conforming mode.
> >How good is GCC 3.2, by the way?

> Still broken as a C99 compiler.  There are no signs that anyone is working
> on that.

I was wondering if you could point me to a brief dummary of its problems in
terms of C99 compliance.

-Ed
--



Sun, 24 Jul 2005 00:53:41 GMT  
 dynamic allocation

Quote:


>> >To be honest, // is a feature that many C compilers have supported
>> >since before 1999.
>> But not if invoked in conforming mode.

>Conforming C89 implementations *could* support //-comments if they
>are careful to check for '*' immediately following and exempt that
>case.

But they're still required to produce a diagnostic, because // is a
syntax error in C89.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

--



Sun, 24 Jul 2005 03:13:39 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. C Dynamic allocation of Pointers

2. char **info; /*dynamic allocation*/

3. Dynamic Allocation of Multi-Dimensional Array (Simple Question)

4. Dynamic Allocation

5. Dynamic allocation!

6. Dynamic allocation of 2-dimensional array

7. Dynamic Allocation of 2D Arrays

8. Dynamic allocation and reallocation of two dimensional arrays

9. Character array dynamic allocation

10. Dynamic Allocation in ISR

11. dynamic allocation problem

12. Dynamic allocation of an array of structures.

 

 
Powered by phpBB® Forum Software