1

I have a string in my main functions.

I need to send it to other functions and make malloc to new string with the same size of the original.

I try something like this:

#define SIZE_STRING 100
typedef char string[SIZE_STRING];

char *testCode(string str)
{   
    char *new_str;
    int limit = strlen(str);
    int new_limit;
    new_str = (char*)calloc(limit, sizeof(char));
    new_limit = strlen(new_str);
    printf("%d", new_limit);

    return new_str;
}

void main()
{
    char str[SIZE_STRING] = { "BLA BLA BLA" };
    char *new_str;
    new_str=testCode(str);
}

The new_limit get the size of 0.

What should I do?

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
R2R
  • 41
  • 5
  • `char *testCode(string str)` What is string ? BTW: main() should return int. – wildplasser May 04 '14 at 21:35
  • typedef char string[SIZE_STRING]; – this May 04 '14 at 21:35
  • just typdef of char, it will be string[100]; – R2R May 04 '14 at 21:36
  • Why ? Why does main use `char str[SIZE_STRING] = { "BLA BLA BLA" };` again ? – wildplasser May 04 '14 at 21:36
  • Always use the language tag for proper highlighting, better categorization and to get the right eyes on it. Use the `edit`link to add it. – Deduplicator May 04 '14 at 21:36
  • 2
    Writing a new version of [`strdup`](http://pubs.opengroup.org/onlinepubs/009695399/functions/strdup.html)? – Edward May 04 '14 at 21:37
  • Well i'm new here, it will be beter, thakns! – R2R May 04 '14 at 21:37
  • @wildplasser The problem is pretty obvious. How are you comments helpful? – this May 04 '14 at 21:38
  • They are comments. And they are helpful in the way that first #defining a pseudo-typedef, and subsequently _not_ using it, is confusing, to say the least. – wildplasser May 04 '14 at 21:40
  • So, some tips: [Don't cast the result of malloc (and friends)](http://stackoverflow.com/questions/605845). `sizeof(char)` is per definitionem 1. You probably want to copy the original string, use `strcpy`or `memcpy`. Don't `calloc` if `malloc` can do the job, the added zeroing costs some time. – Deduplicator May 04 '14 at 21:40
  • 3
    `new_limit` is 0 because (as it happens) your newly allocated memory has a zero in its first place. Never mind that; you should not test it anyway, since you want `new_limit == limit`. By the way, you actually want `limit = strlen(str)+1`. – Jongware May 04 '14 at 21:43
  • well, I changed it as you told me, now it return new_limit=24, and it should be 12 – R2R May 04 '14 at 21:46
  • hi, I try to free the new_str in main, I have an Error : Debug Error! HEAP CORRUPTION DETECTED:after normal block(#60) at 0x008A7510 CRT detected that the application wrote to memory after end of heap buffer, any help ? thanks – R2R May 04 '14 at 22:02

3 Answers3

1

Change:

 new_str = (char*)calloc(limit, sizeof(char));

to

new_str = malloc(limit + 1);
strcpy(new_str, str);

You never copied the old string into the buffer you just made, so when you do strlen on it, of course you get zero.

M.M
  • 138,810
  • 21
  • 208
  • 365
0

To malloc() string in c with the same size of original string, the function 'testCode' must be given one additional item of information. Namely, the size of the original string.

#define SIZE_STRING 100
typedef char string[SIZE_STRING];

char *testCode(string str, size_t strSize)

Added 'strSize' (above) to the arguments of 'testCode' to provide the actual size of 'str'.

   {   
   char *new_str;
// int limit = strlen(str);
// int new_limit;
   new_str = (char*)calloc(strSize, sizeof(char));

Exchanged 'limit' for 'strSize' in order to allocate the correct size.

   new_limit = strSize;

Exchanged 'strlen(new_str)' for 'strSize'. The previous term 'strlen(new_str)' will always be zero due to calloc()'s 'zeroing-out the new memory' feature.

   printf("%d", new_limit);

  return new_str;
  }

void main()
   {
   char str[SIZE_STRING] = { "BLA BLA BLA" };
   char *new_str;
   new_str=testCode(str, sizeof(str));
  }

A final thought...

The question was how to "malloc string in c with the same size of original string". Keep in mind that the 'size' of a character array differs from the 'length' of a character array. While the 'size' of an array is defined as the number of bytes (or perhaps elements) that make up the entire array, the 'length' of a C string is the number of characters found in the array up to the terminating '\0' character.

Hence if the question was actually how to "malloc string in c with the same length of original string", the above answer would still apply, except for the line:

new_str=testCode(str, sizeof(str));

would have to be changed to:

new_str=testCode(str, strlen(str)+1);
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
  • You might want to point out that the allocated length is one too small in the original code (in the question); it has forgotten to allow for the terminating null. It also seems odd to allocate the space without copying the string. If you don't need to copy the string, then `malloc()` or `calloc()` does the job without any extra help. However, that's a separate discussion, perhaps. – Jonathan Leffler May 05 '14 at 02:03
  • Using `strlen` is fine – M.M May 05 '14 at 02:33
  • first of all thank you all, now,@Mahonri Moriancumer I'm not allow to change the title of the functions, so it should stay. For all those who told me to use strcpy, well, who say I need to copy strings ?, I have some projects in my collage and one of them is allocating enough memory for the new string.. again, thanks to all of you, the next problem is, I can't free the memory from the main... puts(new_str); free(new_str); puts(new_str); the free(new_str) just get Error, thankS ! – R2R May 05 '14 at 06:17
0

Do you mean you were given task to reimplement strdup() libc function?

https://sourceware.org/git/?p=glibc.git;a=blob;f=string/strdup.c;h=cedffa0da2e7a94ea8b95e79af6a7a5d0d3236d9;hb=HEAD#l37

kerolasa
  • 401
  • 2
  • 5