Thread: Need help on malloc and realloc

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    Need help on malloc and realloc

    Hello,
    I am a C newbie working on a program using malloc and realloc functions. The problem is after compiling and running the program, the output of the array is garbage values. I would really appreciate it if someone could point out what's wrong with my code. Thanks a lot.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include <cstdlib>  // for malloc and realloc memory functions
    
    int main() {
    	int *listPtr;   // pointer to dynamically allocated array
    	int temp_input; // value which will be copied into listPtr array
    	int count=0;    // count value used to reallocate a new size for the integer
    	                // array on each integer input
    
    	listPtr = (int *) malloc(2 * sizeof(int)); 
    	// allocate memory locations for a minimum of 2 integers
    
    	do {
    		++count;
    		cout << "Enter integer " << count << ": ";
    		cin >> temp_input;
    
    		listPtr = (int *) realloc(listPtr, count); 
    		// reallocate the array increasing its size by 1 since
    		// one more integer is entered each time
    		
    		if (temp_input != -1) { 
    			*(listPtr + (count-1)) = temp_input; // count-1 so first space is used up
    		}
    		// assign subsequent integer into next free array spot
    		// make sure an array is pointing to the next free spot since realloc()
    		// function points to beginning of the array each time
    
    	} while (temp_input != -1);
    
    	for (int a=0; a < count; a++) {  // output the dynamically allocated array
    		cout << *(listPtr + a) << ' ';
    	}
    	cout << endl;
    
    return 0;
    }
    Justin
    Last edited by YevGenius; 03-05-2004 at 10:52 PM.

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    YevGenius.

    Please decide whether you're going to code this in C or C++.

    Once decided place the code in the appropriate forum.
    R.I.P C89

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    What makes you think this is a C++ program?

    Hi,
    I don't understand. I think I have written everything according to C standards. Is it the cin and couts that make you think this is a C++ program? Can you please tell me why you can't tell if this is a C or C++ program?

    Justin

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    You need to error-check you malloc to make sure it actually allocated the memory. It returns NULL if an error occured. also you don't need to typecast malloc. Don't ask me why someone else should post why I don't know. Also remember to free(); your malloc and realloc calls

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Re: What makes you think this is a C++ program?

    Originally posted by YevGenius
    Hi,
    I don't understand. I think I have written everything according to C standards. Is it the cin and couts that make you think this is a C++ program? Can you please tell me why you can't tell if this is a C or C++ program?

    Justin
    me too
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    the only 2 things that make this code c++ is the cout and cin plus the int var in your for loop you cannot create variables in the middle of the program only at the beginning
    [edit]
    well I think in c99 that int var in a for loop is legal, but I feel it is depreciated
    [/edit]

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    No errors with malloc or realloc...just garbage values outputted

    Hi again,
    To the person that told me to check that I don't get a NULL in a pointer after malloc or realloc, I checked it and it did not give me any problems. So, I am still stuck. I continue receiving garbage values for my output.

    Please help.

    Justin

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: What makes you think this is a C++ program?

    Originally posted by YevGenius
    Hi,
    I don't understand. I think I have written everything according to C standards. Is it the cin and couts that make you think this is a C++ program? Can you please tell me why you can't tell if this is a C or C++ program?

    Justin
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include <cstdlib>  // for malloc and realloc memory functions
    Nothing in your program is C if any part of it is C++. Your C compiler can not compile any code with any C++ in it. If it compiles, it's because you're compiling it as C++ and not C.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > listPtr = (int *) malloc(2 * sizeof(int));
    There is no point with an initial memory allocation, you can just start with
    listPtr = NULL;
    realloc handles extending memory from nothing.

    > listPtr = (int *) realloc(listPtr, count);
    3 things wrong here
    1. You're compiling with C++ - yeah, the other posters are right. In C, we don't cast the return result of malloc type calls. C++ forces you to do this. The cout calls also give the game away.
    2. you forgot the sizeof(int) scaling factor
    3. you don't check that the realloc call succeeded. Worse still, if it failed, you lose everything

    Code:
    void *temp = realloc( listPtr, count * sizeof *listPtr );
    if ( temp != NULL ) {
        /* success, update our real pointer */
        listPtr = temp;
    } else {
        /* oh no, save whats in listPtr and back out */
        /* save and quit, or just report can't do that */
    }
    > if (temp_input != -1)
    do this before extending the array.

    > *(listPtr + (count-1)) = temp_input;
    Unless this is also an exercise in using pointer notation, its clearer to write
    listPtr[count-1] = temp_input;
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, realloc..wat on earth!
    By zesty in forum C Programming
    Replies: 3
    Last Post: 12-21-2007, 01:42 PM
  2. To malloc or not realloc on struct members
    By gh0st in forum C Programming
    Replies: 4
    Last Post: 11-17-2006, 05:27 AM
  3. malloc, realloc
    By figo2476 in forum C Programming
    Replies: 3
    Last Post: 04-28-2006, 10:11 PM
  4. Linked list versus malloc and realloc.
    By Bajanine in forum C Programming
    Replies: 2
    Last Post: 06-20-2005, 08:08 PM
  5. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM