coding dynamic allocation question

Discussion in 'C Programming' started by Bill Cunningham, Apr 19, 2014.

  1. 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 Cunningham, Apr 19, 2014
    1. Advertisements

  2. 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.
    Malcolm McLean, Apr 19, 2014
    1. Advertisements

  3. [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 Cunningham, Apr 20, 2014
  4. 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.
    Michael Angelo Ravera, Apr 20, 2014
  5. Why do you think you need a dynamic buffer?
    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.
    Jumping in is almost always the wrong approach. Programs should be
    designed after careful analysis. Actual coding is one of the very
    last steps.
    Pass by value has no more to do with pointer manipulation than
    addition of two variables does.
    Barry Schwarz, Apr 20, 2014
  6. No, "pass by value" specifically *doesn't* involve pointer manipulation.
    Keith Thompson, Apr 20, 2014
  7. I wonder what I'm meaning then. I will demonstrate by code.

    int *pi;
    int a;

    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 Cunningham, Apr 20, 2014
  8. 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".
    Keith Thompson, Apr 20, 2014
  9. Ok so I'm not passing by value then. Then what exactly is passing by
    value? Does it include pointer manipulation?

    Bill Cunningham, Apr 20, 2014
  10. 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.
    Keith Thompson, Apr 20, 2014
  11. 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 Cunningham, Apr 20, 2014
  12. val is an object of type char*. The value of that object (which happens
    to be a pointer) is passed to func.
    Keith Thompson, Apr 20, 2014
  13. Bill Cunningham

    Ian Collins Guest

    That's over generous, I'd only go to a 7.
    Ian Collins, Apr 20, 2014
  14. Bill Cunningham

    rescattered Guest

    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".
    rescattered, Apr 20, 2014
  15. Bill Cunningham

    rescattered Guest

    Correct title: "C Programming: A Modern Approach"
    rescattered, Apr 20, 2014
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.