473,395 Members | 1,526 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,395 software developers and data experts.

Possible memory issue

If I have the following declaration:

static char *list[1] = {0};

and I execute this code:

list[0] = malloc(10);

am I asking for problems?

Here's what I'm wondering. "list" is a 1 element array of char
pointers. The address that is stored in the 0th element of "list" is a
hard-coded 0. Now, I know that when a pointer is initialized to a
constant value (in practice, this is usually a string), the value that
the pointer points to should not be changed. In this instance, the
array pointer is pointing to 0. The malloc will change the value that
the array pointer points to to the allocated address. If the array
pointer was pointing to a _constant_ 0 that should not be changed, I
could see how this could cause problems.

In reality, does it?

Thanks,
Richard

Nov 15 '05 #1
8 1446
It will cause memory leak.
Not a big leak.

it is not a pointer as a string.
It is pointer to pointer.

Nov 15 '05 #2
Hello,

I'm not sure to really understand your question. Anyway, here is an
anwser.

In following sample, listA can be changed because static does not mean
constant. However, listB cannot be changed because its values are
"const".

== start
#include <stdlib.h>
#include <stdio.h>

static char *listA[1] = {0};
static char * const listB[1] = {0};

int main(void) {

/* value can be changed */
printf("listA[0] = %p\n", listA[0]);
listA[0] = malloc(10);
printf("listA[0] = %p\n", listA[0]);

/* forbidden */
//listB[0] = malloc(10);
printf("listB[0] = %p\n", listB[0]);

return(0);
}
==END

Nov 15 '05 #3
Richard Edwards <re******@salepoint.com> wrote:
static char *list[1] = {0}; list[0] = malloc(10); am I asking for problems?
No.
Now, I know that when a pointer is initialized to a
constant value (in practice, this is usually a string), the value that
the pointer points to should not be changed.


Only if the pointer is declared as "pointer to const x". You didn't,
so what you're doing is perfectly acceptable.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #4
"Richard Edwards" <re******@salepoint.com> wrote:
# If I have the following declaration:
#
# static char *list[1] = {0};
#
# and I execute this code:
#
# list[0] = malloc(10);
#
# am I asking for problems?

Unless you declaring list to be const, you can change what
its elements are.
static char *list[1] = {0};
list[0] = "abc";
list[0] = "def";
list[0] = 0;
list[0] = "ghi";
changes the element of list.

This is distinct from changing what the list element pointers point to.
static char *list[1] = {0};
list[0] = "abc";
Then
list[0][1] = 'B';
is not changing the list element, but the string the list element
points to. If that string doesn't like being changed (usual for
string constants), then you have problems.

static char *list[1] = {0};
list[0] = malloc(4); strcpy(list[0],"abc");
Then
list[0][1] = 'B';
In this case, I'm modifying a mallocked block within its bounds
so I know this will not a problem.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
You hate people.
But I love gatherings. Isn't it ironic.
Nov 15 '05 #5


Richard Edwards wrote On 09/23/05 09:50,:
If I have the following declaration:

static char *list[1] = {0};

and I execute this code:

list[0] = malloc(10);

am I asking for problems?

Here's what I'm wondering. "list" is a 1 element array of char
pointers. The address that is stored in the 0th element of "list" is a
hard-coded 0. Now, I know that when a pointer is initialized to a
constant value (in practice, this is usually a string), the value that
the pointer points to should not be changed. In this instance, the
array pointer is pointing to 0.
Here, I think, is the root of your confusion. There
are two pointers wandering around in this code snippet:

- `list[0]' is a pointer to `char', a `char*'. You
initialize this pointer with a value of zero, which
is not the same thing as "pointing to zero."

- `list' itself is an array, and in nearly all cases
"speaking the name" of an array yields a pointer to
the array's first element. That is why you can
write `list[0]': the `list' part turns into a pointer
to the first array element, which is what you need
for array indexing. (Remember that `x[i]' is defined
as `*(x + i)' -- you need a pointer to get started.)
The malloc will change the value that
the array pointer points to to the allocated address. If the array
pointer was pointing to a _constant_ 0 that should not be changed, I
could see how this could cause problems.


It will not. The pointer's value is one thing, the value
of the thing pointed at (if any) is another. Your telephone
number is first in the list of emergency contacts (it is stored
in `emergency_contact_numbers[0]'), but when you go on vacation
somebody else's number goes into that spot. The pointer now
points to somebody else's telephone, but your telephone is
unchanged.

The only possible source of trouble is if the list of
emergency contacts was the *only* record of your telephone
number. In C, an "unlisted number" is usually called a
"memory leak," because once the program has lost track of
an object it can no longer do anything with it -- not even
throw it away. (And here the analogy begins to show some
cracks because you could still place outgoing calls from
your "unlisted number," but objects in C are "passive" and
can't do such things until tickled by the program -- but
if the program can't find them, it can't tickle them.)

--
Er*********@sun.com

Nov 15 '05 #6
"Richard Edwards" <re******@salepoint.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
If I have the following declaration:

static char *list[1] = {0};

and I execute this code:

list[0] = malloc(10);

am I asking for problems?
No.
Here's what I'm wondering. "list" is a 1 element array of char
pointers. The address that is stored in the 0th element of "list" is a
hard-coded 0. .... In this instance, the
array pointer is pointing to 0.


No, the pointer itself is 0 (before assigning it malloc's return value).
As you said yourself (correctly) list is an array of pointers to char, array
of 1 pointer to char. list[0] is that pointer. A pointer is not the same
thing to which it poits.

Alex
Nov 15 '05 #7
Thanks for all of the feedback. Appreciate the help.

Thanks,
Richard

Nov 15 '05 #8
gladiator wrote:
It will cause memory leak.
Not a big leak.

it is not a pointer as a string.
It is pointer to pointer.


1. Please read my sig.

2. What memory leak do you think you see?

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 15 '05 #9

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

Similar topics

9
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but...
9
by: tony collier | last post by:
i have created a 7-dimensional array which has 19.5 million elements. when i try to create 8-dimensional array i get out of bounds system exception. does anyone know if this limitation is due...
1
by: Brian Mahloch | last post by:
Hello, I have an ASP.Net application that seems to hiccup every so often for 10 - 15 minutes. The situation is that the controls on the web form (loading dynamically from SQL Server) will come...
0
by: John | last post by:
We are using Authorization Manager in an ASP.NET application. We are using Microsoft.Interop.Security.AzRoles. We appear to have a memory leak when calling the method...
6
by: Richard Bell | last post by:
I'm having difficulty with memory loss on an application that automates IE6 under XP (pro, fully up to date) using VB.Net. Every 30 minutes (triggered by the XP scheduler) the application visits a...
7
by: Simon Verona | last post by:
I have a problem in my application which I believe is due to open handles.. . The symptom that users report is that after they have been using the application for a while, it will randomly just...
3
by: chris.mcinnes | last post by:
G'day, Got a bizzare issue with PHP 5.0.2/Apache constantly generating variations of the following error: "Fatal error: Possible integer overflow in memory allocation" At first I traced it...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
3
by: san | last post by:
we cannot stop the application from increasingly use memory. The CRM Worker process will continually consume memory but not release it back to the system. Please research into how to make the...
7
by: =?Utf-8?B?Tmlrb2xheSBFdnNlZXY=?= | last post by:
Hi! I know this topic has been discussed a long way, but I haven't found any apparent solution (maybe I shouldn't be looking for a one :)) I have a very simple application with one page and with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...

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.