473,786 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Basic Use of Malloc

Hi. I am an amature c hobbyist and I wish to begin using malloc as I
have been told that running executables from executables is a less
than elegant way to manage memory use. I need basic examples so me and
my compiler can get familliar with malloc. Please give examples that
do not contain nested assignments and the like as I will get lost
quite easily. As an example of my rudimentary style take a look at how
I create an array and assign a pointer to it:

unsigned long arraysize = 50;
char chArray[arraysize];
char *ptrChArray;
ptrChArray = chArray;

I need a few examples of malloc's proper use that are in the same
rudimentary style. Thank you so much for your time. kentinjapan
Nov 13 '05
21 34395
On Wed, 10 Sep 2003 13:35:01 GMT, Joe Wright
<jo********@ear thlink.net> wrote in comp.lang.c:
Martijn wrote:

Jeff wrote:
buffer = malloc(30);
strcpy(buffer, string1);
strcat(buffer, "World.");


Not a point of critisism to the example, but something to watch out for: be
aware that this allocates space for 30 _bytes_, so don't go and try to store
30 integers in that memory. If that's what you want, try this:

int *buffer;
buffer = malloc(30 * sizeof(int));

Good luck,

--
Martijn
http://www.sereneconcepts.nl


The clc canon eschews magic numbers and unnecessary type names. Regard..


....and the clc cannon is always ready to fire on those who violate the
canon...

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #21
pete <pf*****@mindsp ring.com> wrote in message news:<3F******* ****@mindspring .com>...
Richard Heathfield wrote:
The book makes no attempt to teach the C language,
but rather the author's personal,
flawed understanding of that language.


He's one of those, who has deduced what C is,
from his experience with his compiler.

"Note that it's "might" screw things up. In fact, even the void main()
thing is an iffy. I've gotten complaints from various
self-proclaimed C Lords, some of which are even USENET C Jerks,
who proclaim the evils of void main() and
that I'm an ass for every propogating such nonsense.

Perhaps I am an ass. But not one of these C poobahs has
yet to provide a specific instance of void main() or the lack of a
return statement actually screwing anything up.
No, they just speak in generalities, which is typical
(most likely because their knowledge of C is weak
and their self-importance overrated)."


I've nver known void main() to screw things up either, but then for
the tiny effort of int main() and a return, why bother stocking with
void main?

As i got used to the int main() advice here (and elsewhere) i went
along with it. It may not seem to make a difference with the popular
computer OSes, but C is used all over the place, why rely on the
platform to sort it out?

When clc regulars like Richard give advice based on the standard it
comes at no personal cost, an often with very little rework of your
existing code needed to get it just that bit more right. I don't see
a reason to complain.

On malloc i did all that uncessary casting stuff too, once.

Thing is, you could buy that book, get by on your own system and then
one day encounter a subtle bug that you just don't understand, and you
don't get it because you'd followed some bad practice and don;t know
better. Pretty frustrating outcome i hope you'll agree.

( i enjoyed C Programming: A Modern Approach, King, as a learning
tool, hoping that's a good example! )
Nov 13 '05 #22

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

Similar topics

18
2130
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:
9
1284
by: shan_rish | last post by:
Hi CLCers, I coded a function to allocate memory and i am passing a pointer to the function. The code is compiling but throws error and closes while executing. The program is as below: #include<stdio.h> #include<stdlib.h> int main() { int *p; void mem_fun(int *i);
21
470
by: ramu | last post by:
Hi, Will the memory allocated by malloc and realloc be contiguous? regards
25
2266
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 {
58
4690
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is the overhead of automatic variable sized arrays, I suspect it is smaller than that of malloc), although I'm not too worried about it. I was thinking that, with C99's variable length arrays, malloc shouldn't be needed most of the time. But I'm...
1
1888
by: Kevin | last post by:
Hi all, I clearly have an issue with some pointers, structures, and memory allocation. Its probably pritty basic, but I'm a little stuck. Any help would be greatly appreciated. I'd like to instantiate an arbitrary number of arrays of arbitrary size in function_a, copy the pointers, store the data, and free any unused memory. My basic structure is as follows:
71
19137
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a problem? I cannot see why using malloc instead of new does not give the same result.
173
8186
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc. OTOH, if you're allocating a gigabyte for a large array, this might fail, so you should definitely check for a NULL return.
26
3849
by: Muzammil | last post by:
whats the beauty of "malloc" over "new" why its helpful for programmer.for its own memory area.??
0
9647
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
9496
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
10363
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
10164
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
10110
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
8989
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...
0
6745
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
5397
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
4066
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.