473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Sudden doubt

We have the following:

typedef struct {
char *p ;
} data_structure ;

void f(data_structure *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 1270
"K. Jennings" <kj*******@resurgence.netwrote:
typedef struct {
char *p ;
} data_structure ;

void f(data_structure *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*******@resurgence.netwrote:
Message ID: <pa*********************@resurgence.net>
We have the following:

typedef struct {
char *p ;
} data_structure ;

void f(data_structure *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*******@resurgence.net>
wrote:
> We have the following:

typedef struct {
char *p ;
} data_structure ;

void f(data_structure *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_structure *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_structure *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
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...
2
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...
9
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...
3
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...
1
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...
2
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
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
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
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...
1
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
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...

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.