Recent

Author Topic: problem with shortstring assignment  (Read 8489 times)

ajosifoski

  • Jr. Member
  • **
  • Posts: 55
problem with shortstring assignment
« on: October 02, 2012, 06:22:53 pm »
{$H+}

var arData[1..1000000, 1..1000] of shortString;
      s : shortString;
// some code to demonstrate the problem
begin
      s:='abc';
      arData[1,1]:=s;
      showmessage(s);
end.

can't compile

Main problem is that I want to maximize arData rank, and if use AnsiString there are not problems at all, but on some numbers arrive problems with memory.
So with shortStrings, but assignment how?

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: problem with shortstring assignment
« Reply #1 on: October 02, 2012, 06:49:56 pm »
You're claiming 256 GB static data (1000000 * 1000 * 256 bytes) with ShortString.

Bart

ajosifoski

  • Jr. Member
  • **
  • Posts: 55
Re: problem with shortstring assignment
« Reply #2 on: October 02, 2012, 06:59:10 pm »
Actually program will compile with larger numbers from 1000000 and 1000 but generates run time error when assigning shortstring to some variable from array

Tnx for notifying me about fact that shorstrings use static data, how can be problem be solved dinamically?

goal is to have that matrix with maximum rank and to only use ansistrings

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: problem with shortstring assignment
« Reply #3 on: October 02, 2012, 08:15:13 pm »
based on your code I assume that the test is mend to be a console application in which case showmessage can not be used replace it with writeln(s) instead and see if that solves your problem.

Also saying that you have problems with ansi string with out specifying
1) operating system and 32bit or 64
2) the exact problem you have
3) lazarus version
4) FPC version

There not much we can do but assume what wend wrong. In this case I'm assuming that the data that are coming in are not in ansi but some obscure DOS code page and you need to OEMToAnsi or something along those lines in windows at least.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

ajosifoski

  • Jr. Member
  • **
  • Posts: 55
Re: problem with shortstring assignment
« Reply #4 on: October 02, 2012, 08:27:47 pm »
Ubuntu 12.04 /32b
FPC 2.4.4-3.1
Lazarus: 0.9.30.2-2

problem is getting maximum from memory to work with strings
or let say Maximizing matrix array of strings

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: problem with shortstring assignment
« Reply #5 on: October 02, 2012, 10:15:54 pm »
You say data is numbers, floating point? Can you change array type to single? This would only use 4 bytes per data, whereas string type reserves much more. You would get off with only 4GB memory reservation.

Also, i would do it dynamically anyway:
Code: [Select]
var arData of Something...

  setlength(arData, 1000000*1000);
Should be more reliable than using stack.

ajosifoski

  • Jr. Member
  • **
  • Posts: 55
Re: problem with shortstring assignment
« Reply #6 on: October 02, 2012, 10:25:16 pm »
all Data are strings

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: problem with shortstring assignment
« Reply #7 on: October 03, 2012, 08:57:38 am »
are you still not able to compile?

Code: Pascal  [Select][+][-]
  1. {$H+}
  2.  
  3. var arData : array[1..1000000, 1..1000] of shortString;
  4.       s : shortString;
  5. // some code to demonstrate the problem
  6. begin
  7.       s:='abc';
  8.       arData[1,1]:=s;
  9.       writeln(s);
  10. end.
  11.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

ajosifoski

  • Jr. Member
  • **
  • Posts: 55
Re: problem with shortstring assignment
« Reply #8 on: October 03, 2012, 09:07:48 am »
sorry, problem was not with compiling, but with runtime error with shortstring assignment

{$H+}
 
var
    somearray : array [1..1000, 1..1000] of shortstring; // or string[20]
    s : shortstring; // string[20]
begin
 // somecode
 somearray[1,1]:=s;  // here is the problem as topic describes

end.

even I declare same type sometype=string[20] and use it for declaring shortstrings problem is same
« Last Edit: October 03, 2012, 09:47:35 am by ajosifoski »

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: problem with shortstring assignment
« Reply #9 on: October 03, 2012, 12:26:16 pm »
Compiles and rusn just fine:

Code: [Select]
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>type test.pas
program test;
{$H+}
var
    somearray : array [1..1000000, 1..1000] of shortstring;
    s : shortstring;
begin
 s := 'foo';
 somearray[1,1]:=s;  // here is the problem as topic describes
 writeln(somearray[1,1]);
end.
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>fpc test.pas
Free Pascal Compiler version 2.6.0 [2011/12/25] for i386
Copyright (c) 1993-2011 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling test.pas
Linking test.exe
13 lines compiled, 0.1 sec , 26000 bytes code, 1628 bytes data
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
foo

Bart
« Last Edit: October 03, 2012, 12:27:47 pm by Bart »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: problem with shortstring assignment
« Reply #10 on: October 03, 2012, 12:39:08 pm »
Compiles and rusn just fine:

try this one
Code: [Select]
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>type test.pas
program test;
{$H+}
var
    somearray : array [1..1000000, 1..1000] of shortstring;
    s : shortstring;
begin
 s := 'foo';
 somearray[1,1]:=s;
 somearray[10,1]:=s;
 somearray[100,1]:=s;
 somearray[1000,1]:=s;
 somearray[10000,1]:=s;
 somearray[100000,1]:=s;
 somearray[1000000,1]:=s;
 writeln(somearray[1,1]);
 writeln(somearray[10,1]);
 writeln(somearray[100,1]);
 writeln(somearray[1000,1]);
 writeln(somearray[10000,1]);
 writeln(somearray[100000,1]);
 writeln(somearray[1000000,1]);
end.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: problem with shortstring assignment
« Reply #11 on: October 03, 2012, 03:54:26 pm »
Yep, that one crashes.
a 1000 x 1000 array seems to go OK.
10000 x 1000 array wants > 2 GB, which I think a single proces on windows cannot have?

Bart

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: problem with shortstring assignment
« Reply #12 on: October 04, 2012, 06:55:06 am »
Quote
10000 x 1000 array wants > 2 GB, which I think a single proces on windows cannot have?
It's implementation limitation AFAIK, the maximum data element (total) may not exceed 2GB.

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: problem with shortstring assignment
« Reply #13 on: October 04, 2012, 11:54:19 am »
It's implementation limitation AFAIK, the maximum data element (total) may not exceed 2GB.
Shouldn't the compiler give a warning or an error then?

Bart

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: problem with shortstring assignment
« Reply #14 on: October 04, 2012, 10:49:09 pm »
Quote
Shouldn't the compiler give a warning or an error then?
Looking at the generated code it seems that when the array is too big, it's no longer put into stack, but (should be) generated dynamically on the heap. However, I can't seem to find any dynamic memory allocation in the generated code. Could be a compiler bug.

 

TinyPortal © 2005-2018