Possible memory issue

R

Richard Edwards

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
 
G

gladiator

It will cause memory leak.
Not a big leak.

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

Christopher Benson-Manica

Richard Edwards said:
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.
 
L

laurent.constantin

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
 
S

SM Ryan

# 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.
 
E

Eric Sosman

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' 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.)
 
A

Alexei A. Frounze

Richard Edwards said:
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
 
D

Default User

gladiator said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top