Welcome, Guest. Please login or register.

Author Topic: char *buf2=new char[size]; problem  (Read 3815 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: char *buf2=new char[size]; problem
« on: November 11, 2004, 10:11:01 AM »
Quote
sounds like you are trying to use a C++ code on a C compiler. The new C++ function is called malloc in C. I think the syntax is:

char *buf2 = (char*) malloc((sizeof(char)*"an integer"));

if I remember corretly

You don't.

(sizeof(char) * "string") is nonsense, you need to give it the size of the memory to allocate.
For example:
Code: [Select]

char *str = "a string";
char *buf = malloc(strlen(str) + 1);
if (buf)
{
  strcpy(buf, str);
  ...
  free(buf);
}
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: char *buf2=new char[size]; problem
« Reply #1 on: November 11, 2004, 10:24:45 AM »
@nex4060

That was rather confusing, then.

 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: char *buf2=new char[size]; problem
« Reply #2 on: November 11, 2004, 11:34:40 AM »
Quote
and next - hardcore reboot/red window of death/etc. why ?:)

Because the code overwrite memory that is not allocated.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: char *buf2=new char[size]; problem
« Reply #3 on: November 11, 2004, 11:45:33 AM »
Quote
memcpy(temp,buf2,sizeof(buf2));

This is wrong. sizeof(buf2) is sizeof(char *), size of a pointer, 4 with 32bit addressing systems, 8 with 64bit addressing ones.

It should be memcpy(temp,buf2,size-1);

Anyway, this code is pretty inefficient, it should use larger buffer size, and preferably linked list of chunks, rather than excessive copying which kills cache.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: char *buf2=new char[size]; problem
« Reply #4 on: November 11, 2004, 03:26:35 PM »
Quote
Code: [Select]

// Could do char buf1 here and pass &buf1 to function
char buf1[1];
// Initialise to 0 elements
int size=0;
// Initialise to NULL so realloc works properly!
// Could do "char *buf2 = NULL;", its the same thing!
char buf2[] = NULL;

while (recv(new_fd,buf1,sizeof(buf1),0))
{
    // Increase size of array
    size++;
    // Reallocate buffer (+1 for terminator!)
    realloc(buf2,(size+1)*(sizeof(char));
    // Copy character (-1 because array starts at 0)
    buf2[size-1] = buf1[0];
    // Reset terminator
    buf2[size] = '\0';
    // Print status
    printf("size: %d\nbuf2: %s\n",size,buf2);
}

This code is broken. It crashes horribly when realloc can't resize the allocation, but need to allocate new memory area and move the data. Return value of realloc must be tested for failure or new address.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: char *buf2=new char[size]; problem
« Reply #5 on: November 11, 2004, 04:29:16 PM »
@PhatBoiCollier
The code is still broken and crashes horribly.

If realloc() cannot expand the current memory allocation, it returns different pointer than the one passed in. In that case, you poke already free memory -> boom. If that doesn't nuke, it will crash at double free when calling realloc/free for the wrong ptr.