coding dynamic allocation question

B

Bill Cunningham

I guess these two statements are not the same. Why not? I guess I
haven't learned this I haven't used it much.

int *p;
p=malloc(sizeof (int)); //seems to be in error
p=malloc(sizeof (int *)); //is the correct coding

What exactly is the difference?

Bill
 
M

Malcolm McLean

I guess these two statements are not the same. Why not? I guess I
haven't learned this I haven't used it much.

int *p;

p=malloc(sizeof (int)); //seems to be in error
p=malloc(sizeof (int *)); //is the correct coding

What exactly is the difference?
The last one is almost certainly wrong.

malloc() creates a buffer, a region of memory where you can put things. So
if you want ten ints, p=malloc(10*sizeof(int)); If you want ten pointers to
ints, p=malloc(10*sizeof(int*));
But since p is an int *, it points to a buffer of ints, so presumably we
want ints in our buffer.

Usually you call malloc when the size of the buffer you need isn't known
at compile time. So if N is a variable holding the number of ints we need,
it's p = malloc(N * sizeof(int)); That's the power of it. It gives us the
memory we need to scale up to any size N, until we run out of memory and
malloc() returns NULL.
 
B

Bill Cunningham

[snip]

Thanks for your help. I work as best I can with the *nix API in kandr2 and I
am interested in sockets. The most confusing thing seems to be pointers but
that's typical. I know if you wanted a static buffer you can simply use an
array. I'm not quite sure how to use malloc as a dynamic buffer but I will
try and find out. The thing with C is I know the basics but not the
/caveats/ of how to use the language. Such as malloc is intended for buffers
that you are not sure of what the size is going to be. All I know to do is
just jump in and do your best and if you hit a brick wall; ask for help. I
am beginning to undestand what "pass by value" actually means and it
involves pointer manipulation. But as is said "C wears well as one's
experience with it.." and so on.

Bill
 
M

Michael Angelo Ravera

I guess these two statements are not the same. Why not? I guess I

haven't learned this I haven't used it much.



int *p;
p=malloc(sizeof (int)); //seems to be in error
p=malloc(sizeof (int *)); //is the correct coding

What exactly is the difference?

Please do us all a favor and study a little bit about data representation. Both of these are statements that would never (except maybe as a test of something) appear in a sane program.

Apart from an occasional effort to skirt implementation-specific limitations (like stack size, which you don't understand because you don't understanddata representation) most programmers do not use malloc when the size or multiplicity of something is known, or assumed to be known or limited, in advance and, even then, certainly, never to allocate a single scalar value.

So, go learn something about data representation and get back to us.

Of course, if you are a C programming instructor posting to see, if you canget one of your students to respond, I apologize. But, history shows that's not the case.
 
B

Barry Schwarz

[snip]

Thanks for your help. I work as best I can with the *nix API in kandr2 and I
am interested in sockets. The most confusing thing seems to be pointers but
that's typical. I know if you wanted a static buffer you can simply use an
array. I'm not quite sure how to use malloc as a dynamic buffer but I will

Why do you think you need a dynamic buffer?
try and find out. The thing with C is I know the basics but not the

Obviously not or you would never ask what the difference is between an
int and a pointer to int as you did in the origin of this discussion.
/caveats/ of how to use the language. Such as malloc is intended for buffers
that you are not sure of what the size is going to be. All I know to do is
just jump in and do your best and if you hit a brick wall; ask for help. I

Jumping in is almost always the wrong approach. Programs should be
designed after careful analysis. Actual coding is one of the very
last steps.
am beginning to undestand what "pass by value" actually means and it
involves pointer manipulation. But as is said "C wears well as one's

Pass by value has no more to do with pointer manipulation than
addition of two variables does.
 
K

Keith Thompson

Bill Cunningham said:
I am beginning to undestand what "pass by value" actually means and it
involves pointer manipulation.

No, "pass by value" specifically *doesn't* involve pointer manipulation.
 
B

Bill Cunningham

Keith said:
No, "pass by value" specifically *doesn't* involve pointer
manipulation.

I wonder what I'm meaning then. I will demonstrate by code.

int *pi;
int a;
a=20;
pi=&a;
*pi=10;
printf("%d\n",*pi);

Now you mean I am not changing the value of the int from 20 to 10 using a
pointer? Is that not pointer manipulation?

Bill
 
K

Keith Thompson

Bill Cunningham said:
I wonder what I'm meaning then. I will demonstrate by code.

int *pi;
int a;
a=20;
pi=&a;
*pi=10;
printf("%d\n",*pi);

Now you mean I am not changing the value of the int from 20 to 10 using a
pointer? Is that not pointer manipulation?

Yes, you're changing the value of the int from 20 to 10 using a pointer.
Yes, that's pointer manipulation. But it's not "pass by value".
 
B

Bill Cunningham

Keith said:
Yes, you're changing the value of the int from 20 to 10 using a
pointer. Yes, that's pointer manipulation. But it's not "pass by
value".

Ok so I'm not passing by value then. Then what exactly is passing by
value? Does it include pointer manipulation?

Bill
 
K

Keith Thompson

Bill Cunningham said:
Ok so I'm not passing by value then. Then what exactly is passing by
value? Does it include pointer manipulation?

Passing by value is how C passes parameters to functions. The value of
the argument expression is passed to the function. If the argument is a
variable name, the function doesn't have access to the variable, just to
the value it had when the call was executed. And again, no, it doesn't
involve pointer manipulation, just copying a value.

The term applies only to function calls.
 
B

Bill Cunningham

Keith Thompson said:
Passing by value is how C passes parameters to functions. The value of
the argument expression is passed to the function. If the argument is a
variable name, the function doesn't have access to the variable, just to
the value it had when the call was executed. And again, no, it doesn't
involve pointer manipulation, just copying a value.

The term applies only to function calls.

Ok. That makes sense except for this one thing. Strings. How are they
pass by value? When I think of "value" I am thinking about numerical values.
For example...

char *val;
val="I am a string.";

Pass that val to a function with a paramter of say

void func(char *value);

How is that still pass by value? It's a string. Just a little confused by
that. The ints, doubles, float and such makes sense.

Bill
 
K

Keith Thompson

Bill Cunningham said:
Ok. That makes sense except for this one thing. Strings. How are they
pass by value? When I think of "value" I am thinking about numerical values.
For example...

char *val;
val="I am a string.";

Pass that val to a function with a paramter of say

void func(char *value);

How is that still pass by value? It's a string. Just a little confused by
that. The ints, doubles, float and such makes sense.

val is an object of type char*. The value of that object (which happens
to be a pointer) is passed to func.
 
R

rescattered

[snip]



Thanks for your help. I work as best I can with the *nix API in kandr2 and I

am interested in sockets. The most confusing thing seems to be pointers but

that's typical. I know if you wanted a static buffer you can simply use an

array. I'm not quite sure how to use malloc as a dynamic buffer but I will

try and find out. The thing with C is I know the basics but not the

/caveats/ of how to use the language. Such as malloc is intended for buffers

that you are not sure of what the size is going to be. All I know to do is

just jump in and do your best and if you hit a brick wall; ask for help. I

am beginning to undestand what "pass by value" actually means and it

involves pointer manipulation. But as is said "C wears well as one's

experience with it.." and so on.



Bill

C is possibly the worst language for "just jumping in". A language like Python
is designed to be fairly intuitive and has a gentle learning curve. You can
pick up the basics on Friday and write a working program on Saturday (unlikely
to be a *good* Python program but if it is short enough it will likely do what
you intend it to do). With C, there are a host of problems which are likely to
trip up beginners (out-of-bounds errors, buffer overflows, stray pointers,
memory leaks) which can't arise in a language like Python. Furthermore -- these
are not always problems that you encounter "like a brick wall". Sometimes they
are more like hidden booby traps which don't explode until after your program
is up and seemingly running. If you learn about pitfalls only as you become
aware of them then it is almost certain that your code is going to contain
bugs silently lurking in the background.

Having said all this -- if you are a hobbyist programmer and you are just
playing around then none of this really matters, and actually coding *is* a
good way to learn. So by all means - jump in. But, don't *just* jump in. I
would recommend dividing your time between coding and reading a good book. I
highly recommend King's book "Modern C Programming".
 
R

rescattered

Malcolm McLean wrote:

Thanks for your help. I work as best I can with the *nix API in kandr2 and I
am interested in sockets. The most confusing thing seems to be pointers but
that's typical. I know if you wanted a static buffer you can simply use an
array. I'm not quite sure how to use malloc as a dynamic buffer but I will
try and find out. The thing with C is I know the basics but not the
/caveats/ of how to use the language. Such as malloc is intended for buffers
that you are not sure of what the size is going to be. All I know to do is
just jump in and do your best and if you hit a brick wall; ask for help. I
am beginning to undestand what "pass by value" actually means and it
involves pointer manipulation. But as is said "C wears well as one's
experience with it.." and so on.
Bill



C is possibly the worst language for "just jumping in". A language like Python

is designed to be fairly intuitive and has a gentle learning curve. You can

pick up the basics on Friday and write a working program on Saturday (unlikely

to be a *good* Python program but if it is short enough it will likely do what

you intend it to do). With C, there are a host of problems which are likely to

trip up beginners (out-of-bounds errors, buffer overflows, stray pointers,

memory leaks) which can't arise in a language like Python. Furthermore -- these

are not always problems that you encounter "like a brick wall". Sometimes they

are more like hidden booby traps which don't explode until after your program

is up and seemingly running. If you learn about pitfalls only as you become

aware of them then it is almost certain that your code is going to contain

bugs silently lurking in the background.



Having said all this -- if you are a hobbyist programmer and you are just

playing around then none of this really matters, and actually coding *is* a

good way to learn. So by all means - jump in. But, don't *just* jump in. I

would recommend dividing your time between coding and reading a good book. I

highly recommend King's book "Modern C Programming".

Correct title: "C Programming: A Modern Approach"
 

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

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top