Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.pascal.delphi.misc > #735

Re: Strings and PChar (D4 Pro)

From "P E Schoen" <paul@pstech-inc.com>
Newsgroups comp.lang.pascal.delphi.misc
Subject Re: Strings and PChar (D4 Pro)
Date 2016-08-27 06:15 -0400
Organization A noiseless patient Spider
Message-ID <nprp91$trt$1@dont-email.me> (permalink)
References <nprn44$mvb$1@dont-email.me> <1rco3b1rvcu6.1k5cho7qohwwc$.dlg@40tude.net>

Show all headers | View raw


"JJ"  wrote in message news:1rco3b1rvcu6.1k5cho7qohwwc$.dlg@40tude.net...

[snip]
> GetCurrentDirectory() arguments are: the buffer pointer, then the buffer 
> length. Not the buffer length, then the buffer pointer.

From the Delphi help:

DWORD GetCurrentDirectory(

    DWORD nBufferLength,    // size, in characters, of directory buffer
    LPTSTR lpBuffer     // address of buffer for current directory
   );

> And, currDir variable was declarated as "array[0..50] of Char" which means 
> that it has a storage of 51 bytes plus one byte alignment padding (a total 
> of 52 bytes). The "sizeof(currDir)+1" expression would translate to "51+1" 
> which is 52 bytes. You're lucky you've declared currDir variable as an odd 
> length Char array. If you've declared it as an array with a length of 
> multiple of 4, there would be no padding following its storage. The data 
> following its storage might be used by other variable. So, if you use 
> "sizeof(currDir)+1" you may overwrite one byte which follows the actual 
> array - a byte outside of the array.

Yes, I probably should have used "sizeof(currDir)-1".

Also, perhaps I could use the return value of the function with a buffer 
size of zero. It would return the size needed, and then I could use alloc() 
to create the memory space to use. I just don't know what is the "best" way 
to handle functions like this, and I may have been using the PChar() cast 
improperly.

Thanks for the quick reply!

Paul 

Back to comp.lang.pascal.delphi.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Strings and PChar (D4 Pro) "P E Schoen" <paul@pstech-inc.com> - 2016-08-27 05:39 -0400
  Re: Strings and PChar (D4 Pro) "P E Schoen" <paul@pstech-inc.com> - 2016-08-27 06:03 -0400
    Re: Strings and PChar (D4 Pro) "P E Schoen" <paul@pstech-inc.com> - 2016-08-27 06:28 -0400
    Re: Strings and PChar (D4 Pro) JJ <jj4public@vfemail.net> - 2016-08-27 18:00 +0700
      Re: Strings and PChar (D4 Pro) Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2016-08-27 13:41 +0200
  Re: Strings and PChar (D4 Pro) JJ <jj4public@vfemail.net> - 2016-08-27 17:05 +0700
    Re: Strings and PChar (D4 Pro) "P E Schoen" <paul@pstech-inc.com> - 2016-08-27 06:15 -0400
      Re: Strings and PChar (D4 Pro) JJ <jj4public@vfemail.net> - 2016-08-27 18:09 +0700
      Re: Strings and PChar (D4 Pro) Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2016-08-27 13:12 +0200
        Re: Strings and PChar (D4 Pro) "P E Schoen" <paul@pstech-inc.com> - 2016-08-27 18:59 -0400
  Re: Strings and PChar (D4 Pro) Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2016-08-27 12:50 +0200

csiph-web