473,787 Members | 2,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sudden doubt

We have the following:

typedef struct {
char *p ;
} data_structure ;

void f(data_structur e *data)
{
char *r = malloc(16) ;

data->p = r ;

return ;
}

Assuming that malloc returns a non-NULL pointer, data->p will
point to a valid memory region for as long as data is not released. Is
this correct? I know that when f() returns the value of r becomes
undefined, but I assume that whatever it was pointing to will be left
untouched.

Jul 18 '07 #1
4 1284
"K. Jennings" <kj*******@resu rgence.netwrote :
typedef struct {
char *p ;
} data_structure ;

void f(data_structur e *data)
{
char *r = malloc(16) ;

data->p = r ;

return ;
}

Assuming that malloc returns a non-NULL pointer, data->p will
point to a valid memory region for as long as data is not released. Is
this correct? I know that when f() returns the value of r becomes
undefined, but I assume that whatever it was pointing to will be left
untouched.
Entirely correct. Do remember to free() data->p (which is now your only
pointer to that allocated block) later on.

Richard
Jul 18 '07 #2
On Wednesday 18 Jul 2007 5:57 pm, in comp.lang.c, K. Jennings
<kj*******@resu rgence.netwrote :
Message ID: <pa************ *********@resur gence.net>
We have the following:

typedef struct {
char *p ;
} data_structure ;

void f(data_structur e *data)
Hopefully data is initialised to point to a valid data_structure
object.
{
char *r = malloc(16) ;

data->p = r ;

return ;
}

Assuming that malloc returns a non-NULL pointer, data->p will
point to a valid memory region for as long as data is not released.
Is this correct?
Yes.
I know that when f() returns the value of r
becomes undefined, but I assume that whatever it was pointing to
will be left untouched.
Yes, provided you've taken care to save the value previously in r.
Jul 18 '07 #3
On 18 Jul 2007 12:27:05 GMT, "K. Jennings" <kj*******@resu rgence.net>
wrote:
> We have the following:

typedef struct {
char *p ;
} data_structure ;

void f(data_structur e *data)
{
char *r = malloc(16) ;

data->p = r ;

return ;
}

Assuming that malloc returns a non-NULL pointer, data->p will
point to a valid memory region for as long as data is not released. Is
this correct? I know that when f() returns the value of r becomes
undefined, but I assume that whatever it was pointing to will be left
Others have answered your intended question. This is just a
terminology nit. When f returns, r ceases to exist. Consequently,
the value that was in r no longer exists **in r**. However, since you
had the foresight to copy the value to data->p, that value is still
available to the function that called f or to any other function that
can find the struct.

That value is the address of allocated memory which will remain
allocated until you explicitly free it. If the address in r had
pointed to another automatic object in f, that object would also cease
to exist when f returned and, by definition, the value in data->p
would then become indeterminate.
>untouched.

Remove del for email
Jul 19 '07 #4
"K. Jennings" wrote:
>
We have the following:

typedef struct {
char *p ;
} data_structure ;

void f(data_structur e *data)
{
char *r = malloc(16) ;

data->p = r ;

return ;
}

Assuming that malloc returns a non-NULL pointer, data->p will
point to a valid memory region for as long as data is not released. Is
this correct? I know that when f() returns the value of r becomes
undefined, but I assume that whatever it was pointing to will be left
untouched.
I believe you are getting confused because of previous posts which
included code along the lines of:

void f(data_structur e *data)
{
char r[16];

data->p = r;

return;
}

In this case, you are left with data->p pointing to something that
no longer exists. (That is, r goes away upon return, yet data->p
still points to r.)

However, in your case, r is simply a pointer to the address returned
by malloc(). You are storing the _value_ of r into data->p, not its
address. The address returned by malloc() (assuming it succeeded)
is still valid until you free() it. The fact that r goes away is
irrelevent, as r was simply used to hold a value.

Consider that the body of your function could be written:

data->p = malloc(16);

The results are identical, and there is no "r" do disappear. (In
fact, a modern optimizing compiler may treat your code as if r did
not exist in the first place.)

--
+-------------------------+--------------------+-----------------------+
| 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>

Jul 19 '07 #5

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

Similar topics

2
1434
by: Tony Short | last post by:
Has anyone seen inexplicable sudden rises in the number of sessions open? About 2-3 times a day, the session count (on perfmon) jumps by about 50, and this may happen repeatedly over a short space of time - taking the total session count to 1000+. What could be causing this? Could it be some sort of attack? Could it be some sort of loop? (It's a new phenomenon for us.) Thanks
2
1644
by: Frances Del Rio | last post by:
please, can you go to http://www.francesdelrio.com/dhtml/ when you click the name of the character the font color for all the lines of that character should change.. this is a simple DHTML memo... but I find that none of the JS or DHTML code is working at all on page... if I download page and view it locally the JS & DHTML code work fine, I don't understand this.. I'm very anxious b/c I just submitted this page as a demo for a...
9
3180
by: Patrick | last post by:
I have an ASP.NET page that searches for someone in the corporate Active Directory. It had been working fine until recently when I changed from Basic Authentication on IIS6 back to Integrated Windows authentication. The error occurs on the FindAll method. The exceptions are as follows. anyway of getting the code working with Integrated Windows authentication (too annoying for user to enter user-name/password). Note I do need to use...
3
1392
by: DFS | last post by:
Largish system, was 13mb (after de/compile and compact/repair) It's mostly forms, queries and code. About 40 local tables (with tiny numbers of records, like 1 to 10) Recently added a few more TEMP* local tables (no records) and a couple of screens and a few hundred lines of code Now it's 44mb after de/compiled and compact/repair!
1
2047
by: Henrik | last post by:
I have a problem with processor load in the enviroment where I'm running the firts installation of my new application. My program is running on a Pentium 4 3.0 GHz, wtih 1 GB DDR SDRAM and 80 GB harddrive. The operating system is Win XP professional and I have no other programs installed on this box. My program is communicating with other devices using two network cards. The communication is time critical in a sence that the messages...
2
1263
by: skishorev | last post by:
Hi, In sudden power off, How to backup means in which format(files, streams) & when to backup in C++. Thanks, SAI
11
1666
by: skishorev | last post by:
Hi, In sudden power off, How to backup means in which format(files, streams) & when to backup in C. Thanks, SAI
5
2624
by: PW | last post by:
Hi, All of a sudden, some clients of our Access 2003 software are getting Asian lettering in some text boxes, combo boxes, etc.... We have done nothing to it. Any ideas?
0
1429
by: =?Utf-8?B?TWF0dCBDYWxob29u?= | last post by:
Hi there, Have had Team Foundation Server working for months now and all of a sudden after a restart of IIS it has been failing with errors: The identity of application pool 'TFS AppPool' is invalid, so the World Wide Web Publishing Service can not create a worker process to serve the application pool. Therefore, the application pool has been disabled. For more information, see Help and Support Center at
1
2570
by: rajayan | last post by:
Jochen Kalmbach wrote: Thank you for your reply. Yes we do. Could you please elaborate. Are you referring to the CString::AllocBuffer related hangs or sudden crashes --
0
9655
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
9498
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...
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
8993
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
7517
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
6749
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();...
1
4069
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
3
2894
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.