Thread: Error when realloc in C

  1. #16
    Registered User
    Join Date
    Jul 2016
    Posts
    17
    Quote Originally Posted by taazz View Post
    True enough but ++p_Array == p_array = p_array+sizeof(pointer); which in effect looses the primary address creating a memory leak.
    After increasing address, I reset address by p_array -= size. But error still occurs.

  2. #17
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by thachdovan View Post
    After increasing address, I reset address by p_array -= size. But error still occurs.
    1) you should always post the error you get.
    2) I do not know if "p_array -= size" is correct, from a sleepy look it looks ok but I need to test it to be sure.
    3) realloc should allocate enough bytes to hold size integers eg
    Code:
     p_array =(int *) realloc(p_array, sizeof(int) * size);

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thachdovan
    In above code, I init p_array with malloc, so I need to free it. why in this case, I dont have to free it. Please share in more detail.
    Aslaville must have misread the code.

    Quote Originally Posted by thachdovan
    After increasing address, I reset address by p_array -= size. But error still occurs.
    That is simply error prone. Follow Salem's advice:
    Quote Originally Posted by Salem
    Do NOT modify the pointer variable used to hold the result of malloc, otherwise you won't be able to free it later on.
    Instead, use a different pointer to point to where you want.

    Furthermore, you should not be declaring global variables. Declare local variables only for now.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Jul 2016
    Posts
    17
    Furthermore, you should not be declaring global variables. Declare local variables only for now.
    My application need to allocate dynamic memory and change it in runtime. I try to use it as a local variable but I can not (I put it in some another and use extern to process). So my question is why I should not be use as local variables, could you give me some relative links or documents about this problem.
    Thank everyone!

  5. #20
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Because global variables can confuse and obtuse code. They are harder to
    keep track of, and because they can be modified within any function without being
    passed via the parameter it's possible an overlooked piece of executing code can
    modify the value by accident resulting in invalid results.

    A more reliable type that is similar is to make said variable static. But of course,
    there are important differences between the two.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-13-2013, 11:35 AM
  2. Error using realloc inside function
    By NotAProgrammer in forum C Programming
    Replies: 10
    Last Post: 12-29-2012, 08:13 AM
  3. Replies: 3
    Last Post: 08-22-2008, 11:12 AM
  4. Error when freeing memory (allocated with realloc)
    By svdrjeug in forum C Programming
    Replies: 18
    Last Post: 01-03-2008, 11:16 AM
  5. use of realloc
    By alzar in forum C Programming
    Replies: 3
    Last Post: 09-17-2007, 12:18 PM

Tags for this Thread