Since we did not want to make an error in counting of bytes, we used
the code
char *p;
...
p = malloc(strlen("hello")+1);
strcpy(p,"hello");
instead of the intended
char *p;
...
p = malloc(6);
strcpy(p,"hello");
i want to compare the memory requirements of each version: which one
requires less memory? 20 1599
khan wrote:
Since we did not want to make an error in counting of bytes, we used
the code
char *p;
...
p = malloc(strlen("hello")+1);
strcpy(p,"hello");
instead of the intended
char *p;
...
p = malloc(6);
strcpy(p,"hello");
i want to compare the memory requirements of each version: which one
requires less memory?
Did you compare strlen("hello")+1 to 6?
--
Ian Collins.
Yes..
Both evaluates to 6 ...but does these two programs internally use same
amount of memory?
khan wrote:
Yes..
Both evaluates to 6 ...but does these two programs internally use same
amount of memory?
Please retain enough context for your post to make sense.
Of course they do, they a both malloc(6).
--
Ian Collins.
In article <5o************@mid.individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>Both evaluates to 6 ...but does these two programs internally use same amount of memory?
>Of course they do, they a both malloc(6).
But the one with strlen("hello") might have an extra copy of the
string literal, though one would hope not.
Using sizeof("hello") would be more natural in this case.
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
khan wrote:
Since we did not want to make an error in counting of bytes, we used
the code
char *p;
...
p = malloc(strlen("hello")+1);
strcpy(p,"hello");
instead of the intended
char *p;
...
p = malloc(6);
strcpy(p,"hello");
i want to compare the memory requirements of each version: which one
requires less memory?
A good compiler should consume equal memory for both versions.
On Oct 30, 10:55 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
But the one with strlen("hello") might have an extra copy of the
string literal, though one would hope not.
String literals have no scope and no duplicates exist.
Use sizeof "hello", and please check the return value before you do
anything
char *p = malloc(sizeof "hello");
if(p) strcpy(p, "hello");
free(p);
You could also use asprintf or the non-standard strdup.
char *p;
if(asprintf(&p, "hello") == -1) perror("asprintf");
else free(p);
char *p = strdup("hello");
free(p);
If you simply want a mutable string, you can use this c99 feature
char *p = (char[]){ "hello" };
or enable some compiler extension that allows string literals to be
modified (not recommended) vi****************@gmail.com wrote:
On Oct 30, 10:55 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
>But the one with strlen("hello") might have an extra copy of the string literal, though one would hope not.
String literals have no scope and no duplicates exist.
The first part of that statement is true, though it's relevance is
unclear. The second part is desirable, but not guaranteed by the
standard. 6.4.5p6 says that "It is unspecified whether these arrays are
distinct": if you use the same string literal twice, it might or might
not result in the creation of two distinct arrays of char being created.
On Oct 30, 1:48 pm, James Kuyper <jameskuy...@verizon.netwrote:
The first part of that statement is true, though it's relevance is
unclear. The second part is desirable, but not guaranteed by the
standard. 6.4.5p6 says that "It is unspecified whether these arrays are
distinct": if you use the same string literal twice, it might or might
not result in the creation of two distinct arrays of char being created.
That means "hello" != "hello" might evaluate to true, which is..
irritating.
These std commies sometimes leave too much freedom in the
implementation. vi****************@gmail.com wrote:
On Oct 30, 1:48 pm, James Kuyper <jameskuy...@verizon.netwrote:
>The first part of that statement is true, though it's relevance is unclear. The second part is desirable, but not guaranteed by the standard. 6.4.5p6 says that "It is unspecified whether these arrays are distinct": if you use the same string literal twice, it might or might not result in the creation of two distinct arrays of char being created.
That means "hello" != "hello" might evaluate to true, which is..
irritating.
But rarely relevant; if you want to compare strings, compare them by
content, or implement your own interning machinery.
These std commies sometimes leave too much freedom in the implementation.
I find your choice of adjectives troubling, especially since in this
instance it's nowhere near "too much freedom".
--
Chris "duplicate removal not universally available" Dollin
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
santosh wrote:
>
khan wrote:
Since we did not want to make an error in counting of bytes, we used
the code
char *p;
...
p = malloc(strlen("hello")+1);
strcpy(p,"hello");
instead of the intended
char *p;
...
p = malloc(6);
strcpy(p,"hello");
i want to compare the memory requirements of each version: which one
requires less memory?
The result is always the same -- malloc() will be called with a
parameter of 6.
A good compiler should consume equal memory for both versions.
However, the first example may generate more code, as it calls
strlen() and adds one, rather than simply passing 6 to malloc().
Of course, a "good compiler" may see that you're passing a literal
to strlen() and skip the actual call, knowing that the result is
always 5.
Without optimization enabled, my compiler calls strlen(). With
the default optimization, it calls strlen(). With maximum
optimization, it hard-codes the value. With just "intrinsic
functions" optimizing enabled, it does something weird -- it
hard-codes the 5, and adds 1:
return strlen("hello") + 1; /* line 5 */
; Line 5
mov eax, 1
add eax, 5
All of these scenarios are, of course, valid.
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
vipvipvipvipvip...@gmail.com wrote:
On Oct 30, 1:48 pm, James Kuyper <jameskuy...@verizon.netwrote:
The first part of that statement is true, though it's relevance is
unclear. The second part is desirable, but not guaranteed by the
standard. 6.4.5p6 says that "It is unspecified whether these arrays are
distinct": if you use the same string literal twice, it might or might
not result in the creation of two distinct arrays of char being created.
That means "hello" != "hello" might evaluate to true, which is..
irritating.
These std commies sometimes leave too much freedom in the
implementation.
I don't know what "commies" has to do with it. The phrase "too much
freedom" is not the stereotype that normally goes with that insult.
Would you care to explain?
I gather that this freedom was necessary, because at the time of the
original standard, there were a number of compilers which did not
bother checking whether the same string literal was used multiple
times in a program. Those implementors decided that they didn't want
to "waste" time performing such a search. Before you judge that
decision too harshly, keep in mind that computers were typically a lot
less powerful back then, and compilers were often excruciatingly
slow.
I don't know the exact politics involved, but I suspect that it would
have been harder to get the original standard approved if it had been
written in a way that would require such implementations to add string
literal merging. Since that time, there's never been a sufficiently
strong reason for declaring this feature obsolescent. Keep in mind
that nowadays memory is dirt cheap; so bothering to merge string
literals might still not be worth while, at least on some platforms. vi****************@gmail.com writes:
<snip>
If you simply want a mutable string, you can use this c99 feature
char *p = (char[]){ "hello" };
or enable some compiler extension that allows string literals to be
modified (not recommended)
or use the universally accepted:
char p[] = "hello";
No need for any C99isms or compiler extensions.
--
Ben.
khan:
Since we did not want to make an error in counting of bytes, we used
the code
char *p;
...
p = malloc(strlen("hello")+1);
strcpy(p,"hello");
instead of the intended
char *p;
...
p = malloc(6);
strcpy(p,"hello");
i want to compare the memory requirements of each version: which one
requires less memory?
Presumably the former version would take up more memory because of the
machine code required to invoke strlen.
Martin
On Oct 30, 1:42 pm, khan <d.nayeem.k...@gmail.comwrote:
Since we did not want to make an error in counting of bytes, we used
the code
char *p;
...
p = malloc(strlen("hello")+1);
strcpy(p,"hello");
instead of the intended
char *p;
...
p = malloc(6);
strcpy(p,"hello");
i want to compare the memory requirements of each version: which one
requires less memory?
It is very obvious that invoking of strlen in the first version
might consume a bit more memory compared to the second version.
Also, it depends on the level of optimisation that you use
and the compiler.
Karthik Balaguru
khan wrote:
Since we did not want to make an error in counting of bytes, we used
the code
char *p;
...
p = malloc(strlen("hello")+1);
strcpy(p,"hello");
instead of the intended
char *p;
...
p = malloc(6);
strcpy(p,"hello");
i want to compare the memory requirements of each version: which one
requires less memory?
The answer is given by measurement, why not use
char p[] = "hello";
instead?
--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Tor Rustad wrote:
khan wrote:
>Since we did not want to make an error in counting of bytes, we used the code
char *p; ... p = malloc(strlen("hello")+1); strcpy(p,"hello");
instead of the intended
char *p; ... p = malloc(6); strcpy(p,"hello");
i want to compare the memory requirements of each version: which one requires less memory?
The answer is given by measurement,
Indeed (and is implementation-specific).
why not use
char p[] = "hello";
instead?
Different semantics? The original code makes a mallocated object
that will persist until freed; your proposal makes a local array
that will evaporate when the function exits. Only the OP knows
if that's a good replacement ...
--
Far-Fetched Hedgehog
On Oct 31, 9:50 am, Chris Dollin <e...@electrichedgehog.netwrote:
Different semantics? The original code makes a mallocated object
that will persist until freed; your proposal makes a local array
that will evaporate when the function exits. Only the OP knows
if that's a good replacement ...
You mean until it runs out of scope.
int foo(void) {
{ char bar[100]; strcpy(bar, "hello world");}
int bar = 3;
printf("%d \n", bar);
return 0;
} vi****************@gmail.com wrote:
On Oct 31, 9:50 am, Chris Dollin <e...@electrichedgehog.netwrote:
>Different semantics? The original code makes a mallocated object that will persist until freed; your proposal makes a local array that will evaporate when the function exits. Only the OP knows if that's a good replacement ...
You mean until it runs out of scope.
Good catch. Yes.
--
Chris "messed up the extent of the scope" Dollin
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
Chris Dollin wrote:
Tor Rustad wrote:
[...]
>The answer is given by measurement,
Indeed (and is implementation-specific).
Yup
>why not use
char p[] = "hello";
instead?
Different semantics? The original code makes a mallocated object
that will persist until freed; your proposal makes a local array
that will evaporate when the function exits.
The point is, this should be better than a memory-leak (or a free call).
I prefer using malloc() for allocating variable-size objects, not string
literals.
--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
"Hello everybody out there using minix - I'm doing a (free) operating
system (just a hobby, won't be big and professional like gnu) for
386(486) AT clones" -Linus 1991
On Tue, 30 Oct 2007 08:42:30 -0000, khan <d.***********@gmail.com>
wrote:
>Since we did not want to make an error in counting of bytes, we used the code
char *p;
...
p = malloc(strlen("hello")+1);
strcpy(p,"hello");
instead of the intended
char *p;
...
p = malloc(6);
strcpy(p,"hello");
i want to compare the memory requirements of each version: which one requires less memory?
Does it matter that one of the programs includes the function strlen
and the other doesn't?
Remove del for email This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Bentley |
last post by:
John Bentley:
INTRO
The phrase "decimal number" within a programming context is ambiguous. It could
refer to the decimal datatype or the related but separate concept of a generic
decimal number....
|
by: Skybuck Flying |
last post by:
Hi,
I needed a method to determine if a point was on a line segment in 2D. So I
googled for some help and so far I have evaluated two methods.
The first method was only a formula, the second...
|
by: **Developer** |
last post by:
If I need to allocate memory, maybe because I'm going to:
Marshal.StructureToPtr
Does it matter which of the following I use?
Marshal.AllocCoTaskMem
Marshal.AllocHGlobal
I know the arguments...
|
by: rAinDeEr |
last post by:
Hi,
I have a web application with a table to store terms and
conditions of a Company.
This may some times run into many pages and some times it may be just a
few sentences. It is a character...
|
by: toton |
last post by:
Hi,
I want a circular buffer or queue like container (queue with array
implementation).
Moreover I want random access over the elements. And addition at tail
and remove from head need to be low...
|
by: ragi |
last post by:
Hi All,
In terms of efficiency, whether normal function call or using a
function pointer.
thxs in advance,
ragi
|
by: Martin T. |
last post by:
Hi all!
char* p = new char; // POD!
....
delete p; // std compliant
delete p; // will work on VC8
free(p); // will also work on VC8
I am interested on which platforms/compilers the...
|
by: Juha Nieminen |
last post by:
I asked a long time ago in this group how to make a smart pointer
which works with incomplete types. I got this answer (only relevant
parts included):
...
|
by: asm23 |
last post by:
Hi, everyone, I'm studying the <<Thinking in C++>volume Two. In
Chapter One, the example code : Auto_ptr.cpp
//-------------------------------------------------------
#include <memory>
#include...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
| |