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 1646
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******@hotma il.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
--
"Considerat ion 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("asprint f");
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...@ve rizon.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...@ve rizon.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 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. "Decimal Number" sometimes serves to distinguish Base 10
numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal
Number" serves to differentiate a number from an integer. For the rest of this
post I shall only use either...
|
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 method was a piece of C code
which turned out to be incorrect and incomplete but by modifieing it would
still be usuable.
The first method was this piece of text:
|
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 are different which would make one more convient in a
given situation, but other that that does it make any signifficant
difference.
|
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 text field. I want to know which Data
type I need to use so that it doesnt waste memory.
thanks in advance,
rAinDeEr
|
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 cost.
STL vector is suitable for removing form tail? or it is as costly as
removing from middle?
any other std container to serve this purpose? (note , I dont need
linked list implementation of any container, as I want random access)
| |
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 second two statements
|
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):
//------------------------------------------------------------------
template<typename Data_t>
class SmartPointer
{
Data_t* data;
void(*deleterFunc)(Data_t*);
|
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 <iostream>
#include <cstddef>
using namespace std;
class TraceHeap {
int i;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |