Doubts on C

Discussion in 'C Programming' started by ranjeet.gupta@gmail.com, Jun 13, 2005.

  1. Guest

    Dear All

    Below are the few doubts which I got while studying about C

    1. Is there any method in C by which we can process the entire string
    in one unit,


    2. Does there exist any way to make the command line arguments
    available to other functions without passing them as arguments to
    the function.


    3. Is their limit for alloctaing the memory for calloc and malloc,
    I suppose There will be I can allocate whole the memeory in one
    slot i,e total memory of the Heap I can allocate in one slot.
    I am not sure about the above. Please do let me.
     
    , Jun 13, 2005
    #1
    1. Advertising

  2. Grumble Guest

    wrote:

    > 1. Is there any method in C by which we can process the entire string
    > in one unit


    What do you mean?

    > 2. Does there exist any way to make the command line arguments
    > available to other functions without passing them as arguments to
    > the function.


    Sure, copy them to global variables.

    static int myargc;
    static char **myargv;

    int main(int argc, char **argv)
    {
    myargc = argc;
    myargv = argv;
    ...
    return 0;
    }

    > 3. Is their limit for alloctaing the memory for calloc and malloc,
    > I suppose There will be I can allocate whole the memeory in one
    > slot i,e total memory of the Heap I can allocate in one slot.


    What's a heap? ;-)
     
    Grumble, Jun 13, 2005
    #2
    1. Advertising

  3. Guest

    Lawrence Kirby wrote:
    > On Mon, 13 Jun 2005 07:22:40 -0700, ranjeet.gupta wrote:
    >
    > > Dear All
    > >
    > > Below are the few doubts which I got while studying about C
    > >
    > > 1. Is there any method in C by which we can process the entire string
    > > in one unit,

    >
    > There are various standard library functions such as strcpy() and strlen()
    > which work on entire strings.
    >
    > > 2. Does there exist any way to make the command line arguments
    > > available to other functions without passing them as arguments to
    > > the function.

    >
    > You could always store argc and argv in static or "global" vaiables and
    > access those from the function. It is usually better practie to pass as
    > arguments though.


    int fucntion(int, FILE *fp);

    int main (int argc, Char *argv[]) {

    FILE *fp = fopen("argv[1]", "r+b");
    function(argc, fp);
    return 0;
    }

    int fucntion (int pased_argc, FILE *fp) {

    /* you process your data over here */

    return (/* Desired value to be retunred */);
    }

    So you mean like this ?? suppose I have the 2 arguments which I
    I am using as the comand line argument, So can I access the
    same file into the other file also?. I dont have the compiler to
    check at present.

    And also let me know if there is better solution then above (If
    above is correct),

    Regards
    Ranjeet



    >
    > > 3. Is their limit for alloctaing the memory for calloc and malloc,
    > > I suppose There will be I can allocate whole the memeory in one slot
    > > i,e total memory of the Heap I can allocate in one slot. I am not
    > > sure about the above. Please do let me.

    >
    > That's up to the particular implementation, how much memory it has
    > available, the maximum size of object it can support and so on. The
    > largest value you can REQUEST is the largest value you can pass to the
    > function. malloc() takes an argument of type size_t which should be able
    > to take values up to at least 32767 (C90) or 65535 (C99) in a hosted
    > environment (a normal one). That doesn't mean malloc() will succeed for
    > values less than that, it may not have the resources to honour your
    > request.
    >
    > Lawrence
     
    , Jun 13, 2005
    #3
  4. Guest

    Grumble wrote:
    > wrote:
    >
    > > 1. Is there any method in C by which we can process the entire string
    > > in one unit

    >
    > What do you mean?


    Got the answer as I was also bit baffled by the above satement,
    as answered by Lawrence it is strcpy, strdup...etc....

    >
    > > 2. Does there exist any way to make the command line arguments
    > > available to other functions without passing them as arguments to
    > > the function.

    >
    > Sure, copy them to global variables.
    >
    > static int myargc;
    > static char **myargv;
    >
    > int main(int argc, char **argv)
    > {
    > myargc = argc;
    > myargv = argv;
    > ...
    > return 0;
    > }
    >


    Thansk for your answer.

    > > 3. Is their limit for alloctaing the memory for calloc and malloc,
    > > I suppose There will be I can allocate whole the memeory in one
    > > slot i,e total memory of the Heap I can allocate in one slot.

    >
    > What's a heap? ;-)


    As far as I know The memory is divided into the the satck, heap,
    data segment, and the code segment, Form the heap only all the
    dynamic allocation is done through calloc and malloc,

    I suppose you are kidding ;-) Please let me know If I am wrong
    in my understanding.

    Regards
    Ranjeet
     
    , Jun 13, 2005
    #4
  5. On Mon, 13 Jun 2005 07:22:40 -0700, ranjeet.gupta wrote:

    > Dear All
    >
    > Below are the few doubts which I got while studying about C
    >
    > 1. Is there any method in C by which we can process the entire string
    > in one unit,


    There are various standard library functions such as strcpy() and strlen()
    which work on entire strings.

    > 2. Does there exist any way to make the command line arguments
    > available to other functions without passing them as arguments to
    > the function.


    You could always store argc and argv in static or "global" vaiables and
    access those from the function. It is usually better practie to pass as
    arguments though.

    > 3. Is their limit for alloctaing the memory for calloc and malloc,
    > I suppose There will be I can allocate whole the memeory in one slot
    > i,e total memory of the Heap I can allocate in one slot. I am not
    > sure about the above. Please do let me.


    That's up to the particular implementation, how much memory it has
    available, the maximum size of object it can support and so on. The
    largest value you can REQUEST is the largest value you can pass to the
    function. malloc() takes an argument of type size_t which should be able
    to take values up to at least 32767 (C90) or 65535 (C99) in a hosted
    environment (a normal one). That doesn't mean malloc() will succeed for
    values less than that, it may not have the resources to honour your
    request.

    Lawrence
     
    Lawrence Kirby, Jun 13, 2005
    #5
  6. <> wrote in message
    news:...
    > Grumble wrote:
    > > wrote:
    > > > 3. Is their limit for alloctaing the memory for calloc and

    malloc,
    > > > I suppose There will be I can allocate whole the memeory in
    > > > one slot i,e total memory of the Heap I can allocate in one

    slot.
    > >
    > > What's a heap? ;-)

    >
    > As far as I know The memory is divided into the the satck, heap,
    > data segment, and the code segment, Form the heap only all the
    > dynamic allocation is done through calloc and malloc,
    >
    > I suppose you are kidding ;-) Please let me know If I am wrong
    > in my understanding.


    There is no guarantee that a given implementation has a stack, heap,
    data segment, or code segment. Where the memory for static, automatic,
    and dynamic objects comes from is largely irrelevant since you don't
    need to know those details to write portable code. In fact, knowing
    those details often leads to unintentionally writing unportable code.

    <OT>
    Some implementations may provide a way to find out the size of the
    largest request that malloc() will succeed for, but (a) there's no
    guarantee such a function exists, (b) that value may change before you
    get a chance to call malloc(), and (c) there still may be more memory
    left over after you request the maximum available, either because size_t
    isn't large enough to enumerate the entire address space or due to
    memory fragmentation.
    </OT>

    S

    --
    Stephen Sprunk "Those people who think they know everything
    CCIE #3723 are a great annoyance to those of us who do."
    K5SSS --Isaac Asimov
     
    Stephen Sprunk, Jun 13, 2005
    #6
  7. wrote:
    >
    > Lawrence Kirby wrote:
    > > On Mon, 13 Jun 2005 07:22:40 -0700, ranjeet.gupta wrote:
    > >
    > > > Dear All
    > > >
    > > > Below are the few doubts which I got while studying about C
    > > >
    > > > 1. Is there any method in C by which we can process the entire string
    > > > in one unit,

    > >
    > > There are various standard library functions such as strcpy() and strlen()
    > > which work on entire strings.
    > >
    > > > 2. Does there exist any way to make the command line arguments
    > > > available to other functions without passing them as arguments to
    > > > the function.

    > >
    > > You could always store argc and argv in static or "global" vaiables and
    > > access those from the function. It is usually better practie to pass as
    > > arguments though.

    >
    > int fucntion(int, FILE *fp);
    >
    > int main (int argc, Char *argv[]) {

    ^
    char, not Char
    >
    > FILE *fp = fopen("argv[1]", "r+b");


    No! argv[1] (if it exists) is already a null-terminated char array.
    You probably want something like this:
    if ( arvg > 0 ) {
    fopen( argv[1], "r+b" );
    }

    > function(argc, fp);


    Passing the arg count by itself us not very useful.

    > return 0;
    > }
    >
    > int fucntion (int pased_argc, FILE *fp) {
    >
    > /* you process your data over here */
    >
    > return (/* Desired value to be retunred */);
    > }
    >
    > So you mean like this ?? suppose I have the 2 arguments which I
    > I am using as the comand line argument, So can I access the
    > same file into the other file also?. I dont have the compiler to
    > check at present.
    >
    > And also let me know if there is better solution then above (If
    > above is correct),
    >
    > Regards
    > Ranjeet
    >
    > >
    > > > 3. Is their limit for alloctaing the memory for calloc and malloc,
    > > > I suppose There will be I can allocate whole the memeory in one slot
    > > > i,e total memory of the Heap I can allocate in one slot. I am not
    > > > sure about the above. Please do let me.

    > >
    > > That's up to the particular implementation, how much memory it has
    > > available, the maximum size of object it can support and so on. The
    > > largest value you can REQUEST is the largest value you can pass to the
    > > function. malloc() takes an argument of type size_t which should be able
    > > to take values up to at least 32767 (C90) or 65535 (C99) in a hosted
    > > environment (a normal one). That doesn't mean malloc() will succeed for
    > > values less than that, it may not have the resources to honour your
    > > request.
    > >
    > > Lawrence



    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Common User Interface Services
    M/S 2R-94 (206)544-5225
    #! rnews 1006
    Xref: xyzzy rec.models.rockets:558453
    Newsgroups: rec.models.rockets
    Path: xyzzy!nntp
    From: WallaceF <>
    Subject: Re: Leave Jerry Alone
    X-Nntp-Posting-Host: pex003155.se.nos.boeing.com
    Content-Type: text/plain; charset=us-ascii
    Message-ID: <>
    Sender: (Boeing NNTP News Access)
    Content-Transfer-Encoding: 7bit
    Organization: The Boeing Company
    X-Accept-Language: en
    References: <> <szjre.10949$> <LDjre.673$> <fWjre.23$yW.18@fed1read02>
    Mime-Version: 1.0
    Date: Mon, 13 Jun 2005 20:10:09 GMT
    X-Mailer: Mozilla 4.79 [en]C-CCK-MCD Boeing Kit (Windows NT 5.0; U)

    I'm going to get two, one from work and one from home..:)-)

    Fred

    David Erbas-White wrote:
    >
    > Dave Grayvis wrote:
    >
    > >
    > > Does Yours count as #001? And if so, does Mine count as #002? :)

    >
    > I want 007!!! I want 007!!!
    >
    > Oops, looks like I got 003... <G>
    >
    > David Erbas-White
     
    Fred L. Kleinschmidt, Jun 13, 2005
    #7
  8. wrote on 13/06/05 :
    > Dear All
    >
    > Below are the few doubts which I got while studying about C
    >
    > 1. Is there any method in C by which we can process the entire string
    > in one unit,


    Unclear to me. Do you mean is there a 'string' built-in C-object ? The
    answer is no. There are arrays of char called strings when terminated
    by a 0. Such 'strings' can be used with str* functions and some others.
    (*printf() etc.)

    > 2. Does there exist any way to make the command line arguments
    > available to other functions without passing them as arguments to
    > the function.


    Some dirty globals, probably.

    > 3. Is their limit for alloctaing the memory for calloc and malloc,


    Yes. For malloc(), the absolut limit is (size_t) -1. For calloc(), it's
    (size_t) -1 * (size_t) -1. Don't expect such a result, your hardware
    would be probably dead before...

    > I suppose There will be I can allocate whole the memeory in one
    > slot i,e total memory of the Heap I can allocate in one slot.
    > I am not sure about the above. Please do let me.


    Platform-dependent. Try a dichotomic approach. A returned NULL means
    allocation error.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Mal nommer les choses c'est ajouter du malheur au
    monde." -- Albert Camus.
     
    Emmanuel Delahaye, Jun 13, 2005
    #8
  9. Re : Doubts on C

    Le 13/06/2005 22:05, dans ,
    « Emmanuel Delahaye » <> a écrit :

    >> I suppose There will be I can allocate whole the memeory in one
    >> slot i,e total memory of the Heap I can allocate in one slot.
    >> I am not sure about the above. Please do let me.

    >
    > Platform-dependent. Try a dichotomic approach. A returned NULL means
    > allocation error.



    Very dangerous ! First, the program could hang: platform dependent, so
    we can imagine the program will hang on some platform if there isn't
    enough memory. Second: maybe you can allocate that much memory, but
    not in one block. With your dichotomy, you only find the largest free
    block, there can be a huge difference. And third: on MacOS X for example
    you can allocate 2GiB (in several blocks), without hurting the system.
    It will hurt only if you access this allocated memory. So a successful
    malloc doesn't mean you have that much *RAM*. You may work with 400MiB
    allocated and only 256MiB RAM, BTW. You may even allocate 2GiB, and
    access it as needed, but it's not good practice. Your suggested dichotomy
    will work only under MSDOS ;-)
     
    Jean-Claude Arbaut, Jun 13, 2005
    #9
  10. writes:
    [...]
    > 3. Is their limit for alloctaing the memory for calloc and malloc,
    > I suppose There will be I can allocate whole the memeory in one
    > slot i,e total memory of the Heap I can allocate in one slot.
    > I am not sure about the above. Please do let me.


    There is no portable way to determine how much memory is available for
    allocation (other than trying to allocate and checking whether the
    allocation succeeded).

    On some systems, allocating all available memory, or more memory than
    your program needs, is a bad idea. If there are other programs
    running simultaneously, allocating too much memory (or any other
    resource) can affect those other programs, and system performance as a
    whole.

    This is all extremely system-specific, and the details are off-topic
    here. On an embedded system, allocating all available memory might be
    perfectly appropriate.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jun 13, 2005
    #10
  11. <posted & mailed>

    wrote:

    > Dear All
    >
    > Below are the few doubts which I got while studying about C
    >
    > 1. Is there any method in C by which we can process the entire string
    > in one unit,


    It depends on what you mean "process", but strlen() does this.

    >
    >
    > 2. Does there exist any way to make the command line arguments
    > available to other functions without passing them as arguments to
    > the function.


    Certainly.

    #include <stdio.h>
    #include <stdlib.h>
    char **a;
    int c;
    int main(int argc, char **argv)
    {
    v = argv;
    c = argc;
    myfunc();
    return EXIT_SUCCESS;
    }

    void myfunc()
    {
    int i;
    for (i=0; i<c; ++i) printf("argv[%d]=%s\n", i, v);
    }


    >
    > 3. Is their limit for alloctaing the memory for calloc and malloc,
    > I suppose There will be I can allocate whole the memeory in one
    > slot i,e total memory of the Heap I can allocate in one slot.
    > I am not sure about the above. Please do let me.


    As a practical matter, there has to be SOME limit (if only because the
    hardware imposes such a limit). The C language itself imposes no limits,
    and does not have the concept of a "heap".

    --
    Remove '.nospam' from e-mail address to reply by e-mail
     
    James McIninch, Jun 14, 2005
    #11
  12. Malcolm Guest

    <> wrote
    >
    > 1. Is there any method in C by which we can process the entire string
    > in one unit,
    >

    No. With the exception of the ability to enter a string literal into a
    program

    eg
    printf("Hello world\n");

    C has no concept of strings. printf() accepts a pointer to the first
    character in the string. This is unlike most other languages. However no
    processor currently available can handle strings as units. They are always
    treated internally as sequences of bytes.
    >
    > 2. Does there exist any way to make the command line arguments
    > available to other functions without passing them as arguments to
    > the function.
    >

    No good way. You can write them to globals. A function cannot access local
    variables or parameters of its caller, and the parameters to main() are not
    an exception to this rule.
    >
    > 3. Is their limit for alloctaing the memory for calloc and malloc,
    > I suppose There will be I can allocate whole the memeory in one
    > slot i,e total memory of the Heap I can allocate in one slot.
    > I am not sure about the above. Please do let me.
    >

    All computers will eventually run out of memory and return null after
    repeated calls to malloc(). This might be after a few kilobytes or many
    terabytes.
    Often a call for one big chunk will fail but calls for two chunks of half
    the size will succeed.
     
    Malcolm, Jun 14, 2005
    #12
  13. Re : Doubts on C

    Le 15/06/2005 00:03, dans d8nk6h$57v$-infra.bt.com,
    « Malcolm » <> a écrit :

    >> 2. Does there exist any way to make the command line arguments
    >> available to other functions without passing them as arguments to
    >> the function.
    >>

    > No good way. You can write them to globals. A function cannot access local
    > variables or parameters of its caller, and the parameters to main() are not
    > an exception to this rule.
    >>


    Actually there is a way, but completely non-standard and
    non-portable - maybe you have noticed I've been stricken by the evil of
    non-standard code :).

    Since argc, argv and sometimes envp are only arguments to the main
    function, you can find them on the stack. You will need to follow
    the stack frame chain, until you find the one for "main", and you've
    got it. But it needs good knowledge of your architecture peculiarities.
    In some cases, these parameters are simply at the top of the stack,
    so you only have to look there. On some architectures, there may
    be functions for that purpose (returning CL parameters).

    But, please note, this is by no means a good way. I answered only
    because of the kind of question "Does there exist a way"... 'Could
    not resist temptation :)

    Now flame on, not so frightening for a devil...
     
    Jean-Claude Arbaut, Jun 14, 2005
    #13
  14. "Malcolm" <> writes:
    > <> wrote
    >>
    >> 1. Is there any method in C by which we can process the entire string
    >> in one unit,
    >>

    > No. With the exception of the ability to enter a string literal into a
    > program
    >
    > eg
    > printf("Hello world\n");
    >
    > C has no concept of strings. printf() accepts a pointer to the first
    > character in the string. This is unlike most other languages. However no
    > processor currently available can handle strings as units. They are always
    > treated internally as sequences of bytes.

    [...]

    C certainly does have a concept of strings. C99 7.1.1p1 defines the
    term:

    A string is a contiguous sequence of characters terminated by and
    including the first null character.

    There are no operators that act on entire strings, but there are a
    number of standard functions that do so. From a programmer's point of
    view, it should make little difference whether the operation that
    copies one string value to another is spelled "strcpy" or "=" (though
    strcpy() does leave a lot more details up to the programmer than, say,
    a Perl-style string assignment).

    For that matter, it is possible in some cases to copy a string using
    just an assignment operator (if the value being copied is a structure
    that contains a character array member whose value happens to be a
    string).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jun 15, 2005
    #14
  15. Re: Re : Doubts on C

    On Wed, 15 Jun 2005 00:48:46 +0200, Jean-Claude Arbaut wrote:

    >
    > Le 15/06/2005 00:03, dans d8nk6h$57v$-infra.bt.com,
    > « Malcolm » <> a écrit :
    >
    >>> 2. Does there exist any way to make the command line arguments
    >>> available to other functions without passing them as arguments to
    >>> the function.
    >>>

    >> No good way. You can write them to globals. A function cannot access local
    >> variables or parameters of its caller, and the parameters to main() are not
    >> an exception to this rule.
    >>>

    >
    > Actually there is a way, but completely non-standard and
    > non-portable - maybe you have noticed I've been stricken by the evil of
    > non-standard code :).


    "No good
    way" means no good way in the langauage discussed in this
    newsgroup i.e. (standard, portable) C. If you invent or use language
    extensions to do it then fine, but you are then having to use a different
    language to do it. You might say that your language has a more than
    passing similarlity to C, but then so does C++.

    Lawrence
     
    Lawrence Kirby, Jun 15, 2005
    #15
  16. Re : Re : Doubts on C


    > "No good way" means no good way in the langauage discussed in this
    > newsgroup i.e. (standard, portable) C. If you invent or use language
    > extensions to do it then fine, but you are then having to use a different
    > language to do it. You might say that your language has a more than
    > passing similarlity to C, but then so does C++.


    It's also a matter of vocabulary: take gcc, mrc or xlc, they all have
    extensions, so they all compile a language that is not C. And I suspect
    tcc, bcc, lcc and pacc have also extensions. Still, I
    can use them, and write perfect C code. Further, I can write what I
    call non-portable C (what you call non-C), in such a way that many
    compilers will compile it: just use as many #ifdef as needed.
    And each compiler will understand its extensions. Well, everybody
    knows it's not portable, but who cares ? You may or may not
    write non portable C, that's your problem. Another question, Eric
    Sosman said on comp.std.c that it's reserved to C Standard questions,
    if comp.lang.c is also reserved to C standard, you're just suggesting
    I go to hell with my "awful" ideas ? :) Maybe this NG is not for
    me. If you know of one which discusses practical C, just tell me ;-)
    YOU think only vanilla STANDARD C is C, not me. And BTW, K&R C is not C, I
    Suppose ? How fun. And all extensions on F77 are not Fortran ? That's
    bad luck, *every* F77 compiler has extensions, since the Standard
    was too narrow. Many things are too narrow... :)
     
    Jean-Claude Arbaut, Jun 15, 2005
    #16
  17. pete Guest

    Re: Re : Re : Doubts on C

    Jean-Claude Arbaut wrote:

    > Another question, Eric
    > Sosman said on comp.std.c that it's reserved to C Standard questions,
    > if comp.lang.c is also reserved to C standard, you're just suggesting
    > I go to hell with my "awful" ideas ? :)


    Future changes to the standard, are on topic on comp.std.c.

    The C programming language itself,
    being the sum of the implications of what the standard says,
    is on topic here.
    If you want to discuss K&R C, that's on topic here too.

    --
    pete
     
    pete, Jun 15, 2005
    #17
  18. Re : Re : Re : Doubts on C

    Le 15/06/2005 15:35, dans , « pete »
    <> a écrit :

    > Jean-Claude Arbaut wrote:
    >
    >> Another question, Eric
    >> Sosman said on comp.std.c that it's reserved to C Standard questions,
    >> if comp.lang.c is also reserved to C standard, you're just suggesting
    >> I go to hell with my "awful" ideas ? :)

    >
    > Future changes to the standard, are on topic on comp.std.c.
    >
    > The C programming language itself,
    > being the sum of the implications of what the standard says,
    > is on topic here.
    > If you want to discuss K&R C, that's on topic here too.


    Thank you for your answer.
     
    Jean-Claude Arbaut, Jun 15, 2005
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MS

    Help, .NET doubts.....

    MS, Jun 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    573
    Yuancai \(Charlie\) Ye
    Jun 4, 2004
  2. Replies:
    0
    Views:
    552
  3. Nisheeth

    xilinx ise doubts

    Nisheeth, Mar 30, 2005, in forum: VHDL
    Replies:
    0
    Views:
    589
    Nisheeth
    Mar 30, 2005
  4. Synchronizer doubts

    , Jul 20, 2005, in forum: VHDL
    Replies:
    2
    Views:
    1,733
  5. Synchronizer doubts

    , Jul 20, 2005, in forum: VHDL
    Replies:
    0
    Views:
    473
Loading...

Share This Page