Closed Thread Icon

Preserved Topic: Java / C comparison (copy problem) (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=21098" title="Pages that link to Preserved Topic: Java / C comparison (copy problem) (Page 1 of 1)" rel="nofollow" >Preserved Topic: Java / C comparison (copy problem) <span class="small">(Page 1 of 1)</span>\

 
WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 09-27-2002 23:13

In Java I could have a chunk of code as follows:

code:
char charArrayp[] = new char[20];
char charCopy[] = new char[20];

charCopy = copyString(charArray);

public static char[] copyString(char[] toCopy){
char tempArray = new char[20];
for(int i = 0; i< 20; i++) tempArray[i] = toCopy[i];
return tempArray;
}



This code would copy the array and return a new pointer for the new array to reference. When using C I am having a much harder time achomplishing this.

I could do something similar to:

code:
char charArray[20];
char charCopy[20];

stringCopy(charArray,charCopy);

void stringCopy(*string, *copy){
while(*string++ = *copy++)
;
}



Which would copy the contents of charArray to charCopy. However, the way I would like it to work would be that I could make my function:

code:
char stringCopy(char toCopy);



This way I could do something similar to the java code:

code:
char stringCopy = stringCopy(stringArray);



Anyone have any ideas?




[This message has been edited by WarMage (edited 09-27-2002).]

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 09-28-2002 00:19

[edit] it didn't work as it was supposed to I'll repost it as soon as I find out what is wrong with it [/edit]

Been looking at the code some more now and it turns out that I have no idea how to make it work , as far as I know it seems to be 'impossible'. The function needs to know where to store the new string. So if you don't send that into the function it has no way of knowing that and therefore doesn't work. But I'm pretty new at this stuff so I could be that I am way of target...
_________________________
Anyone who has lost track of time when using a computer knows the propensity to dream, the urge to make dreams come true and the tendency to miss lunch.
- copied from the wall of cell 408 -


[This message has been edited by Veneficuz (edited 09-28-2002).]

[This message has been edited by Veneficuz (edited 09-29-2002).]

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 09-28-2002 15:02

what are you trying to do, make a header or library or something?


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 09-29-2002 22:35

To do what you want to do in C:

code:
char stringCopy = stringCopy(stringArray);



you will need to allocate the space fot the new string in the function stringCopy. Something like this:

code:
char * stringCopy(char * string)
{
char * newString;


newString = malloc(length(string) + 1);
memcpy(newString, string, length(string)+1);

return(newString);
}



This is actually a very bad programming practice.

The probelm with this is that you have allocated memory within the function stringCopy and you depend on the programmer to remember to free this memory at some other place in the program. Of course, you don't want to
free the memory before you are through using it, but it should be freed once you no longer need it. This becomes a real problem when you write a program and it needs to be changed a year or two later by you or someone else. It leads to the problem called a "memory leak". A common problem with programs that allocate memory in a function and pass a pointer to the memory back to the calling function.



-- not necessarily stoned... just beautiful.

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 09-29-2002 22:40

I forgot to mention that the reason you can do the following in Java:

code:
newString = stringCopy(string)



is that Java performs "reference counting". That is, when you call stringCopy, Java increments a counter of the number of references to the string. As you make more references to the string Java continues to increment the counter. As each of these references move out of scope, Java decrements the reference counter and automatically frees the memory used by the string when the reference counter drops to zero.

Since there is no reference counting C, you mus keep track of all refernces to a string and must remember to release the memory used by the string when all references to the string have gone out of scope.

-- not necessarily stoned... just beautiful.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 10-01-2002 15:20

THank you for the great answer. It went a ways into helping me understand pointers in C and how memory works. C is quite different than what I am used to programming in. All methods are pretty much like static methods. It can get a bit confusing.

Thanks,

« BackwardsOnwards »

Show Forum Drop Down Menu