473,804 Members | 3,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

which one requires less memory?

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?

Oct 30 '07 #1
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.
Oct 30 '07 #2
Yes..
Both evaluates to 6 ...but does these two programs internally use same
amount of memory?

Oct 30 '07 #3
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.
Oct 30 '07 #4
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.
Oct 30 '07 #5
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.

Oct 30 '07 #6
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)

Oct 30 '07 #7
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.
Oct 30 '07 #8
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.

Oct 30 '07 #9
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

Oct 30 '07 #10

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

Similar topics

17
6157
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...
65
12634
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:
4
7634
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.
5
3410
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
7
20015
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)
10
1580
by: ragi | last post by:
Hi All, In terms of efficiency, whether normal function call or using a function pointer. thxs in advance, ragi
23
1961
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
50
4522
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*);
15
1885
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;
0
9706
marktang
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...
0
10583
Oralloy
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...
0
10082
tracyyun
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...
0
9160
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
7622
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
6854
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();...
0
5525
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...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
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

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.