Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Doubts on C

Reply
Thread Tools

Doubts on C

 
 
ranjeet.gupta@gmail.com
Guest
Posts: n/a
 
      06-13-2005
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.

 
Reply With Quote
 
 
 
 
Grumble
Guest
Posts: n/a
 
      06-13-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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?
 
Reply With Quote
 
 
 
 
ranjeet.gupta@gmail.com
Guest
Posts: n/a
 
      06-13-2005


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


 
Reply With Quote
 
ranjeet.gupta@gmail.com
Guest
Posts: n/a
 
      06-13-2005


Grumble wrote:
> (E-Mail Removed) 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

 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      06-13-2005
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
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      06-13-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Grumble wrote:
> > (E-Mail Removed) 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


 
Reply With Quote
 
Fred L. Kleinschmidt
Guest
Posts: n/a
 
      06-13-2005


(E-Mail Removed) 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 <(E-Mail Removed)>
Subject: Re: Leave Jerry Alone
X-Nntp-Posting-Host: pex003155.se.nos.boeing.com
Content-Type: text/plain; charset=us-ascii
Message-ID: <(E-Mail Removed)>
Sender: (E-Mail Removed) (Boeing NNTP News Access)
Content-Transfer-Encoding: 7bit
Organization: The Boeing Company
X-Accept-Language: en
References: <(E-Mail Removed)> <szjre.10949$(E-Mail Removed)> <LDjre.673$(E-Mail Removed)> <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

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      06-13-2005
(E-Mail Removed) 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.

 
Reply With Quote
 
Jean-Claude Arbaut
Guest
Posts: n/a
 
      06-13-2005



Le 13/06/2005 22:05, dans (E-Mail Removed),
«*Emmanuel Delahaye*» <(E-Mail Removed)> 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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-13-2005
(E-Mail Removed) 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Synchronizer doubts jahaya@gmail.com VHDL 2 07-22-2005 07:57 AM
Synchronizer doubts jahaya@gmail.com VHDL 0 07-20-2005 09:53 AM
doubts regarding dotnet reflection programming forvino@gmail.com ASP .Net 0 07-03-2005 03:23 PM
xilinx ise doubts Nisheeth VHDL 0 03-30-2005 05:00 PM
Help, .NET doubts..... MS ASP .Net 1 06-04-2004 01:16 PM



Advertisments