Go Down

Topic: Do I need a special library for strcpy and strcat (Read 17013 times) previous topic - next topic

pierrot10

Dear PaulS,

It's interesting what you stated

So if I understood, it is not enought to just return a array, which has been declared and "filled" into an function. It's because it has been declare at the top of the script, we have to use malloc(). That right?

Then I should declare coords in that way (in the function)

Code: [Select]
char * coords;
  coords = (char*) malloc (100+1);

instead of
Code: [Select]
char coords[100];

May I ask you to correct me or show me an exemple, if I am wrong (I found this, wuth Google, so not sure)?

Many thank
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

PaulS

Quote
Then I should declare coords in that way (in the function)

Yes. just remember that you need to free the memory that malloc() allocates, at some point when you are done with it.

Also remember that the Arduino does not have a lot of memory, and malloc() can use that all up in a heartbeat.
The art of getting good answers lies in asking good questions.

AWOL

Quote
So if I understood, it is not enought to just return a array, which has been declared and "filled" into an function. It's because it has been declare at the top of the script, we have to use malloc(). That right?

I'm a bit lost now but unless an array declared within a function is qualified "static", you should never return a pointer to it or any part of it.

This is bad:
Code: [Select]
char* myFunction ()
{
 char myArray [20];
 // do stuff with myArray
 return myArray;
}


This may be OK
Code: [Select]

char* myFunction ()
{
 static char myArray [20];
 // do stuff with myArray
 return myArray;
}


This is OK, but probably pointless
Code: [Select]
char myArray [20];
char* myFunction ()
{
 // do stuff with myArray
 return myArray;
}

pierrot10

#48
Jul 15, 2013, 04:00 pm Last Edit: Jul 15, 2013, 04:02 pm by pierrot10 Reason: 1
Ok this is interrestinf but when I add "static", now it shoe an error

Code: [Select]

char* myFunction ()
{
 
int le = strlen(lo)+strlen(la)+strlen(al)+strlen(ti)+strlen(ve)+strlen(en)+strlen(lon)+strlen(lat)+strlen(alt)+strlen(vel)+strlen(time);
  /*
  char * coords;
  coords = (char*) malloc (le+1);
*/
 
  static char coords[le];
//do stuff with coords
return coords;
}

Quote
storage size of 'coords' isn't a constant


I am a bit afraid because 'le', should not be a constant because it may change each period of time..  :.
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

AWOL

If something is static, its size needs to be known at compile-time.
So, you need to figure out what the biggest value "le" can ever be, and size your array accordingly.

pierrot10

I really because creasy and I think I have a lot of difficultire to understand char variable.

I rwrite my code because I make it to complicate with the getGPSCoords function. I deleted it.

I make it more simple, but my code stop responding for a reason I totaly ignore

When I enter 'send' in my terminal, the serial.println(coords); displays the right value
Quote

lo=608.482777&la=4613.163541&al=459.471680&ti=20130715215238&ve=0.000000

but when I comment the Serial.println(coords) and I uncomment the next line
Code: [Select]

//Serial.println(coords);
Serial.println(strlen(coords));

My code stop before Serial.println(strlen(coords));
Here is my code
Code: [Select]

    // SEND DATA
    if(!strcmp(inSerial,"send")){
        #ifdef DEBUG
          Serial.println(F(""));
          Serial.println(F("SENDING COORDS TO THE REMOTE SERVER"));
          Serial.println(F("---------------------------------------"));
        #endif
       
        #ifdef DEBUG
          stat=gps.getStat();
          if(stat==1){
            Serial.println(F("NOT FIXED"));
          }else if(stat==0){
            Serial.println(F("GPS OFF"));
          }else if(stat==2){
            Serial.println(F("2D FIXED"));
          }else if(stat==3){
            Serial.println(F("3D FIXED"));
          }
          delay(5000);
        #endif   
 
        //Get data from GPS
 
        gps.getPar(lon,lat,alt,time,vel);
     
       char coords[96];
       
        strcpy(coords,lo);          //3
        strncat(coords,lon,15);    //18

        strcat(coords,la);         //22
        strncat(coords,lat,15);    //37
 
        strcat(coords,al);         //41
        strncat(coords,alt,15);    //56

        strcat(coords,ti);         //60
        strncat(coords,time,14);   //74
 
        strcat(coords,ve);         //78
        strncat(coords,vel,15);    //93
 
        strcat(coords,en);       //95
       
        Serial.println(coords);
        //Serial.println(strlen(coords));
        /*
        inet.httpPOST(host, port_post, path_post, coords, "Result", 50);
        */
        delay(10000);
       
        enterCommand();
    }


Someone can tell me why??????

I hope after I will leave you "alone"  :)
Thank for all
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

PaulS

Quote
Someone can tell me why??????

I think that it is probably because you are running out of memory,,,,,,, but it is impossible to tell because you won't post all of your code........

Code: [Select]
       char coords[96];
       
        strcpy(coords,lo);          //3
        strncat(coords,lon,15);    //18

        strcat(coords,la);         //22
        strncat(coords,lat,15);    //37
 
        strcat(coords,al);         //41
        strncat(coords,alt,15);    //56

        strcat(coords,ti);         //60
        strncat(coords,time,14);   //74
 
        strcat(coords,ve);         //78
        strncat(coords,vel,15);    //93
 
        strcat(coords,en);       //95
       
        Serial.println(coords);

There is NO reason to waste 96 bytes of memory. Print each piece of the string to the serial port.
The art of getting good answers lies in asking good questions.

pierrot10

#52
Jul 16, 2013, 05:38 pm Last Edit: Jul 16, 2013, 05:42 pm by pierrot10 Reason: 1
Ok,
But how can I check the memory and make free it? How can controle or the prevent memory usage?

I have the same problem when I enter into the ter,imal 'dirr' (see at "READING PHONE DIRECTORY");, it only display the fisrt phone and then nothing happen else.

Thank a  lot for your help

Here is my full code:
In red is where the code stop
http://www.hello-web.net/temp/forum.html
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

acboother

I have suggested in an email to you that a little bit of background reading/work on memory and strings will speed you along and you may even be able to answer your own questions and/or ask more appropriate ones here in the forum.

Sorry to sound so negative but the help you seem to be getting and the questions you ask indicate the 'gap' in your knowledge that needs to be filled in a bit more by you to understand the advice you are getting.

PaulS

Quote
But how can I check the memory

http://playground.arduino.cc/Code/AvailableMemory

Quote
and make free it?

Size arrays correctly. Don't create unnecessary ones. Use the F() macro to keep string literals out of SRAM where possible.

Some places you missed:
Code: [Select]
Serial.println("\nstatus=READY\n");
Serial.println("\nstatus=IDLE");
Serial.println("Waiting for 20sec before trying \nto attach GSM\n");


After
that
I
gave
up
on
reading
your
poorly
formatted
code.
The art of getting good answers lies in asking good questions.

Go Up