Sponsored Content
Top Forums Programming Creating an array to hold posix thread ids: Only dynamic array works Post 302238712 by kmehta on Sunday 21st of September 2008 05:36:06 PM
Old 09-21-2008
I am not sure I understand. But you were right, if the above code is placed in the main() function, it works fine, but if I place it in a separate function and call this function from main(), it gives the garbage output.

But I am still not quite sure why it shouldn't work. As you said, first example works fine becuase dynamically allocated threadids exists beyond this fucntion, but it second example it dies immediately. So, shouldnt I face a problem when I place the code directly in the main() function ?? The threadids could die immediately again before the thread is actually spawned, in which case it picks up a garbage output.

Thanks.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

creating a dynamic array in ksh

Hi, Is it possible to create a dynamic array in shell script. I am trying to get the list of logfiles that created that day and put it in a dynamic array. I am not sure about it. help me New to scripting Gundu (3 Replies)
Discussion started by: gundu
3 Replies

2. UNIX for Advanced & Expert Users

MAX SIZE ARRAY Can Hold it

Hi, Do anyone know what's the max size of array (in awk) can be store before hit any memory issue. Regards (3 Replies)
Discussion started by: epall
3 Replies

3. Shell Programming and Scripting

Dynamic Array Issue

Could one of you, please, provide some input regarding my problem below and it is as follows: I have 2 files that I need to make sure are identical before processing: First, I sort both files Second, I do a diff file1 file2 > File 3 This provides me with the difference. Now, I need to... (6 Replies)
Discussion started by: ddedic
6 Replies

4. Shell Programming and Scripting

How to hold string array in shell scripts

Gents, Below is the Shell script which I am trying to hold a string of array that is passed from a java file. But it is not working . Can any one please help me to by fixing it. #!/bin/csh/ set copy = ($argv) echo $copy >> /home/users/bavananr/rrr.log echo $copy >>... (3 Replies)
Discussion started by: brajesh
3 Replies

5. Shell Programming and Scripting

creating a dynamic array

i want to create an array the array elements are populated depending upon the number of entries present in a data file The data file is created dynamically how to achieve the same thanks (1 Reply)
Discussion started by: trichyselva
1 Replies

6. Programming

array dynamic allocation

Hi, I have the following problem: i must allocate a dynamic array from a subroutine which should return such array to main function. The subroutine has already a return parameter so i thought of pass the array as I/O parameter. I tried the following program but it doesn't work (segmentation... (11 Replies)
Discussion started by: littleboyblu
11 Replies

7. Shell Programming and Scripting

dynamic index for array in while loop

Hi, I'm just trying to use a dynamic index for some array elements that I'm accessing within a loop. Specifically, I want to access an array at variable position $counter and then also at location $counter + 1 and $counter + 2 (the second and third array positions after it) but I keep getting... (0 Replies)
Discussion started by: weak_code-fu
0 Replies

8. Programming

readdir and dynamic array memory corruption

Hi everyone I am developing an utility. At some part of it I read directory entries to a dynamic array: struct list It stores pointers to items: list.entries, which are structures: struct entry If a number of files in a directory is greater then number of elements an array was initially... (11 Replies)
Discussion started by: torbium
11 Replies

9. Programming

Memory corruption in dynamic array of strings

I put together a C function to add strings to a dynamic array of strings (mostly for educational purpose to explain pointers to my kid). It works, but sometimes one or two strings in the array becomes corrupted. Running example on 64 bit Ubuntu, gcc ver. 4.8.4 Hope my code is self-explanatory: ... (2 Replies)
Discussion started by: migurus
2 Replies

10. Shell Programming and Scripting

Creating a pseudo-array in dash, (POSIX).

Arrays in dash, (POSIX). Hi gurus... I am thinking of trying AudioScope.sh in pure POSIX so... I need an array in dash, I know it is not possible but pseudo-arrays are. I have two versions that work, the second is an idea from the WWW. The first is what I would like to use. There are... (8 Replies)
Discussion started by: wisecracker
8 Replies
Test::Refcount(3pm)					User Contributed Perl Documentation				       Test::Refcount(3pm)

