Re: (newbie) question concerning memory allocation

Discussion in 'C Programming' started by Joe Pfeiffer, Sep 29, 2012.

  1. Joe Pfeiffer

    Joe Pfeiffer Guest

    lipska the kat <> writes:

    > Ubuntu Linux 12.04 LTS 64bit
    > gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
    > /bin/bash
    >
    > Hi
    >
    > I'm a raw beginner at C so please excuse me if
    > the answer to my question is obvious.
    >
    > I have the following program called
    > pointers.c It compiles and if I run it
    > thusly:
    >
    > $pointers 3
    >
    > I get the expected output
    >
    > 1 2 3
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(int argc, char *argv[]){
    >
    > struct foo{
    > int bar;
    > long baz;
    > };
    >
    > int x = 0;
    >
    > if(argc > 1){
    > x = atoi(argv[1]);
    > }
    >
    > struct foo **foos;
    > struct foo **bars;


    Notice that you've declared foos to be a pointer to a pointer to a
    struct foo -- foos itself is currently uninitialized, and can be
    pointing anywhere
    > int i;
    >
    > /* create foos*/
    > for(i = 0; i < x; i++){
    > *foos = malloc(sizeof(struct foo));


    At this point, you've got whatever foos points at (that's what *foos
    means) pointing at your newly allocated space. But you haven't assigned
    foos itself pointing at anything, so this statement is working through
    sheer luck.

    <snip>

    > The question is, why does declaring a second pointer to the array of
    > pointers to foo cause the first pointer (foos) be be allocated an
    > unavailable memory slot.


    This will be an unsatisfying answer, but it only worked the first time
    through luck (and, for that matter, when I tried it on my machine it
    seg faulted the first way, too). I can't tell you why, specifically,
    that assignment is causing your program to crash now.

    If you were to actually allocate the space for foos, everything would
    work:
    /* create foos*/
    foos = malloc(x * sizeof(*foos)); // NEW!

    for(i = 0; i < x; i++){
    *foos = malloc(sizeof(struct foo));
    (*foos)->bar = i + 1;
    (*foos)->baz = i + 2;
    ++foos;
    }

    /* set foos back to the first foo */
    foos-=x;

    For that matter, the code might be a bit clearer if you also rewrote
    your for-loop as

    for (i = 0; i < x; i++){
    foos = malloc(sizeof(struct foo));
    foos->bar = i + 1;
    foos->baz = i + 2;
    }

    /* set foos back to the first foo */
    // foos-=x; don't do this any more
     
    Joe Pfeiffer, Sep 29, 2012
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RmxhY2s=?=

    Newbie question concerning page load event

    =?Utf-8?B?RmxhY2s=?=, May 17, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    521
    TDAVISJR
    May 17, 2005
  2. Ken
    Replies:
    24
    Views:
    3,945
    Ben Bacarisse
    Nov 30, 2006
  3. chris
    Replies:
    6
    Views:
    1,031
    chris
    Oct 28, 2005
  4. Eric Sosman
    Replies:
    6
    Views:
    299
    Phil Carmody
    Oct 10, 2012
  5. Ben Bacarisse

    Re: (newbie) question concerning memory allocation

    Ben Bacarisse, Sep 29, 2012, in forum: C Programming
    Replies:
    3
    Views:
    330
    Anand Hariharan
    Oct 10, 2012
Loading...

Share This Page