Results 1 to 3 of 3

Thread: memset char * char[]

  1. #1
    Join Date
    Jul 2015
    Posts
    7
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Talking memset char * char[]

    ULONG ncnt=10;

    unsigned char * pBuffer=NULL;
    pBuffer=(unsigned char * )malloc(ncnt);

    // unsigned char pBuffer[ncnt];

    memset(pBuffer,0,ncnt);
    this works well.
    but however when it comes blow I come into a C++ runtime error
    ULONG ncnt=10;

    //unsigned char * pBuffer=NULL;
    // pBuffer=(unsigned char * )malloc(ncnt);

    unsigned char pBuffer[ncnt];

    memset(pBuffer,0,ncnt);
    why?

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: memset char * char[]

    The few lines of code you provided are correct, but it is likely that the context in which they are executed cause the error. The difference between
    Qt Code:
    1. unsigned char * pBuffer = (unsigned char *)malloc(ncnt);
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. unsigned char pBuffer[ncnt];
    To copy to clipboard, switch view to plain text mode 
    is that in the first case, the array is allocated on the heap (and you must deallocate it yourself with free()), while in the second case it is allocated on the stack and automatically deallocated at the end of its scope. Your program probably accesses pBuffer outside its scope, after it has been deallocated, which causes an error at some point.

  3. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: memset char * char[]

    When you switch from malloc/free to allocating the buffer on the stack, did you also comment the code that does free(pBuffer)? You can't free something allocated on the stack.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Char Followed by a Char is illegal (qglobal.h)
    By saad_saadi in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2014, 14:19
  2. How to convert unsigned char[] to char *?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 29th April 2011, 09:58
  3. char to const char* with atof
    By mickey in forum General Programming
    Replies: 5
    Last Post: 29th February 2008, 05:10
  4. Conversion from unsigned char* to unsigned char
    By santosh.kumar in forum General Programming
    Replies: 1
    Last Post: 6th August 2007, 14:12
  5. c++ char to Int
    By mickey in forum General Programming
    Replies: 3
    Last Post: 5th October 2006, 00:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.