realloc in array struct

This is a discussion on realloc in array struct within the C Programming forums, part of the General Programming Boards category; i have this code: Code: char record[100]; int N=0; typedef struct{ double x,y; int year; }eggrafi; eggrafi *arxeio=NULL,*arxeio1; while(N<15) { ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    1

    realloc in array struct

    i have this code:

    Code:
    	char record[100];
    	int N=0;
    	
    	typedef struct{
    		double x,y;
    		int year;
    	}eggrafi;
    
    	eggrafi *arxeio=NULL,*arxeio1;
    
    
    		
    	while(N<15)
    	{
    		arxeio1=(eggrafi*)realloc(arxeio,(N+1)*sizeof(eggrafi));
                    arxeio=arxeio1;
    
    		arxeio[N].x=596.369;
    		arxeio[N].y=596.639;
    		arxeio[N].year=5;
    	
    		N++;
    	}
    when i try to run this code it crashes...
    when i delete the blue line no prob...
    any ideas?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Yeah, what else is in your code?

    That, by itself, isn't a problem (except for the leak at the end)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ( ) {
    char record[100];
        int N=0;
    
        typedef struct{
            double x,y;
            int year;
        }eggrafi;
    
        eggrafi *arxeio=NULL,*arxeio1;
    
    
    
        while(N<15)
        {
            arxeio1=(eggrafi*)realloc(arxeio,(N+1)*sizeof(eggrafi));
                    arxeio=arxeio1;
    
            arxeio[N].x=596.369;
            arxeio[N].y=596.639;
            arxeio[N].year=5;
    
            N++;
        }
      return 0;
    }
    $ ./a.out 
    $ valgrind a.out
    valgrind: a.out: command not found
    $ valgrind ./a.out
    ==2017== Memcheck, a memory error detector
    ==2017== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
    ==2017== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
    ==2017== Command: ./a.out
    ==2017== 
    ==2017== 
    ==2017== HEAP SUMMARY:
    ==2017==     in use at exit: 300 bytes in 1 blocks
    ==2017==   total heap usage: 15 allocs, 14 frees, 2,400 bytes allocated
    ==2017== 
    ==2017== LEAK SUMMARY:
    ==2017==    definitely lost: 300 bytes in 1 blocks
    ==2017==    indirectly lost: 0 bytes in 0 blocks
    ==2017==      possibly lost: 0 bytes in 0 blocks
    ==2017==    still reachable: 0 bytes in 0 blocks
    ==2017==         suppressed: 0 bytes in 0 blocks
    ==2017== Rerun with --leak-check=full to see details of leaked memory
    ==2017== 
    ==2017== For counts of detected and suppressed errors, rerun with: -v
    ==2017== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 8)
    If you screw up with malloc at any point in the program, then the effect can show up
    - immediately,
    - some time later, in an unrelated part of the program
    - far in the future, like a change of OS or compiler
    - never

    It looks like door number 2 to me.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    unless this is for an assignment where you need to use realloc, you might consider allocating all the space you need upfront (since you know you will need 15 eggrafi's) instead of realloc'ing in a loop. at a glance, i don't think arxeio1 is needed, you can just assign it right to arxeio.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21