I'm trying to take parse the HTTP reply I get from uploading data to pachube.com. I'm using EhterCard.h and it has a function tcpReply() that returns the HTTP response, which is
According to strlen() it's 308 bytes long. I want to use strtok (using \n as delimiter) to parse each line, but first I'm using strncpy() to prevent a
a invalid conversion from 'const char*' to 'char*'
But my sketch seems to be failing at the strncpy() function and reboots before I even get to strtok(). Below is my code, can anyone tell me what I am doing wrong?
const char* reply = ether.tcpReply(session); // part of EtherCard.h library
if (reply != 0)
{
char reply2[strlen(reply)+1];
strncpy(reply2, reply, sizeof(reply)); // I think Arduino is rebooting here
reply2[strlen(reply)] = '\0';
char delims[] = "\n";
char *result = NULL;
result = strtok( reply2, delims );
while( result != NULL )
{
Serial.println(result);
result = strtok( NULL, delims );
}
}
I made this change but it still reboots. I don't know about running out of memory. How do I tell? When I compile it says Binary Sketch: 18,082 bytes (of a 30,720 byte maximum)
I don't know about running out of memory. How do I tell? When I compile it says Binary Sketch: 18,082 bytes (of a 30,720 byte maximum)
That only reports program (flash) memory - the amount of SRAM used is not reported.
My guess is that you have lots of strings consuming RAM.
But without seeing code, it really is just a guess.
I want to use strtok (using \n as delimiter) to parse each line, but first I'm using strncpy() to prevent a
a invalid conversion from 'const char*' to 'char*'
The proper cast would prevent the "problem" without the need to copy the string.
I want to use strtok (using \n as delimiter) to parse each line, but first I'm using strncpy() to prevent a
a invalid conversion from 'const char*' to 'char*'
The proper cast would prevent the "problem" without the need to copy the string.
Before I define reply2, I have less then 200 bytes. reply2 would be 308 bytes. I'd like not have to create reply2 and just use reply with strtok(), but reply is a const char and strtok() doesn't like that. So my solution is to strncpy reply to reply2.
I think I'm running out of ram when I define reply2 on line 451.
I would also like to free up some ram, but I'm not sure how to do that. Attached is the entire sketch, except for PachubeAPIKey.h which is just a #DEFINE with my API key (65 characters).
I'm using a Nanode, which is similar to Arduino Deumilanove.
PaulS:
I'll never understand why Microsoft invented a bunch of complex templates for casting, and why anyone uses them.
Just to set the record straight, the different cast types allow you to perform different types of casting as opposed to the C casting operation that strips all attributes from the cast variable. They also allow proper casting of polymorphic objects.
E.g. static_cast<char *>() will convert a (const void *) pointer to (const char *) pointer without stripping the const attribute. There is no way to do this with C casts unless you know that the original variable is const in every case to start with, and explicitly cast as (const char *).
There is no way to do this with C casts unless you know that the original variable is const in every case to start with, and explicitly cast as (const char *).
If you don't know what the original type is, casting is hardly a good idea. The reason for most casts is to allow a function to accept a const array, when you KNOW that the function is not going to change the contents of the array, but the function doesn't explicitly guarantee that by using the const keyword.
In the case that OP is talking about, I can't figure out why ether.tcpReply() returns a const pointer. Why does it care if you want to point it to something else, or alter the contents that it points to?