473,887 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory allocation for an initialized character pointer;

Hi all,

If I have a piece of code something like this

void main(void)
{
char * p1="abcdefghijk lmn";
............... ............... ...............

}

For p1 and whatever it points to , where is the memory allocated. Is
it on heap or stack or
some other common global area. I am assuming it should be either on
stack or some global area. Unless
a user does a malloc storage space cannot be allocated on heap is my
assumption. Can someone
throw some light on how compilers do this.

What if the above piece of code is changed to something like

void main(void)
{
char *p1;
char a[20]="abcdefgh";

strcpy(p1,a);
}

Now where is the space allocated for p1 and whatever it points to
after strcpy.

Thanks in advance.

Regards,
Ar
Sep 30 '08
50 3542
Richard Heathfield wrote:
s0****@gmail.co m said:
>On Oct 2, 1:23 am, Richard Heathfield <rj*@see.sig.in validwrote:
>>s0****@gmail.co m said:
On Oct 1, 8:20 pm, Richard Heathfield <rj*@see.sig.in validwrote:
....
accepted standards". Now, in the very narrow context of a C99
implementation that documents void main(void) in its conformance
documentation - and, incidentally, I know of no such implementation - it
C99 implementations are pretty rare, and therefore don't provide a good
sample size. I've heard that many popular C90 compilers permit void
int(void); do none of them document the fact that they permit it?

I can, unfortunately, confirm that this is the case for the only
compiler whose documentation I could quickly locate. gcc allows void
main(void), but "info gcc" section 4 "C implementation-defined behavior"
fails to mention void main(void), even though it says "A conforming
implementation of ISO C is required to document its choice of behavior
in each of the areas that are designated "implementa tion defined". The
following lists all such areas, ...".
>both rely on implementation-defined behavior, and are therefore non-
strictly-conforming.

True enough, but so what? I am not arguing that only strictly-conforming
programs are correct. I am arguing that void main(void) is not correct,
except that it may be just about possible for a truly pedantic language
lawyer to argue that it could be correct on C99 implementations that
document it as an alternative signature for main - but it would still be
incorrect on the vast majority of implementations , and indeed on ALL the
implementations I've ever used, and I've used a bundle.
void main(void) is permitted by the version of gcc installed on my home
machine (4.1.2 20070626 (Red Hat 4.1.2-13). This is true even when
compiling in either -std=c89 or -std=c99, even with all of my other
favorite options ( -pedantic -Wall -Wpointer-arith -Wcast-align
-Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes) turned on. It
produces a warning message, but it also generates an executable that
works as intended.
Oct 2 '08 #21
In article <af************ *************** *******@d31g200 0hsg.googlegrou ps.coms0****@gmail.co m writes:
On Oct 2, 1:54=A0am, Keith Thompson <ks***@mib.orgw rote:
....
By the same reasoning, we can now thing if it as UB and not
necessarily as "correct", even though it will invoke UB in some
implementations and be "correct" in others.

In a sense, yes, but I know a lot of people that think of C99 as
"allowing void main(void)" and of C89 as "*not* allowing void
main(void)". Now, I know this is just common sense, and it doesn't
really apply in CLC Pedants Club(tm), but I think it's worth taking it
in into account.
But remember that C99 also allows: "double main(double)".. .
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Oct 2 '08 #22
On Oct 2, 3:05 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
vipps...@gmail. com said:
On Oct 2, 12:39 pm, gaze...@shell.x mission.com (Kenny McCormack)
wrote:

<snip>
If my implementation documents the behavior of void main(void),
then it is required to behave as documented. I believe even KT would
agree on this point.
False, it's not required to.

Even a clock that runs backwards is correct occasionally - and how often
this happens depends only on how wrong it was to start with and how fast
it spins.

Let's focus on C99, since the whole "it's implementation-defined" business
was introduced therein. (In C90, the accuracy of documentation not
pertaining to the requirements of the Standard is a QoI issue.)

In C99, then, implementations are free to provide alternative signatures
for main "in an implementation-defined manner". Now, the Standard imposes
requirements on implementations for implementation-defined behaviour:
"each implementation documents how the choice is made". If you are right,
then implementations are free to document their choices with arbitrary
disregard for accuracy. For example, an implementation might claim in its
docs that INT_MAX is 65535 whereas it is actually 32767. So the question
is whether such an implementation is conforming (assuming it conforms in
all other ways).

Since the Standard doesn't define the verb "document", we have no
alternative but to read that word with its ordinary English meaning
(unless, of course, the word is defined in one of the normative
references, which is something I have no way of checking).

In Chambers, the verb "document" is defined as follows: "1. to record
something, especially in written form. 2. to provide written evidence to
support or prove something - from Latin /documentum/ lesson or proof."

Taking sense 1 first - if INT_MAX is 32767 but the implementation' s
paperwork says that it's 65535, then it hasn't recorded the value of
INT_MAX. It has *mis*-recorded it. Taking sense 2 - if INT_MAX is 32767
but the implementation' s paperwork says that it's 65535, then it is not
supporting INT_MAX's value, but undermining it. It is not proving it, but
lying about it. If X is Y but the lie is told that X is not-Y, the lie
neither proves that X is not-Y nor that X is Y. It's just a lie.

So it is not reasonable to suppose that documentation that the
implementation is required to provide for implementation-defined behaviour
is allowed to be incorrect.
All that you say is fine and I'd agree if void main(void) was
"implementa tion defined behavior".
The reason I disagreed with Kenny and perhaps with you is that I think
void main(void) is UB in C99. (I don't know about C90, I don't have
that standard)

Here's why

5.1.2.2.1 Program startup p 1
The function called at program startup is named main. The implementation declares no
prototype for this function. It **shall** be defined with a returntype of int and with no
parameters:
Emphasis added by me.

Then, 4. Conformance p 1
In this International Standard, ''shall'' is to be interpreted as a requirement on an
implementation or on a program; conversely, ''shall not'' is to be interpreted as a
prohibition.
What this undoubtedly means is that the program is required to be
defined with a return type of int.
If not, it's UB. Do we agree on this interprentation ?
If we do, can you claim the same you claimed about implementation
documentation for undefined behavior?
If you agree with me about void main(void), I think you can also agree
that the standard doesn't require an implementation to document UB. If
the implementation chooses so, it's outside of the scope of the C
stardard; whether the documentation lies or not is not C's concern.
Oct 2 '08 #23
vi******@gmail. com writes:
[...]
All that you say is fine and I'd agree if void main(void) was
"implementa tion defined behavior".
The reason I disagreed with Kenny and perhaps with you is that I think
void main(void) is UB in C99. (I don't know about C90, I don't have
that standard)

Here's why

5.1.2.2.1 Program startup p 1
>The function called at program startup is named main. The
implementati on declares no prototype for this function. It
**shall** be defined with a return type of int and with no
parameters:
[I've fixed the "fi" ligature above.]
Emphasis added by me.

Then, 4. Conformance p 1
>In this International Standard, ''shall'' is to be interpreted as a
requirement on an implementation or on a program; conversely,
''shall not'' is to be interpreted as a prohibition.

What this undoubtedly means is that the program is required to be
defined with a return type of int.
If not, it's UB. Do we agree on this interprentation ?
Um, no. I agree that C99 5.1.2.2.1p1 could be reasonably read that
way, and the wording should probably be cleaned up a bit, but I'm
almost certain that the *intent* is that main shall be defined:
as int main(void)
*OR*
as int main(int argc, char *argv[])
*OR*
in some other implementation-defined manner. I don't believe the
phrase "with a return type of int" was intended to cover the "some
other implementation-defined manner" case.

I note that the interpretation that it does calls for a somewhat
strained interpretation of the wording. The phrase "or in some other
implementation-defined manner" clearly (at least it's clear to me)
logically follows "It shall be defined". Compare my interpretation:
It shall be defined ... in some other implementation-defined
manner.
to yours:
It shall be defined with a return type of int ... in some other
implementation-defined manner.

As a matter of common sense (yes, I know that's a dangerous thing to
apply to the standard), I can't think of any good *reason* why an
implementation should be allowed to vary the parameters but not the
return type.

The problem with the current wording is that the phrase "with a return
type of int" appears only once, but is meant to apply to two of the
following three clauses. I think that's just a consequence of the
fact that C90 just has the first two clauses, and the third was tacked
on in C99.

But even if we accept your interpretation, that 5.1.2.2.1 doesn't
permit void main(void), 4p6 does (which is why I've long said that the
added permission in 5.1.2.2.1 is redundant):

A conforming implementation may have extensions (including
additional library functions), provided they do not alter the
behavior of any strictly conforming program.

and 4p8:

An implementation shall be accompanied by a document that defines
all implementation-defined and locale-specific characteristics and
all extensions.

Ignoring the added phrase in 5.1.2.2.1, "void main(void)" is a valid
extension. As such, an implementation that supports it is required to
document it. The documentation requirement is meaningless if an
implementation is allowed to lie in its own documentation.

In the absence of such documentation, the behavior of void main(void)
is undefined. And if an implementation does support void main(void),
it needn't behave in the obvious manner. A conforming implementation
could, as a documented extension, say that this program:
void main(void) { }
prints "main returns int, you fool!" on stderr.
If we do, can you claim the same you claimed about implementation
documentation for undefined behavior?
If you agree with me about void main(void), I think you can also agree
that the standard doesn't require an implementation to document UB. If
the implementation chooses so, it's outside of the scope of the C
stardard; whether the documentation lies or not is not C's concern.
If an implementation defines behavior that is not defined by the
standard in the document required by C99 4p8, then it must do so
accurately.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 2 '08 #24
s0****@gmail.co m writes:
On Oct 2, 1:54*am, Keith Thompson <ks***@mib.orgw rote:
>s0****@gmail.co m writes:
On Oct 1, 8:20*pm, Richard Heathfield <rj*@see.sig.in validwrote:
[...]
>C&V, please. Where does the Standard explicitly say that void
main(void) is "correct"? Indeed, where does the Standard
explicitly mention void main(void) at all?
It is implied by "or in some other implementation-defined manner" in
5.1.2.2.1. Perhaps that's not as explicit as it would be if it clearly
stated something like "main shall be defined as void main(void)", but
at least it's more explicit than simply allowing it to be accepted as
an extension (or as a result of UB).

You are wrong.

A statement like "main shall be defined as void main(void)" (which
doesn't occur in the standard) has a completely different meaning than
the "or in some other implementation-defined manner" that actually
does occur in the standard. *It's not just more or less explicit, it's
a *different statement*.

It's different in the sense that one is a mandate, the other is a
permission. But they're both referring to the same subject. Moreover,
in C99, it is an *explicit* permission.
You seem to be implying that the difference between a mandate and a
permission is not very significant. I'm just going to give up on
trying to figure out what point you're trying to make here.

But C90 provides the same permission in section 4, where it permits
extensions (supporting "void main(void)" is a valid extension).
Given that wording in 5.1.2.2.1, we can now think of it as "correct"
and not necessarily as UB, even though it will yield correct results
in some implementations and UB in others.

By the same reasoning, we can now thing if it as UB and not
necessarily as "correct", even though it will invoke UB in some
implementation s and be "correct" in others.

In a sense, yes, but I know a lot of people that think of C99 as
"allowing void main(void)" and of C89 as "*not* allowing void
main(void)". Now, I know this is just common sense, and it doesn't
really apply in CLC Pedants Club(tm), but I think it's worth taking it
in into account.
People who think that C99 allows "void main(void)" and C89 doesn't (if
there are any such people) are mistaken. Both versions of the
standard explicitly allow extensions that don't affect the behavior of
strictly conforming code.

[...]
>What point are you really trying to make here, anyway? *This entire
debate seems to be about the meaning of the word "correct", not about
the C language. *Are you arguing just for the sake of arguing, or is
there some point to all this? *Is there any real disagreement about
what the standard says, or are just playing word games?

At this point it *is* pretty much about the meaning of the word
"correct". Is it word games? Well, in a post at the beginning of this
thread you yourself said "And no, I'm not playing word games; it
really does depend on what you mean by "correct"." :-)
And your apparent meaning of "correct" is one that I find absurd.

I agree that "void main(void)" is correct for an implementation that
supports it. But suppose I'm using a conforming compiler that rejects
any program that declared "void main(void)". If I write a program
that declares "void main(void)" and submit it to that compiler, I'm
out of luck. And this is what you call "correct" -- not just for a
particular implementation, but without qualification.
>You do understand, don't you, that some implementations allow
"void main(void)" and others don't, but that all hosted
implementation s allow "int main(void)"? *I don't think you've
explicitly advocated using "void main(void)" in real code; do you
think it should be used? *If so, why? *If not, why are you so
interested in attempting to prove that it's "correct"?

I don't think void main(void) is better than int main(void), of
course, but, aside from the issue of portability, I don't think
there's any reason not to use it (provided that you're using a
compiler that supports it). And I'm puzzled about the fact that you
don't consider it to be correct, even in C99.
I'm puzzled by your puzzlement. And I'm equally puzzled as to why you
would consider using "void main(void)" even with a compiler that
supports it. It gains you nothing over "int main(void)".

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 2 '08 #25
James Kuyper said:
Richard Heathfield wrote:
>s0****@gmail.co m said:
>>On Oct 2, 1:23 am, Richard Heathfield <rj*@see.sig.in validwrote:
s0****@gmail.co m said:
On Oct 1, 8:20 pm, Richard Heathfield <rj*@see.sig.in validwrote:
...
>accepted standards". Now, in the very narrow context of a C99
implementati on that documents void main(void) in its conformance
documentatio n - and, incidentally, I know of no such implementation - it

C99 implementations are pretty rare, and therefore don't provide a good
sample size. I've heard that many popular C90 compilers permit void
int(void);
Presumably you mean void main(void) - I don't know of any compilers that
permit void int(void)!
do none of them document the fact that they permit it?
Not in their conformance docs, no. That is, none that I've ever used - and
I've used a few.
I can, unfortunately, confirm that this is the case for the only
compiler whose documentation I could quickly locate. gcc allows void
main(void), but "info gcc" section 4 "C implementation-defined behavior"
fails to mention void main(void),
Quite so.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 2 '08 #26
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote:
....
>But C90 provides the same permission in section 4, where it permits
extensions (supporting "void main(void)" is a valid extension).
Exactly.

....
>I'm puzzled by your puzzlement. And I'm equally puzzled as to why you
would consider using "void main(void)" even with a compiler that
supports it. It gains you nothing over "int main(void)".
My point (with the silly $700B example) was that it might.
My implementation (particularly if it is, an in my example, an embedded
[non-hosted] environment) might give extra functionality (as an
extension) if I write void main(void).

As you say, extensions are permitted, and, most importantly, the
standard says nothing about what void main(void)[*] can or can't do.
[*] Hereinafter, let's abbreviate this as vmv, since I predict we will
be discussing this for years to come.

Oct 2 '08 #27
Richard Heathfield <rj*@see.sig.in validwrote:
s0****@gmail.co m said:
(the standard only guarantees that 'int' can
represent up to 32767), but it will never fail. Is this program
"incorrect" according to the standard?

It depends which Standard we're talking about. It's incorrect under C89,
but correct under C99.
What's different in C99 that makes you say that?
--
Larry Jones

Why can't I ever build character in a Miami condo or a casino somewhere?
-- Calvin
Oct 2 '08 #28
la************@ siemens.com said:
Richard Heathfield <rj*@see.sig.in validwrote:
>s0****@gmail.co m said:
(the standard only guarantees that 'int' can
represent up to 32767), but it will never fail. Is this program
"incorrect" according to the standard?

It depends which Standard we're talking about. It's incorrect under C89,
but correct under C99.

What's different in C99 that makes you say that?
C99 codifies // (and since we're on the subject, it also codifies falling
off the end of main).

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 2 '08 #29
James Kuyper <ja*********@ve rizon.netwrote:
>
C99 implementations are pretty rare, and therefore don't provide a good
sample size. I've heard that many popular C90 compilers permit void
int(void); do none of them document the fact that they permit it?
As far as I know, almost all of the implementations that "permit" it
only do so accidentially, not as a deliberate extension. That is, they
don't do anything to specifically allow it, but they don't bother to
forbid it, either, which is a perfectly acceptable course of action for
undefined behavior. If the resulting (undefined) behavior is innocuous,
as it usually is, it *appears* that the implementation supports it when
it really doesn't. That's one of the reasons that the "or in some other
implementation-defined manner" wording was added to C99: to emphasize
that appearing to work is not sufficient, the implementation must also
document it or it's just an accident, not intentional support.
--
Larry Jones

