problem with data in allocated memory

This is a discussion on problem with data in allocated memory within the C Programming forums, part of the General Programming Boards category; hi, i have a problem in storing the data in allocated memory using malloc. i have structure to store huge ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    11

    problem with data in allocated memory

    hi,

    i have a problem in storing the data in allocated memory using malloc.

    i have structure to store huge data form the serial port.
    Code:
    typedef struct FileData {		
    		unsigned char  *FRdata;
    		struct FileData *next;
    }FileData;
    
    /*I store the data in FileData->FRdata, by allocating sufficient memory to it*/
    
    Datacur->FRdata=(char *)malloc(sizeof(max_bytes));
    
    for(j=0;bytes_to_read>0;j++,bytes_to_read--)
    	Datacur->FRdata[j]=*Ftemp++;
    Datacur->next=NULL;
    i have to write the raw data from the serial port in to file after converting the raw into its format[ the data format is usually float type].

    when i print few data before opening a file
    Code:
    j=0;
    printf("read");
    while(j<10){
    printf("%02x ",Datahead->FRdata[j++]);
    }
    there is no change in the data .

    The problem is after , file is opened
    Code:
     fptr=fopen(Surveyname,"wt");
    the data in the allocated memory{Datahead->FRdata[} has changed { the data that is written to file is different to that of original data.}

    where cud be the malicious alteration hapened.

    any help on this.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    where cud be the malicious alteration hapened.
    In what way is it different?

    By the way, there is no need to cast the return value of malloc() in C.
    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

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Code:
    Datacur->FRdata=(char *)malloc(sizeof(max_bytes));
    You probably don't want a sizeof in there. You probably want just:
    Code:
    Datacur->FRdata = malloc(max_bytes);
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    11
    How silly i was !!!!!! Thanks iMalc for rightly pointing it out......

    problem got fixed!!
    Last edited by supi; 06-09-2008 at 02:09 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 03:45 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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