use of printf prevents segfault!

This is a discussion on use of printf prevents segfault! within the C Programming forums, part of the General Programming Boards category; gcc 4.1.2 for i386-redhat-linux What I really should have been doing all along, as laserlight eventually points out in another ...

  1. #31
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    gcc 4.1.2 for i386-redhat-linux

    What I really should have been doing all along, as laserlight eventually points out in another thread, was this
    Code:
    struct thestruct {
    [...]
    } *ray;
    int main () {
         short int nofl, i;
         [...determine nofl]
         ray = malloc(nofl * sizeof(*ray));
         for (i=0; i<nofl; i++) {
              ray[i].member = whatever;
         }
    Sorry for wasting so much time...like I said, bullheaded and new to C. But I did pick up some stuff along the way.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #32
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,735
    Incidentally, must ray really be a global variable? If feasible, make it a local variable. Also, remember to free() what you malloc(), and to check that malloc() does not return NULL before using ray as a dynamically allocated array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 08:48 PM
  2. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 08:38 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM

Tags for this Thread


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