NAME
"Test::Refcount" - assert reference counts on objects SYNOPSIS
use Test::More tests => 2; use Test::Refcount; use Some::Class; my $object = Some::Class->new(); is_oneref( $object, '$object has a refcount of 1' ); my $otherref = $object; is_refcount( $object, 2, '$object now has 2 references' ); DESCRIPTION
The Perl garbage collector uses simple reference counting during the normal execution of a program. This means that cycles or unweakened references in other parts of code can keep an object around for longer than intended. To help avoid this problem, the reference count of a new object from its class constructor ought to be 1. This way, the caller can know the object will be properly DESTROYed when it drops all of its references to it. This module provides two test functions to help ensure this property holds for an object class, so as to be polite to its callers. If the assertion fails; that is, if the actual reference count is different to what was expected, a trace of references to the object can be printed, if Marc Lehmann's Devel::FindRef module is installed. This may assist the developer in finding where the references are. See the examples below for more information. FUNCTIONS
is_refcount( $object, $count, $name ) Test that $object has $count references to it. is_oneref( $object, $name ) Assert that the $object has only 1 reference to it. EXAMPLE
Suppose, having written a new class "MyBall", you now want to check that its constructor and methods are well-behaved, and don't leak references. Consider the following test script: use Test::More tests => 2; use Test::Refcount; use MyBall; my $ball = MyBall->new(); is_oneref( $ball, 'One reference after construct' ); $ball->bounce; # Any other code here that might be part of the test script is_oneref( $ball, 'One reference just before EOF' ); The first assertion is just after the constructor, to check that the reference returned by it is the only reference to that object. This fact is important if we ever want "DESTROY" to behave properly. The second call is right at the end of the file, just before the main scope closes. At this stage we expect the reference count also to be one, so that the object is properly cleaned up. Suppose, when run, this produces the following output (presuming "Devel::FindRef" is available): 1..2 ok 1 - One reference after construct not ok 2 - One reference just before EOF # Failed test 'One reference just before EOF' # at demo.pl line 16. # expected 1 references, found 2 # MyBall=ARRAY(0x817f880) is # +- referenced by REF(0x82c1fd8), which is # | in the member 'self' of HASH(0x82c1f68), which is # | referenced by REF(0x81989d0), which is # | in the member 'cycle' of HASH(0x82c1f68), which was seen before. # +- referenced by REF(0x82811d0), which is # in the lexical '$ball' in CODE(0x817fa00), which is # the main body of the program. # Looks like you failed 1 test of 2. From this output, we can see that the constructor was well-behaved, but that a reference was leaked by the end of the script - the reference count was 2, when we expected just 1. Reading the trace output, we can see that there were 2 references that "Devel::FindRef" could find - one stored in the $ball lexical in the main program, and one stored in a HASH. Since we expected to find the $ball lexical variable, we know we are now looking for a leak in a hash somewhere in the code. From reading the test script, we can guess this leak is likely to be in the bounce() method. Furthermore, we know that the reference to the object will be stored in a HASH in a member called "self". By reading the code which implements the bounce() method, we can see this is indeed the case: sub bounce { my $self = shift; my $cycle = { self => $self }; $cycle->{cycle} = $cycle; } From reading the "Devel::FindRef" output, we find that the HASH this object is referenced in also contains a reference to itself, in a member called "cycle". This comes from the last line in this function, a line that purposely created a cycle, to demonstrate the point. While a real program probably wouldn't do anything quite this obvious, the trace would still be useful in finding the likely cause of the leak. If "Devel::FindRef" is unavailable, then these detailed traces will not be produced. The basic reference count testing will still take place, but a smaller message will be produced: 1..2 ok 1 - One reference after construct not ok 2 - One reference just before EOF # Failed test 'One reference just before EOF' # at demo.pl line 16. # expected 1 references, found 2 # Looks like you failed 1 test of 2. BUGS
o Temporaries created on the stack Code which creates temporaries on the stack, to be released again when the called function returns does not work correctly on perl 5.8 (and probably before). Examples such as is_oneref( [] ); may fail and claim a reference count of 2 instead. Passing a variable such as my $array = []; is_oneref( $array ); works fine. Because of the intention of this test module; that is, to assert reference counts on some object stored in a variable during the lifetime of the test script, this is unlikely to cause any problems. ACKNOWLEDGEMENTS
Peter Rabbitson <ribasushi@cpan.org> - for suggesting using core's "B" instead of "Devel::Refcount" to obtain refcounts AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.10.1 2011-04-25 Test::Refcount(3pm)
All times are GMT -4. The time now is 01:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy