Welcome! Log In Create A New Profile

Advanced

How to copy data from a pointer into a char not another pointer

Posted by SteelSLasher 
How to copy data from a pointer into a char not another pointer
July 20, 2009 08:44PM
When i use strcpy or strncpy with the destination as a char, and not the pointer variant, and the source pointer is free'd and nulled at a later point after which i attempt to use the destination char an exception is thrown at me.

So what i need is a function that does soemthing similar to strcpy but does not use pointers to copy a string, i want it to physically copy the contents of the memory block which the pointer points to into another variable.
Re: How to copy data from a pointer into a char not another pointer
July 20, 2009 09:12PM
I think you are looking for memcpy. I don't know if it exist in libogc? If yes check this: [msdn.microsoft.com]
Re: How to copy data from a pointer into a char not another pointer
July 20, 2009 10:05PM
yes memcpy is standard.

But I'm not really sure what you're trying to do here. memcpy will pretty much provide the same results as strcpy. The only difference is strcpy looks for the null terminator.

The only way you'd be getting an exception is if you're copying more than the source buffer is allocated for in this case memcpy will also crash. Also don't forget to add one byte as the NULL terminator when dealing with strings!

eg. These all produce the same result.

char source[] = "HELLO";
char dest[6] = {0};

strcpy(dest,source);
strncpy(dest,source,5);
memcpy(dest,source,5);

Re: How to copy data from a pointer into a char not another pointer
July 21, 2009 11:17AM
nothing is crashing, basically all that happens is that i copy the string and when the source pointer is nulled that char in which the string is copied to is also nulled for some reason, will try using memcpy
Re: How to copy data from a pointer into a char not another pointer
July 21, 2009 03:11PM
Here is the code that does what you suposedly claim that you want:
char *string="no idea";
char X;
x=*string;
This will copy the first byte of the string into the variable X.

I am 100 % sure that you are confused so badly that you are not suitable as a programmer. For all of us sake, pick another hobby, like painting. You see, when painting, logic and impossibilities doesn't matter.
Re: How to copy data from a pointer into a char not another pointer
July 21, 2009 07:04PM
Quote
SteelSLasher
nothing is crashing, basically all that happens is that i copy the string and when the source pointer is nulled that char in which the string is copied to is also nulled for some reason, will try using memcpy

If the source pointer is null then it will always crash. You need to find out why it's being nulled. Can you point us to the source or insert a snippet here.
Re: How to copy data from a pointer into a char not another pointer
July 21, 2009 09:20PM
[onsinch.pastebin.com]

I would put it in SVN but i promised the next revision would be a stable one

Also, using the debug data outputted from my app the source pointer is not null infact it is correct since the last debug message recieved is the "Location" header after which the app crashes and also in previous runs the destination would output fine after strcpy but is somehow NULLed later on.

Using this information i can conclude that instead of copying the data a char is somehow turned into a char pointer by force which then returns null at a later point since the source is nulled



Edited 1 time(s). Last edit at 07/21/2009 09:23PM by SteelSLasher.
Re: How to copy data from a pointer into a char not another pointer
July 21, 2009 09:59PM
A couple of questions.

Where is the variable "redirect" defined and how. What are you expecting to be copied to "redirect"
Re: How to copy data from a pointer into a char not another pointer
July 22, 2009 11:17AM
char * redirect;

Its declared straight after the includes, if it was not declared it would cause a compile error


If the second line is "Location: [www.google.co.uk]"
I would expect redirect to be "http://www.google.co.uk/", just to point out that this actually does work when using strcpy

here is the entire script [onsinch.pastebin.com]



Edited 1 time(s). Last edit at 07/22/2009 11:19AM by SteelSLasher.
Re: How to copy data from a pointer into a char not another pointer
July 22, 2009 01:13PM
As far as I see it, "redirect" always points to NULL. And thus your memcpy, or strcpy towards it should always crash. You never create a redirect buffer to put data in.
What you want (I think) is this:

redirect = strdup(line+10);
Debugs(redirect);

This creates a new string buffer, with the contents of line+10, and then later, when you're done with it, free it.

I think you lack quite a bit of knowledge about C and strings in C. (I spot lots of ways that your code will crash) Nothing wrong with that, you'll learn as you go.
Re: How to copy data from a pointer into a char not another pointer
July 22, 2009 06:34PM
Quote
SteelSLasher
char * redirect;

Its declared straight after the includes, if it was not declared it would cause a compile error


If the second line is "Location: [www.google.co.uk]"
I would expect redirect to be "http://www.google.co.uk/", just to point out that this actually does work when using strcpy

here is the entire script [onsinch.pastebin.com]

Yes char* redirect creates a pointer to a char and not what you want. If you don't know the value of l-10 then you need to allocate before you copy to redirect. As you're using redirect in the scope of the entire file it's hard to say where you'd deallocate. That's one advantage to c++ you've got a constructor and destructor, you'd be able to deallocate in the dtor.

If you're confused by memory allocation then start off by using an array and do a size check before you mem/str copy.

#define BUFFER_SIZE (100)
char redirect[BUFFER_SIZE] = {0};
....
....
if (l-10 < BUFFER_SIZE-1 && l-10 > 0)
  memcpy(redirect, line+10, l-10);



Edited 1 time(s). Last edit at 07/22/2009 08:39PM by scanff.
Re: How to copy data from a pointer into a char not another pointer
July 22, 2009 06:47PM
Quote
Daid
I think you lack quite a bit of knowledge about C and strings in C. (I spot lots of ways that your code will crash) Nothing wrong with that, you'll learn as you go.

yeah, i am used to coding in php (i even did compiled php to make things easier) and kept my php habits to make coding in c and c++ less daunting.

the other lines that may cause crashes havent been fixed because the app hasnt yet gotten to those parts yet

thanks though
Re: How to copy data from a pointer into a char not another pointer
July 22, 2009 08:28PM
what you are missing is a bit of infos about pointers and how they work:

think about pointer as a memory address for a data buffer of unknown size, not as the data itself.

memory must ALWAYS be allocated before you can read or write it, you can do this either statically by declaring the variable or dynamically by using malloc (or similar functions)

however, in the case of pointer, declaring it only create the variable that holds the memory address so by default it points to NULL memory, which means unallocated memory. Accessing unallocated memory (or illegal address) generally lead to exception crash.

now, when you know the size you gonna need to store the data, you can statically declare the data buffer by doing something like this:

char data;

which is quite equivalent as:

char *data = malloc(size * sizeof(char));

The difference is that in the later case, data is allocated in a specific memory region and will remain so until you free it, whereas in the first case, data will be allocated in the stack and automatically cleared once you leave the function that made the declaration.

Once you are done, you can always refer as a pointer to the allocated memory by using the "data" variable in any functions (strcpy,memcpy,...) requiring pointers.

When you are using these functions, there are basically only 2 things you need to ask yourself:

1/ does the pointer(s) address allocated memory ?
2/ is the destination size enough to hold the source data ?
Re: How to copy data from a pointer into a char not another pointer
July 22, 2009 10:15PM
Quote
ekeeke
by default it points to NULL memory

MAYBE, if it is declared as a global or a static variable.
DEFINITLY NOT, if it is a local variable.

To be sure, use char *blablabla = 0;

(It is definitly not a bad practice to initilaize all varibales anyway)


Note: I am talking C here, I am not sure about it on C++.



Edited 2 time(s). Last edit at 07/22/2009 10:16PM by daniel_c_w.
Re: How to copy data from a pointer into a char not another pointer
July 22, 2009 10:18PM
Quote

Note: I am talking C here, I am not sure about it on C++.

It's the same in c++, pointers are not null by default so it's always a good idea to null them.
Re: How to copy data from a pointer into a char not another pointer
July 23, 2009 02:01AM
Pointers (and everything else) is defined to not have a defined initial value unless you provide one. They can and will have any value.
Re: How to copy data from a pointer into a char not another pointer
July 23, 2009 06:33AM
Quote
henke37
Pointers (and everything else) is defined to not have a defined initial value unless you provide one.

Actually global variables are guaranteed to be initialized with zeros (bitwise). The same should be the case for local static variables.
I am not quite sure what that does mean for pointers. Are they filled with zero bits, or are they guaranteed to be Nullpointers?



Edited 1 time(s). Last edit at 07/23/2009 06:33AM by daniel_c_w.
Re: How to copy data from a pointer into a char not another pointer
July 23, 2009 11:03AM
strdup works fine, the script now successfully redirects the user if the "Location: ..." header is found, the problem now is that the redirected request (which should be a "200" request) returns garbage for the webpage, i am guessing this is a null but i cant put my fingure on why this is happening.

btw i read [www.eskimo.com] before this convo and it kind of helped a lot

for the current problem (which is probably pointer related) i updated the SVN since its stable and runs fine but doesnt actually download yet (although it used to download which is quite odd)

[code.google.com] goes to the if loop i am refering to
Re: How to copy data from a pointer into a char not another pointer
July 23, 2009 04:47PM
Be sure to dump all headers that you get. (I got a bit of experiance with HTTP) Chances are that you get a gzip compressed stream back. And some servers always send chunked transfers, even if you say HTTP1.0

I have some java HTTP download code if you are interrested?
Re: How to copy data from a pointer into a char not another pointer
July 23, 2009 06:40PM
I'm sure there's a way to request non-compressed. If you have not looked at the http 1.1 spec here's a link [www.w3.org] - look at "content-coding".

Also I'd recommend trying it on another site to verify it is returning a 200 and it's not something with the code.
Sorry, only registered users may post in this forum.

Click here to login