Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: random noise in record?

  1. #1

    random noise in record?

    OS: Windows XP
    IDE: Delphi2005

    On saving a packed record via an buffer to tfilestream i get some noise inside the record.

    TMyRecord = packed record
    name: string[255];
    someint: int64;
    end;
    PMyRecord = ^TMyRecord;

    system.New(myrecord);
    //fillchar(filerecord.name, 255,0);
    myrecord.name := 'name';

    When i leave out the fillchar part i get some random chars inside the name part of the record. Why? It even seems ot include some names i used on the form?

    I even have to use fillchar on the buffer i copy the record to.

    Why cannot delphi inialize memory cleanly?

    Thanks for your answers in advance.
    http://3das.noeska.com - create adventure games without programming

  2. #2

    random noise in record?

    This is something that bothers me too sometimes. When you allocate dynamic or static memory, the initial contents of your variable will be random.

    I can't really explain why the string contains component names. Probably because that memory is no longer in use. It's strange, because that data likely belonged to either delphi or your app, and both are still running.

    One thing i wanted to say is that it's VERY important that you initialize any return value's in your functions, because they will return random data when you leave Result uninitialized. Those bugs can be very hard to kill. :twisted:
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  3. #3

    Re: random noise in record?

    Quote Originally Posted by noeska
    Why cannot delphi inialize memory cleanly?
    That's not delphi's job, it's yours, memory manager's work is to allocate memory blocks asap, zerofilling them is unneccesary overhead in most cases, so it's your job to clear the memory that was allocated, ofcourse the memory allocated can sometimes contain garbage or parts of memory of whatever data that was allocated and deallocated there earlier. This is completely normal in other languages such as c / c++ as well.
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  4. #4

    random noise in record?

    what is a neat way of cleaning a record then?

    e.g. i now only have a fillchar(myrecord.name, 255,0); to do. But with a more complicated record i need many of these line and a fillchar on myrecord only is not valid.

    Hmm the situation is even worse:

    var
    MyRecord : TMyRecord;
    begin
    MyRecord.name := 'Test';

    When i pass this one on as @MyRecord to an buffer is contains garbage. Now how do i clean this one?
    http://3das.noeska.com - create adventure games without programming

  5. #5

    random noise in record?

    Is it in the file where you save the record you get random chars? If so then it makes sense. Your record is probably created on the heap which will most likely be filled with random garbage before the instance gets created. The memory manager will not fill the allocated pointer with zeroes unless you use a specific method(AllocMem, I think)

    There's probably no way around it. So just call FillChar(Myrecord, sizeof(myrecord), 0); first or initialize the variables
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  6. #6

    random noise in record?

    Why do you need to clear it in the first place? Isn't there an ending character or some first char indicating string lenght etc, don't know exactly how it works.

  7. #7

    random noise in record?

    if you do not save the complete record into a file you wont notice it. So you only need to clean them before usage if you want to save them in a file.
    http://3das.noeska.com - create adventure games without programming

  8. #8

    random noise in record?

    I know in memory there is noise, but how it actually effects the actual behavior of program did not come clear in this topic?

    'text'+#0+'skdfjlkj' or '4textsdlkfj' would still show as 'text' would it not? I don't have time to test it at the moment but you missed my question.

  9. #9

    random noise in record?

    like i said it does not affect usage. But saving the record into a file and opening that file in an hex editor you see the random noise. I dont like that. so i clean the records before usage and get nice clean files with records.
    http://3das.noeska.com - create adventure games without programming

  10. #10

    random noise in record?

    that depends types

    for exemple string[255] is an array of 256 char, a string type always contains a 0 terminal, so if your string is 50 long, theres one 0 after, and 205 char of random memory. when you read your record in a file, you obtain the original string just because there's a trailing 0.

    for an integer type, the size of an integer is fixed and know by advance, that's why you can write stream.write(i, sizeof(integer))

    i don't know if i'm understable...

Page 1 of 2 12 LastLast

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
  •