Thread: no outprint from function

  1. #1
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92

    no outprint from function

    I'm having an error in my code I can't figure out. At first I thought it was an error on malloc() or sysctl(), but it seems it's neither. Since I'm all out of options, I'm posting it here.
    Code:
    int main() {
    
        char *systemName;
    
        printf("system information:\n");
        systemName = sysName();
        printf("\toperating system:\t\t%s\n", systemName);
        free(systemName);
    
        return 0;
    }
    
    char *sysName() {
        
        int mib[2];
        size_t length;
        char *osName;
    
        if((osName = (char *)malloc(50)) == NULL) {
            printf("Error on malloc(), no memory allocated\n");
        }
    
        memset(osName, 0, 50);
    
        mib[0] = CTL_KERN;
        mib[1] = KERN_OSTYPE;
    
        length = sizeof(osName);
        
        sysctl(mib, 2, &osName, &length, NULL, 0);
        
        error(); // a function that checks errno
    
        return osName;
         
    }
    The operating system name is not printed out; the line is just blank. If I set osName to be, i.e. 'A': osName[0] = 'A' the A is printed out. This leaves me thinking it's something wrong with sysctl(), but I've used the function the exact same way in another program - where it works! Can anyone see what's wrong? Help appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > length = sizeof(osName);
    You're doing sizeof on a pointer, which is probably not what you want, if you're looking for something with a value of around 50

    > If I set osName to be, i.e. 'A': osName[0] = 'A' the A is printed out
    Probably because it now fits in the 4 bytes you've told it exists, rather than the 50 you think exists.

    size_t length = 50;
    osName = malloc( len * sizeof *osName );
    memset(osName, 0, len);
    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.

  3. #3
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Thanks for your reply, Salem.

    I fixed some of the code, but it now produces a segmentation fault instead. I must have done something wrong (again). I'm not very familiar with malloc; but is the code correct now? It looks kinda.. weird.
    Code:
    char *sysName() {
        
        int mib[2];
        size_t length = 50;
        char *osName;
    
        osName = malloc( length * sizeof *osName );
    
        memset(osName, 0, length);
    
        mib[0] = CTL_KERN;
        mib[1] = KERN_OSTYPE;
        
        sysctl(mib, 2, &osName, &length, NULL, 0);
        
        error();
    
        return osName;
         
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > sysctl(mib, 2, &osName, &length, NULL, 0);
    Knowing nothing about this call, I would have said
    sysctl(mib, 2, osName, length, NULL, 0);
    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.

  5. #5
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    this produced the output I wanted: sysctl(mib, 2, osName, &length, NULL, 0);

    Thanks a million, you saved the day.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM