Thread: question about realloc

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    question about realloc

    if anyone is familiar with steve summit's c tutorial (http://c-faq.com/~scs/cclass/) thats what im reading

    anyway
    it says in section 11.3 (beginner's tutorial) that i can use realloc like so:

    newpointer_to_int = realloc(oldpointer_to_int,maxsize*sizeof(int));

    the compiler (dev-cpp) gives an error:
    [Warning] assignment makes pointer from integer without a cast

    it wont let me assign the pointer realloc returns? to a new pointer
    supposedly im supposed to be assigning this new pointer to see if it would be null or not so i know whether or not to try to write to the newly reallocated memory

    but if i dont bother with that, if i just assume the rellocation works it compiles fine
    ie:
    realloc(oldpointer_to_int,maxsize*sizeof(int));

    anyone want to enlighten me? perhaps re-explain how realloc actually works and maybe comment on the tutorial and tell me if its wrong or if im screwing up or what

    one last thing, lcc gives me even more errors than dev-cpp lol so im just using dev-cpp

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by thesand
    it says in section 11.3 (beginner's tutorial) that i can use realloc like so:

    newpointer_to_int = realloc(oldpointer_to_int,maxsize*sizeof(int));

    the compiler (dev-cpp) gives an error:
    [Warning] assignment makes pointer from integer without a cast
    #include <stdlib.h>?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    ha, sometimes i can look at a piece of code for an hour and still not know where i screwed up

    and it only takes 4 seconds for someone else to notice

    that was definitely the problem lol thanks

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >and it only takes 4 seconds for someone else to notice
    Imagine how many times we've done that exact same thing to notice it so quickly. ^_^
    My best code is written with the delete key.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > one last thing, lcc gives me even more errors than dev-cpp lol so im just using dev-cpp
    You should regard this as a good thing. Spotting potential problems earlier rather than later is always better (nice warning message vs. obscure results or crashes).

    Add these options to the compiler in dev-c++, and it should be a lot more chatty.
    -W -Wall -ansi -pedantic -O2

    Now, if your code compiles and runs with both compilers, then you're definitely onto something which might be a genuine portable C program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    prelude: touché
    salem: i didnt even consider verbose could be a good thing in my compiler - thanks for the tip you just probly saved me 5000 hours of sitting here not knowing where i f'd up

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Realloc Quick Question.
    By killpoppop in forum C Programming
    Replies: 36
    Last Post: 01-13-2009, 01:04 PM
  3. Quick realloc question
    By Matt13 in forum C Programming
    Replies: 7
    Last Post: 04-02-2004, 08:40 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM