Closed Thread
Results 1 to 6 of 6

Thread: realloc() fails on one machine, succeeds on another?

  1. #1
    axi0m is offline Newbie
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Question realloc() fails on one machine, succeeds on another?

    Hi everyone,

    I am writing a program in standard C that uses the MySQL library and the arpa/inet.h header to read some IP addresses stored as decimals, convert them to their respective dotted-format IPaddress with inet_ntop(), and store the results in another column of the same table with a MySQL query. That query string, which is called update_query, is concatenated from the original decimal value, the converted value, and some necessary syntax.

    I developed this code in the Unix environent and compiled it with gcc. For some reason, it works on my 512MB Ubuntu 8.04 server, but it doesn't run (it gets a segmentation fault) on an ancient 512MB CentOS v4 server.

    A backtrace via "gdb -q (filename)" reveals that the segmentation error happens when I first call realloc() in the while loop. Can someone please help me understand why, and what I can do to get this working on the older CentOS server?

    Code:
    /* Simple C program that connects to MySQL Database server*/
    #include <stdlib.h>
    #include </usr/include/mysql/mysql.h>
    #include <stdio.h>
    #include <arpa/inet.h>
    #include <string.h>
    
    #define MAX_QUERY_LENGTH 81
    
    main() {
       MYSQL *conn;
       MYSQL_RES *res;
       MYSQL_ROW row;
    
       int i=0, j;
    
       char converted_addr[INET_ADDRSTRLEN];
       unsigned long row_dec;
    
       char **updatearray;
       int uparr_size=1; 
    
       char *server = "localhost";
       char *user = "ipplan";
       char *password = "*****"; /* password for database */
       char *database = "ipplan";
    
       conn = mysql_init(NULL);
    
       /* Connect to database */
       if (!mysql_real_connect(conn, server,
             user, password, database, 0, NULL, 0)) {
          fprintf(stderr, "%s\n", mysql_error(conn));
          exit(1);
       }
    
       /* send SQL query */
       if (mysql_query(conn, "select ipaddr from ipaddr")) {
          fprintf(stderr, "%s\n", mysql_error(conn));
          exit(1);
       }
    
       res = mysql_use_result(conn);
    
       /* output Decimal - Dotted IP addresses */
       while ((row = mysql_fetch_row(res)) != NULL) {
          /* This is a test of inet_ntop() */
          row_dec = htonl(atol(row[0]));
          inet_ntop(AF_INET, &row_dec, converted_addr, INET_ADDRSTRLEN);
          printf("%s\t-\t%s\n", row[0], converted_addr);
    
    
          char update_query[MAX_QUERY_LENGTH] = "update ipaddr set location=\'";
          strcat(update_query, converted_addr);
          strcat(update_query, "\' where ipaddr=\'");
          strcat(update_query, row[0]);
          strcat(update_query, "\'");
          /* printf("%s\n", update_query); */
    
          if ((updatearray = (char **)realloc(updatearray, (uparr_size++)*sizeof(char *)) ) == NULL ) {
                  fprintf(stderr, "%s\n", "Error: Could not dynamically realloc() char **updatearray!");
                  exit(1);
          }
    
          if ((updatearray[i] = (char *)malloc(MAX_QUERY_LENGTH)) == NULL) {
                  fprintf(stderr, "%s\n", "Error: Could not dynamically malloc() updatearray[i]!");
                  exit(1);
          }
    
          strncpy(updatearray[i], update_query, MAX_QUERY_LENGTH);
    
          i++;
        }
    
        /* free results from query */
        mysql_free_result(res);
    
        for (j=0; j < i; j++) {
                printf("%s\n", updatearray[j]);
    
                if (mysql_query(conn, updatearray[j])) {
                    fprintf(stderr, "%s\n", mysql_error(conn));
                    exit(1);
    
                    }
    
       }
    
       /* close connection */
       mysql_close(conn);
    
       return 0;
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: realloc() fails on one machine, succeeds on another?

    You may want to initialize updatearray to NULL initially.
    Quote Originally Posted by n1124
    If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size. Otherwise, if ptr does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to the free or realloc function, the behavior is undefined. If memory for the new object cannot be allocated, the old object is not deallocated and its value is unchanged.
    One idiom you want to avoid is this:
    Code:
    ptr = realloc(ptr, newsize);
    This will leak memory if realloc fails.

  4. #3
    axi0m is offline Newbie
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Talking Re: realloc() fails on one machine, succeeds on another?

    Quote Originally Posted by dcs View Post
    You may want to initialize updatearray to NULL initially.

    One idiom you want to avoid is this:
    Code:
    ptr = realloc(ptr, newsize);
    This will leak memory if realloc fails.
    Thank you for that! This is what my code now looks like, and it runs perfectly:

    Code:
    /* Simple C program that connects to MySQL Database server*/
    #include <stdlib.h>
    #include </usr/include/mysql/mysql.h>
    #include <stdio.h>
    #include <arpa/inet.h>
    #include <string.h>
    
    #define MAX_QUERY_LENGTH 81
    
    main() {
       MYSQL *conn;
       MYSQL_RES *res;
       MYSQL_ROW row;
    
       int i=0, j;
    	
       char converted_addr[INET_ADDRSTRLEN];
       unsigned long row_dec;
    
       char **updatearray = NULL, **tmp;
    	
       char *server = "localhost";
       char *user = "ipplan";
       char *password = "*****"; /* set me first */
       char *database = "ipplan";
    
       conn = mysql_init(NULL);
    
       /* Connect to database */
       if (!mysql_real_connect(conn, server,
             user, password, database, 0, NULL, 0)) {
          fprintf(stderr, "%s\n", mysql_error(conn));
          exit(1);
       }
    
       /* send SQL query */
       if (mysql_query(conn, "select ipaddr from ipaddr")) {
          fprintf(stderr, "%s\n", mysql_error(conn));
          exit(1);
       }
    
       res = mysql_use_result(conn);
    
       /* output Decimal - Dotted IP addresses */
       while ((row = mysql_fetch_row(res)) != NULL) {
          /* This is a test of inet_ntop() */
          row_dec = htonl(atol(row[0]));
          inet_ntop(AF_INET, &row_dec, converted_addr, INET_ADDRSTRLEN); 
          printf("%s\t-\t%s\n", row[0], converted_addr);
    
          
          char update_query[MAX_QUERY_LENGTH] = "update ipaddr set location=\'";
          strcat(update_query, converted_addr);
          strcat(update_query, "\' where ipaddr=\'");
          strcat(update_query, row[0]);
          strcat(update_query, "\'");
    
          if ((tmp = realloc(updatearray, (i+1)*sizeof(*updatearray)) ) == NULL ) {
    	      fprintf(stderr, "%s\n", "Error: Could not dynamically realloc() char **updatearray!");
    	      exit(1);
          }
          
          updatearray = tmp;
    
          if ((updatearray[i] = malloc(MAX_QUERY_LENGTH)) == NULL) {
    	      fprintf(stderr, "%s\n", "Error: Could not dynamically malloc() updatearray[i]!");
    	      exit(1);
          }
          
          strncpy(updatearray[i], update_query, MAX_QUERY_LENGTH);
          i++;
       }
        
       /* free results from query */
       mysql_free_result(res);
        
       for (j=0; j < i; j++) {
    	    printf("%s\n", updatearray[j]);
    	    
    	    if (mysql_query(conn, updatearray[j])) {
    		fprintf(stderr, "%s\n", mysql_error(conn));
    		exit(1);
    	
    	    }
       }
    
       /* close connection */
       mysql_close(conn);
       
       return 0;
    }

  5. #4
    axi0m is offline Newbie
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Question Re: realloc() fails on one machine, succeeds on another?

    Should I call free() after using malloc() and realloc() or does it not matter in this case? What would be right way to do it if I should? (This program is going to be run as a cronjob on an hourly basis every day.)

  6. #5
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: realloc() fails on one machine, succeeds on another?


  7. #6
    axi0m is offline Newbie
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Re: realloc() fails on one machine, succeeds on another?

    I'm embarrassed, thank you again. You could not have answered my questions any better!

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. what exactly is the second argument of realloc()???
    By shakisparki in forum C and C++
    Replies: 2
    Last Post: 04-05-2011, 08:18 PM
  2. realloc issues
    By chili5 in forum C and C++
    Replies: 4
    Last Post: 03-20-2011, 03:13 PM
  3. Using malloc() and realloc() for a variable-length string
    By DarkLordofthePenguins in forum C and C++
    Replies: 6
    Last Post: 03-17-2011, 10:04 PM
  4. Dynamic Arrays: Using malloc() and realloc()
    By Guest in forum C Tutorials
    Replies: 33
    Last Post: 10-25-2010, 08:39 PM
  5. Realloc problem
    By yelman in forum C and C++
    Replies: 2
    Last Post: 04-15-2010, 12:44 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts