dup


Description:

[ CCode ( cname = "g_memdup" ) ]
public void* dup (void* mem, uint n)

Allocates byte_size bytes of memory, and copies byte_size bytes into it from mem.

Note:

Use dup2 instead, as it accepts a size_t argument for byte_size, avoiding the possibility of overflow in a size_tuint conversion

If mem is null it returns null.

Example: Copy n bytes:

public static int main (string[] args) {
char[] data = {'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\0'};

// Copy the data into a typeless buffer:
// Do not copy arrays / classes / etc when you do not know what you are doing.
// You may break your ref counters / lose array length information / etc
void* copy = Memory.dup (data, (uint) (sizeof (char)*data.length));
print ("%s\n", (string) copy);
free (copy);

return 0;
}

valac --pkg glib-2.0 GLib.Memory.dup.vala

Parameters:

mem

the memory to copy.

byte_size

the number of bytes to copy.

Returns:

a pointer to the newly-allocated copy of the memory, or null if mem is null.


Namespace: GLib.Memory
Package: glib-2.0



2022 vala-language.org