Thread: Function returning malloc'ed memory.

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    Function returning malloc'ed memory.

    OK, I wrote a function that receives two number ranges (say 1 to 1 million), and the address of a counter variable. All values are unsigned long long.

    Code:
    // prototype
    u_long_64 *semiprimegen(u_long_64 first_range, u_long_64 second_range, u_long_64 *semiprimes_count);
    The function will generate semi-prime numbers, allocating the first bunch of memory and reallocating as needed, modifying the counter variable until the ranges are met, sorts the allocated memory, and returns a pointer to that memory, which contains *semiprimes_count elements of type unsigned long long.

    So, In my main function, I have this:
    Code:
    semiprimes = semiprimegen(first_range, second_range, &semiprimes_count);
    Now, after this statement, I accessed the semiprimes_count variable. It works, info is there like its supposed to. I can see the address the semiprimes pointer has (confirmed by printing to console right before semiprimes function returns). But I CAN'T access the memory! What is wrong with this? The printf I put is:

    Code:
    printf("20th semiprime is %I64u\n", *(semiprimes + 19));
    The program crashes right there. In that printf. Anybody knows what's wrong? Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Fixed it!

    What I explained in the post was absolutely ok. It was a problem I had later in the code, but for some reason, it crashed before displaying the printf (no idea why).

    I got scared, I suddenly thought that malloc'ed memory would go out of scope or something, apparently its not the case

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by samus250 View Post
    Fixed it!

    What I explained in the post was absolutely ok. It was a problem I had later in the code, but for some reason, it crashed before displaying the printf (no idea why).
    Using fflush(stdout) may help here - it makes sure the output has gone to the output before continuing.

    I got scared, I suddenly thought that malloc'ed memory would go out of scope or something, apparently its not the case
    Certainly not the case - it's available as long as you have a pointer to it (technically the memory is available even if you haven't got a pointer, just that it's hard to find the address of it...).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Using fflush(stdout) may help here - it makes sure the output has gone to the output before continuing.
    Where do I put the fflush(stdout) [or fpurge(stoud), as learned from C for Dummies]? Before the printf, or after the printf?

    Because I've noticed that problem in some programs. For example, one that enters a loop which may take up to 5 minutes to get out of (finds all prime numbers from a given range, which normally is like 1 to 100 million). Before the loop I wrote a printf, and it doesn't show up until the loop finishes.

    Thanks!

    Edit: I almost forgot, a lot of weird things also happen depending on where I put the newline character. I always try to put it at the end of each string because sometimes, when I put it at the beginning, it doesn't show up until a next printf. (I'll experiment more on that later).
    Last edited by samus250; 04-30-2008 at 01:20 PM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Normally, you would put fflush immediately after the printf (or after a few printf's, if you have several in a row that you don't care to show until the last one is done, if that makes sense to you).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by samus250 View Post
    Edit: I almost forgot, a lot of weird things also happen depending on where I put the newline character. I always try to put it at the end of each string because sometimes, when I put it at the beginning, it doesn't show up until a next printf. (I'll experiment more on that later).
    That's because stdout is usually line-buffered, which means it's flushed automatically when it encounters a newline.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Replies: 7
    Last Post: 10-15-2008, 03:38 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM