Stack & realloc C ++ question

int main()
{
   char myString = NULL;
   realloc(&myString, 5);
   strncpy((char *)&myString, "test", 5);
}

Fine seems to work, but I'm still a bit confused on the stack against the heap, is this allowed? Should I release myString manually or release it when it goes out of scope?


Edit: Thanks for the answers, so I assume this is illegal.

//I want the code to change myString to "tests"
char myString[5] = "test";
realloc(&myString, strlen(myString)+2);
myString[4] = 's';
myString[5] = '\0';
+3
source share
9 answers

No, this is completely wrong. realloc should only be used to reallocate the memory allocated by malloc, what you do only works by accident, and ultimately the crash will happen terribly

char *myString = malloc(x);
myString = realloc(myString,y);
free(myString)

You better use new and delete, and even better use std :: string.

+18
source

:

  • , , , malloc realloc, C.
  • , char * myString, char. ( char) .
  • myString char NULL realloc.
  • 4 strncpy not 5, , .
  • ,
  • realloc. realloc()

[ realloc:] 0, realloc() (, ) . 0, free() . , realloc() errno [ENOMEM].

  • re-alloc malloc NULL:

ptr , realloc() malloc() .

++ :

++, ++. , ( ).

char *myString = new char[5];
strncpy(myString, "test", 4); 
//...
delete[] myString;

:

#include <string>

//...

std::string str = "test";

2

+8

. -, . , , - malloc realloc, .

: - - -, . , realloc - realloc - , . :

char* ptr = malloc(4);
ptr = realloc(ptr, 5);

realloc ptr , ptr , , .

+3

! . - , main(), -, main(). .

. realloc, . - memcpy (& myString).

int dostuff();

int main()
{
        dostuff();
        return 0;
}

int dostuff()
{
        char myString = NULL;
        realloc(&myString, 5);
        strncpy((char *)&myString, "test", 5);
        return 0;
}
+2

, . () realloc() undefined, ( ) ( ), , . , AV. , .

: , . .

+2

++, undefined, . , .

, , . , , . , , , .

realloc(), . , realloc() , . , realloc() , .


, .


char myString = NULL;

char, . C char*, char.

, char NULL, , . , NULL 0. , char, , , C.


realloc(&myString, 5);

, , . .

, . realloc() , . , , . NULL, realloc() , .


strncpy((char *)&myString, "test", 5);

, .


:


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

int main()
{
   /* allocate space for, say, one character + terminator */
   char* myString = (char*) malloc(2);

   /* some code using myString omitted */

   /* get more space */
   myString = (char*) realloc(myString, 5);

   /* write to the string */
   strncpy(myString, "test", 5);

   /* free the memory */
   free(myString);

   return 0;
}

++ realloc(). , - :


#include <string>

int main()
{
   std::string myString;

   /* some code using myString */

   myString = "test";

   return 0;
}
+1

myString, ( "" ).

realloc , NULL , realloc, malloc calloc.

, , , :

int * x;

x ! pointer .

x = (int *) malloc (sizeof (int));

, malloc x! x !

0

, , , -, . myString char . .

realloc() -, . ( , ) .

, -, malloc() realloc() calloc(), .

-

char * myString = NULL;
myString = realloc(myString, 5);

, () myString.

++, , std::string.

0

:

, . myString malloc ( calloc), realloc .

In addition, realloc does not accept a pointer to a pointer as the first argument. It takes a pointer to the allocated memory and returns another (possibly different) pointer. Instead, write a call:

myString = realloc(myString, strlen(myString)+2);
0
source

Source: https://habr.com/ru/post/1704830/


All Articles