I sure wish I could get my hands on some REAL dynamite. -- Calvin
Oct 2 '08 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
8706
by: Michael B. Allen | last post by:
Coming from C and Java on *nix I'm a little out of my element messing around with CList and MSVC++ but I think my issues are largely syntactic. I have an ADT that I use called a 'varray' that can return a pointer to an arbirary sized element in an array given an index and it will allocate the memory to back it if necessary: struct varray *tests = varray_new(sizeof(struct test)); struct test *t = (struct test *)varray_get(tests, 50));
4
2096
by: kk | last post by:
Hi all, i didn't get output in the following code while compiling and executing with g++ version 3.2.3 it doesn't allocate memory to pointer varaible (x) in class B. and it gives correct output while executing in .Net and visual slick editor. why it didn't allocate memory? if anybody knows plz give reply. thanks in advance kk
25
9222
by: MC | last post by:
Hi I have the following piece of code. int *p; p = malloc(10); int *j; j = p; free(p); Is this legal ? will j still point to the same
5
2978
by: fatted | last post by:
I'm trying to write a function which splits a string (possibly multiple times) on a particular character and returns the strings which has been split. What I have below is kind of (oh dear!) printing the results I expect, which I guess means my dynamic memory allocation is a mess. Also, I was advised previously that I should really free memory in the same place I declare it, but I'm not sure how I would go about doing this in my code...
13
5724
by: Rob Corwin | last post by:
Hi, a c# app of mine that parses 30,000 xml files writes large amounts of data to file (> 2GB) using a streamwriter object (sw). everything works fine except that the memory used by the app grows linearly during execution and eventually crashes the computer. without going into too much detail of my code, i made the following observations: - if you comment out the sw.Write(x) statement (which is inside the loop that parses the xml...
3
3479
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based memory allocation? like complex<int> * c1,*c2,*c3; i still want to do something like c3 = c1+c2 ; rether than *c3 = *c1+*c2;
7
4697
by: toton | last post by:
Hi, I have a STL vector of of characters and the character class has a Boost array of points. The things are vector<Characterchars; and class Character{ private: array<Point,Npoints; }; Now are the memory layout is contiguous? i.e all the character resides side by side just like array, and all Points side by side insede the
94
4830
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring that I found inside of a string. Any ideas?
1
7984
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10877
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7988
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5810
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4633
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4239
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.