how to access higher char array element?

M

mwebel

Hi!,
im trying to copy the middle part of a dinamycally created char* string
with memcopy but all i get is rubbish...
I understand, that malloc can allocate memory wherever it wants and it
does not have to be in one piece... but how do i access higher parts of
the allocated memory?

lets say source contains "helloworld" and i only want to have "llowo"
in destination... what do i do?

char* destination=malloc(5);
char* source=malloc(8);
[.....]

memcpy(destination, source +3 ,5);

this line copies whatever.... and writing source[3] does not work
either... a hint would be nice... :D
thanks for any help!
 
R

Richard Heathfield

(e-mail address removed) said:
Hi!,
im trying to copy the middle part of a dinamycally created char* string
with memcopy but all i get is rubbish...

I'm not familiar with memcopy. Do you mean memcpy?
I understand, that malloc can allocate memory wherever it wants and it
does not have to be in one piece...

You understand wrongly. Each call to the malloc function yields either a
null pointer (in the case of failure) or a pointer to a contiguous region
of memory of at least the size requested.
lets say source contains "helloworld" and i only want to have "llowo"
in destination... what do i do?

char* destination=malloc(5);
char* source=malloc(8);

The region pointed to by source does not have sufficient storage to store
the string "helloworld". It would require 11 bytes, not 8.
[.....]

memcpy(destination, source +3 ,5);

this line copies whatever.... and writing source[3] does not work
either... a hint would be nice... :D

It is probable that you are forgetting about the importance of a null
terminator, but we can't tell from what you've shown so far. Post the
smallest *complete* C program that demonstrates the difficulty you are
having, and I have little doubt that we will be able to point out the
problem.
 
M

markpapadakis

Hi!,
im trying to copy the middle part of a dinamycally created char* string
with memcopy but all i get is rubbish...
I understand, that malloc can allocate memory wherever it wants and it
does not have to be in one piece... but how do i access higher parts of
the allocated memory?

lets say source contains "helloworld" and i only want to have "llowo"
in destination... what do i do?

char* destination=malloc(5);
char* source=malloc(8);
[.....]

memcpy(destination, source +3 ,5);

this line copies whatever.... and writing source[3] does not work
either... a hint would be nice... :D
thanks for any help!

Perhaps I am misunderstanding your intentions here, but why something
like:

char *destination = (char *)malloc(6), *source =
strdup("helloworld");


memcpy(destination, source + 2, 5);
destination[5] = '\0';

won't work for you?
Perhaps you need to consider the following:
o you need to allocate length + 1, because the string is null
terminated
o offsets base is 0, not 1. Thus you should memcpy() from source + 2
o you need to set the '\0' on the destination ( null character
terminator ).

Mark Papadakis
 
M

Martin Ambuhl

Hi!,
im trying to copy the middle part of a dinamycally created char* string
with memcopy but all i get is rubbish...
I understand, that malloc can allocate memory wherever it wants and it
does not have to be in one piece... but how do i access higher parts of
the allocated memory?

lets say source contains "helloworld" and i only want to have "llowo"
in destination... what do i do?

char* destination=malloc(5);
Obviously wrong. There are 6 chars in "llowo".
char* source=malloc(8);
Obviously wrong. There are 11 chars in "helloworld".
[.....]

memcpy(destination, source +3 ,5);
Obviously wrong. The chars {'l','l','o','w','o'} begin at source+2.
this line copies whatever.... and writing source[3] does not work
either... a hint would be nice... :D
"Does not work" doesn't work as a description. Below you will see a
demonstration that everything you claim "does not work" does work when
done correctly.


Are you perhaps trying to use the non-string array
{'l','l','o','w','o'} as if it were a string?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
char source[] = "helloworld";
char *dest;
size_t i;

printf("Copying the 5 chars from \"%s\"[2]\n", source);
if (!(dest = malloc(5))) {
printf("malloc failed.\n");
exit(EXIT_FAILURE);
}
memcpy(dest, source + 2, 5);
for (i = 0; i < 5; i++)
printf("%u %#3o %c\n", (unsigned) i, (unsigned) dest,
dest);
putchar('\n');
free(dest);

printf("Creating a string using the 5 chars from \"%s\"[2]\n",
source);
if (!(dest = malloc(6))) {
printf("malloc failed.\n");
exit(EXIT_FAILURE);
}
memcpy(dest, source + 2, 5);
dest[5] = 0;
for (i = 0; i < 6; i++) {
if (dest)
printf("%u %#3o %c\n", (unsigned) i, (unsigned) dest,
dest);
else
printf("%u %#3o %s\n", (unsigned) i, (unsigned) dest,
"(zero byte)");
}
printf("The created string: \"%s\"\n", dest);
free(dest);

return 0;
}


Copying the 5 chars from "helloworld"[2]
0 0154 l
1 0154 l
2 0157 o
3 0167 w
4 0157 o

Creating a string using the 5 chars from "helloworld"[2]
0 0154 l
1 0154 l
2 0157 o
3 0167 w
4 0157 o
5 0 (zero byte)
The created string: "llowo"
 
M

Martin Ambuhl

markpapadakis said:
Perhaps I am misunderstanding your intentions here, but why something
like:

char *destination = (char *)malloc(6), *source =
strdup("helloworld");

If his intention is to write standard C, then there is a good reason not
to use the above: strdup does not exist in C and if it ever does there
is no guarantee that the standard strdup will behave like yours.
 
M

mwebel

Martin Ambuhl schrieb:
[a nice example]


Thanks to all who answered...
indeed the error came from me missallocating the space and trying to
read from non allocated memory! i also didnt take into account the last
0...

indeed im writing in c in windows.
I actually do c++ and had to do this one thing for compatibiltys sake
with a programm im using.

thanks again to all!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top