Doubts on C

R

ranjeet.gupta

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.
 
G

Grumble

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? ;-)
 
R

ranjeet.gupta

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


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
 
R

ranjeet.gupta

Grumble said:
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....
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.
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
 
L

Lawrence Kirby

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
 
S

Stephen Sprunk

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
 
F

Fred L. Kleinschmidt

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


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


--
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 <[email protected]>
Subject: Re: Leave Jerry Alone
X-Nntp-Posting-Host: pex003155.se.nos.boeing.com
Content-Type: text/plain; charset=us-ascii
Message-ID: <[email protected]>
Sender: (e-mail address removed) (Boeing NNTP News Access)
Content-Transfer-Encoding: 7bit
Organization: The Boeing Company
X-Accept-Language: en
References: <[email protected]> <[email protected]> <[email protected]> <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
 
E

Emmanuel Delahaye

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.
 
J

Jean-Claude Arbaut

Le 13/06/2005 22:05, dans (e-mail address removed),
« Emmanuel Delahaye » said:
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 ;-)
 
K

Keith Thompson

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.
 
J

James McIninch

<posted & mailed>

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".
 
M

Malcolm

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.
 
J

Jean-Claude Arbaut

Le 15/06/2005 00:03, dans [email protected],
« Malcolm » said:
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...
 
K

Keith Thompson

Malcolm said:
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).
 
L

Lawrence Kirby

Le 15/06/2005 00:03, dans [email protected],


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
 
J

Jean-Claude Arbaut

"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... :)
 
P

pete

Jean-Claude Arbaut said:
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.
 
J

Jean-Claude Arbaut

Le 15/06/2005 15:35, dans (e-mail address removed), « pete »
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

[C#] Extend main interface on child level 0
Doubts about pointers 17
Meme generator in c 1
Doubts about pointers 71
C doubts 43
Doubts about free() 9
doubts on scanf("%100[^\n]%*[^\n]", buff); 1
Command Line Arguments 0

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top