473,738 Members | 5,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Design question: using malloc?

Hello,

I'm quite new to C++ and have some base C knowledge. So I know how to
solve a given problem, by I sometimes tend to fall back to C. That is
what I want to avoid in my current project. Therefore I have the
following question:

I maintain a list of objects that are created. There is some information
that is logically related to each instance. I encapulate this in a
struct like this:

struct myListEntry {
myObject entry;
int relatedData;
}

What is the ideal way to implement adding another object to the vector?
(myVector has type vector<myListEn try> in this example)
Should I use malloc to reserve memory for my struct?
Should I do the following?
...
myListEntry newEntry;
newEntry.entry = something;
newEntry.relate dData = something_else;
myVector.push_b ack(newEntry);
...
Or is there another preferred way?

Regards,
Christof Krueger

Jul 22 '05
10 2277
> > Another thing that 'C' programmers tend to think is that their C code
is
faster than C++. Not only is this, for the most part, a bogus claim, it also leads to coding improperly and incorrectly when you resort to coding in the 'C'-style.

I'me quite new to C and C++ programming both but I've seen more C code
than C++ code _before_ I started to actually program it myself. That is
why I still mix things up.
C code look intuitively faster, because its more complicated (not that
high level) and one think, that c++ programs have more overhead to
handle. But of course you can write a lot of *really* slow code in
low-level C :)
If you know some must-see links covering performance comparison between
C/C++ programs, please let me know.


You might find this one an interesting read:
http://www.objectmentor.com/resource...tillUsingC.pdf

My advise is don't be overly concerned with performance. Though it is a bit
easier to write inefficient code in C++ than in C, well written C++ code
can be just as fast as well written C code. Often there are more important
concerns than performance, like correctness, maintainability and speed of
development. C++ offers a better compromise between efficiency and elegancy
than C code. My exprience is that only a small part of the code is really
relevant for the performance of an application. On modern hardware most
applications will run fast enough without using dirty tricks. Also many
applications are are more limited by I/O performance than CPU performance,
coding tricks to save CPU cycles won't help in this case.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl


Jul 22 '05 #11

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

Similar topics

46
4148
by: sbayeta | last post by:
Hi, I'd like to know who is responsible of memory recycling and defragmentation in a C/C++ program, assuming all the memory allocation/deallocation is done using malloc/free or new/delete. Thanks
36
7783
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption about the most restrictive type on my machine? Thanks.
36
2275
by: Martin Andert | last post by:
Hello, I have a question regarding malloc and free. Here my code sample: int main() { /* allocating dynamic memory for array */ int* array = (int*) malloc(5 * sizeof(int)); /* ... program code ... */
18
2128
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for last_values as well as assign the value of a pointer to the assigned space. Which I think I'm doing by using:
15
1851
by: sethukr | last post by:
Hi everybody, While running the following program in GCC, i'm very much screwed. main() { char *ptr1; char arr; int i; char *ptr2;
25
2258
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking. --- #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct {
14
1934
by: Jef Driesen | last post by:
I'm writing a library (to communicate with a number of devices over a serial port) and have some questions about the design. I have now a header and source file like this: /* device.h */ typedef struct device device; int device_open (device **dev, const char *name); int device_close (device *dev); int device_read (device *dev, void *data, unsigned int size);
10
1903
by: somenath | last post by:
Hi All, I have one question regarding return value cast of malloc. I learned that we should not cast the return value of malloc because it is bug hider. But my question is as mentioned bellow . Lets say I have not included stdlib.h in my program still I am using malloc so compiler will throw warring because with out prototype
27
2774
by: matt | last post by:
Hello group, I'm trying to become familiar with the information hiding design rules, and I have a lot (3) of questions for all you experts. AFAIK, a generic module has 2 files: ================ module.h ================ #ifndef __MODULE_HDR_INCLUDED__
0
9476
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
9335
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...
0
8210
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
6751
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
6053
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
4570
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
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
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.