Results 1 to 2 of 2

Thread: char * problem

  1. #1
    Join Date
    Nov 2006
    Posts
    96

    Default char * problem

    Hi:

    This is how I get info from the server. The server sends it in "struct risp" format
    Qt Code:
    1. void MainWindow::getSysStat() {
    2. /* create the system info strusture that will hold information about the server */
    3. sysInfo = (struct risp *)malloc(sizeof(struct risp));
    4. //sysInfo->running_kernel = (char *)malloc(sizeof(char) * LONGEST_KERNEL_NAME);
    5. //sysInfo->fsStat->name = (char *)malloc(sizeof(char) * LONGEST_NAME);
    6. //sysInfo->fsStat->devName = (char *)malloc(sizeof(char) * LONGEST_NAME);
    7.  
    8. /* wait until the socket gets connected */
    9. if(socket->waitForConnected(-1)) {
    10. /* wait until data gets available */
    11. if(socket->waitForReadyRead(-1)) {
    12. /* read from the socket */
    13. char *dataRead = (char *)malloc(sizeof(char)*sizeof(struct risp));
    14. if((socket->read(dataRead, sizeof(struct risp))) == -1)
    15. qDebug() << "Error reading data from server";
    16.  
    17. /* cast the data to match the risp structure */
    18. sysInfo = (struct risp *)dataRead;
    19. //qDebug() << "total cpu is: " << sysInfo->total_cpu;
    20. }
    21. }
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    Here is struct risp:

    Qt Code:
    1. struct risp {
    2. /* cpu info */
    3. unsigned long cpu_usage; /* /proc/ */
    4. unsigned long total_cpu; /* /proc/cpuinfo */
    5. //char proc_name[LONGEST_PROC_NAME]; /* /proc/ */
    6. //char *cpu_processes[NUMBER_OF_PROCESSES]; /* /proc/ */
    7.  
    8. /* ram info */
    9. unsigned int ram_usage; /* /proc/meminfo */
    10. unsigned int total_ram; /* /proc/meminfo */
    11. //char *ram_processes[NUMBER_OF_PROCESSES]; /* /proc/ */
    12.  
    13. /* swap info */
    14. unsigned int swap_usage; /* /proc/meminfo */
    15. unsigned int total_swap; /* /proc/swaps */
    16.  
    17. /* filesystems */
    18. struct fsMountList *fsStat;
    19. unsigned short int numFs;
    20.  
    21. /* kernel info */
    22. char *running_kernel; /* /proc/ */
    23.  
    24. float uptime; /* /proc/uptime -> the first number is the uptime in seconds */
    25. unsigned short int num_users; /* /proc */
    26. unsigned short int num_processes;
    27. unsigned short int num_running_processes; /* /proc/stat -> procs_running */
    28. char arips_included; /* 0/1 */
    29.  
    30. };
    To copy to clipboard, switch view to plain text mode 


    The problem is this:

    Qt Code:
    1. qDebug("Kernel: %s", sysInfo->running_kernel);
    To copy to clipboard, switch view to plain text mode 
    it displays noting like ti should: it should display: "2.5.25"

    So does anybody see what's the problem ?

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: char * problem

    i) readyRead just means some data is available...
    you have to wait til enough data has been read.
    Insert some debug statements that print the number of bytes read.
    Probably the number is lower than the size of your struct.

    There are examples on how to do that in the Qt docs.

    ii) you must not transmit POINTERS over tcp/ip.
    The pointer value points to some string on the server side.
    This pointer value is MEANINGLESS on the client side.
    (It is undef. behaviour to dereference it. Usually your program will crash.)

    HTH

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 21:48
  2. Problem in XML parsing ";" (semicolon) char
    By the_bis in forum Qt Programming
    Replies: 7
    Last Post: 1st June 2007, 12:21
  3. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 21:49
  4. Replies: 16
    Last Post: 7th March 2006, 16:57
  5. K&R's strcpy function - problem with pointers
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 8th January 2006, 16:16

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.