Thread: Realloc/Pass by reference quagmire

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    84

    Realloc/Pass by reference quagmire

    I created a function to read in stations. Input is fine. So what I do is, in main(), pass in the array to the function. Then in the function, realloc the size of the array to the number of stations found reading in the file and then I realloc the number of characters it can hold. The errors I get are:

    1) in realloc(): warning: junk pointer, too high to make sense (this is repeated for what looks the number of stations I have)

    2) pointer to wrong page

    2) Core dumped - seg fault.

    Here is my code (not showing stuff that works fine):
    In main():
    Code:
    char **list;
    
    // So want to hold 1 string
    list = malloc(1 * sizeof(*list));
    
    // Then want the string to hold 1 char + \0
    list[0] = malloc(1*sizeof(char)+1);
    
    read(&*list);

    In read():
    Code:
    // So now realloc to hold "num" strings
    *list = realloc(*list, num * sizeof(*list));
    
    // Realloc so that the strings can hold 6 chars now
    for(i=0;i<num;++i){
         list[i] = realloc(list[i], 6 * sizeof(char));
    }
    
    // Now copy the string input
    for(i=0;i<num;++i){
        strcpy(list[i], input[i]);
    }
    
    //print out the new info
    
    for(i=0;i<num;++i){
        printf(%s is the new place.\n", list[i]);
    }
    Any ideas? Interesting is that I can pass a char in, 5, and i can print it inside the function. But after that -- only problems...

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    If you know pointers, realize that &*list == list so the function call becomes read(list).
    Don't name functions same as syscalls as that may conflict with external references.
    And thereafter realloc() storage for the proper object, as in
    Code:
    // So now realloc to hold "num" strings
    list = realloc(list, num * sizeof(*list));
    Last edited by itCbitC; 09-23-2010 at 07:28 PM.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    None of my functions are the same as syscalls... read() is actully a longer function name...

    Any ideas why I cannot realloc?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by towed View Post
    None of my functions are the same as syscalls... read() is actully a longer function name...

    Any ideas why I cannot realloc?
    I updated my last post so re-read it and do note the difference in the realloc() call.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Quote Originally Posted by itCbitC View Post
    If you know pointers, realize that &*list == list so the function call becomes read(list).
    Don't name functions same as syscalls as that may conflict with external references.
    And thereafter realloc() storage for the proper object, as in
    Code:
    // So now realloc to hold "num" strings
    list = realloc(list, num * sizeof(*list));
    I interpret that last part as:
    reallocating list to the "number of items" (aka num) of pointers to a string.

    Correct?


    hmm, it appears I am getting tripped up when allocate the necessary space for every string... Still getting junk pointer, too high to make sense...Hmm
    Last edited by towed; 09-23-2010 at 07:46 PM.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Yep!

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Well, it appears I am getting tripped up when allocate the necessary space for every string... Still getting junk pointer, too high to make sense.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    The first argument to `realloc' has to be either a pointer that has been allocated via a standard library memory call (ie: malloc, realloc, calloc) or NULL. In your example, however, the contents of `list[i]' (when i > 0) may not fulfill said requirement. You should set every list[1 .. num] to NULL before the for loop or call `malloc' instead for list[i] (except when i = 0).
    Last edited by Ronix; 09-23-2010 at 09:11 PM.
    Stick close to your desks and never program a thing,
    And you all may sit in the standards commitee!

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Ahh... Interesting... I was thinking of something similar -- just passing **list in and not mallocing space for the number of characters in the string and instead just do it in the function.

    Thanks! Did not know that about realloc. Neat idea of reallocing the one then mallocing the others...

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Hmm new issue...
    Seems it has been accomplished, but how do I reference it outside that function and in main?

    I tried printing it to the screen outside the function, just as list[i], in a for loop. But get a seg fault. Similary tried adding a * and an & to no avail.

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    How about posting the updated code?

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    84

    Thumbs down

    I can't really, this part is just a small cog of a large program...

    Where I am at it is, I can print inside the read() function, the different id's in the array that I passed in.

    Now in main(), when trying to print out the same ids (using the same print statement and for loop combo) it seg faults.

    In main():
    Code:
    for(i=0;i<num;++i){
        printf(%s is the new place.\n", list[i]);
    }
    I thought I passed the reference in, such that I can use the list outside that function...

    Ideas?

  13. #13
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Probably you should read Question 4.8

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It looks like you're running into a scope issue.
    Think about having your realloc function return a pointer so the array pointer is updated outside the fuction.
    Alternatively declare your array pointer globally.

  15. #15
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Ok, seems to be working. I made struct to hold it, allocate memory for a pointer to the struct... passed it in. And now can use the list outside the function -- is that what you were getting at?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM