Does this function has the same behavior that memset?
inline void SetZeroArray( void *vArray[], unsigned int uArraySize )
{
for(unsigned i=0; i<=uArraySize; i++ )
vArray[i] = NULL;
}
int main( int argc, char *argv[] )
{
unsigned int uLevels[500];
SetZeroArray( (void**)uLevels, 500 );
unsigned int ulRLevels[500];
memset( &ulRLevels, 0, sizeof( ulRLevels ) );
system("pause>nul");
return EXIT_SUCCESS;
}
NO, your function does not behave the same as memset. Your function sets a pointer to NULL and memset sets the values of the data to the value supplied.
Different things altogether.
Related
There is the CommandLineToArgvW() function, which is CommandLineToArgv + W, where this W means wide char (wchar_t in C/C++). But the CommandLineToArgvA() function that should exist, such as GetCommandLineW() and GetCommandLineA(), does not exist, apparently.
char:
int argv;
char **argv = CommandLineToArgvA(GetCommandLineA(), &argc);
wide char:
int argv;
wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
Well, I searched every corner of the Internet for the term CommandLineToArgvA() and the most I found was this function in Linux Wine... I want to know, does this function exist, and if yes, is it normal that it is "hidden"? Otherwise, does it really not exist?
edit: The question was whether there was the CommandLineToArgvA function in the Windows API, however, it does not exist (comment by Remy Lebeau). The answer I checked as correct is the one that best explains how to use the existing CommandLineToArgvW function and turn the wchar_t into char, which will provide the same result that would be provided with the CommandLineToArgvA function if it existed.
I don’t think you should try parsing your own command-line string. Windows does it one way. Trying to write duplicate code to do the same thing is the Wrong Thing™ to do.
Just get the command-line, then use the Window facilities to convert it to UTF-8.
#include <stdlib.h>
#include <windows.h>
#include <shellapi.h>
#pragma comment(lib, "Shell32")
void get_command_line_args( int * argc, char *** argv )
{
// Get the command line arguments as wchar_t strings
wchar_t ** wargv = CommandLineToArgvW( GetCommandLineW(), argc );
if (!wargv) { *argc = 0; *argv = NULL; return; }
// Count the number of bytes necessary to store the UTF-8 versions of those strings
int n = 0;
for (int i = 0; i < *argc; i++)
n += WideCharToMultiByte( CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL ) + 1;
// Allocate the argv[] array + all the UTF-8 strings
*argv = malloc( (*argc + 1) * sizeof(char *) + n );
if (!*argv) { *argc = 0; return; }
// Convert all wargv[] --> argv[]
char * arg = (char *)&((*argv)[*argc + 1]);
for (int i = 0; i < *argc; i++)
{
(*argv)[i] = arg;
arg += WideCharToMultiByte( CP_UTF8, 0, wargv[i], -1, arg, n, NULL, NULL ) + 1;
}
(*argv)[*argc] = NULL;
}
Obtains an argv just like the one main() gets, with a final NULL element
and writeable and all.
Interface is easy enough. Don’t forget to free() the result when you are done with it. Example usage:
#include <stdio.h>
#include <stdlib.h>
void f(void)
{
int argc;
char ** argv;
get_command_line_args( &argc, &argv );
for (int n = 0; n < argc; n++)
printf( " %d : %s\n", n, argv[n] );
free( argv );
}
int main(void)
{
f();
}
Enjoy!
I have a strange behavior with a char pointer initialized by the value of a return function and with the cout.
All my code is for an Arduino application, this is why I use char pointer, char array and string.h.
I created a class named FrameManager, with a function getDataFromFrame to extract data from a string (in fact a char array). See above:
`char * FrameManager::getDataFromFrame ( const char frame[], char key[] )
{
char *pValue = nullptr;
int frameLength = strlen ( frame );
int previousStartIndex = 0;
for ( int i=0; i<frameLength; i++ ) {
char c = frame[i];
if ( c == ',' ) {
int buffSize = i-previousStartIndex+1;
char subbuff[buffSize];
memset ( subbuff, 0, buffSize ); //clear buffer
memcpy ( subbuff, &frame[previousStartIndex], i-previousStartIndex );
subbuff[buffSize]='\0';
previousStartIndex = i+1;
int buffLength = strlen ( subbuff );
const char *ptr = strchr ( subbuff, ':' );
if ( ptr ) {
int index = ptr-subbuff;
char buffKey[index+1];
memset ( buffKey, 0, index+1 );
memcpy ( buffKey, &subbuff[0], index );
buffKey[index+1]='\0';
char buffValue[buffLength-index];
memset ( buffValue, 0, buffLength-index );
memcpy ( buffValue, &subbuff[index+1], buffLength-index );
buffValue[buffLength-index]='\0';
if ( strcmp ( key,buffKey ) == 0 ) {
pValue = &buffValue[0];
break;
}
}
} else if ( i+1 == frameLength ) {
int buffSize = i-previousStartIndex+1;
char subbuff[buffSize];
memcpy ( subbuff, &frame[previousStartIndex], frameLength-1 );
subbuff[buffSize]='\0';
int buffLength = strlen ( subbuff );
const char *ptr = strchr ( subbuff, ':' );
if ( ptr ) {
int index = ptr-subbuff;
char buffKey[index+1];
memset ( buffKey, 0, index+1 );
memcpy ( buffKey, &subbuff[0], index );
buffKey[index+1]='\0';
char buffValue[buffLength-index];
memset ( buffValue, 0, buffLength-index );
memcpy ( buffValue, &subbuff[index+1], buffLength-index );
buffValue[buffLength-index]='\0';
if ( strcmp ( key,buffKey ) == 0 ) {
pValue = &buffValue[0];
break;
}
}
}
}
return pValue;
}`
In the main(), I created juste a little code to test the returned value:
int main(int argc, char **argv) {
const char frame[] = "DEVICE:ARM,FUNC:MOVE_F,PARAM:12,SERVO_S:1";
FrameManager frameManager;
char key[] = "DEVICE";
char *value;
value = frameManager.getDataFromFrame(frame, &key[0]);
cout << "Retrieved value: " << value << endl;
cout << "Retrieved value: " << frameManager.getDataFromFrame(frame, &key[0]) << endl;
printf("%s",value);
return 0;
}
and here the result:
Retrieved value: y%R
Retrieved value: ARM
ARM
The first "cout" doesn't display the expected value.
The second "cout" display the expected value and the printf too.
I don't understand what is the problem with the first "cout".
Thanks
Jocelyn
pValue points into local arrays, which get out of scope. That's undefined behavior. It might work, but your program might also crash, return wrong values (that's what you experience), corrupt your data or do any other arbitrary action.
Given that you're already using C++, consider using std::string as a result instead or point into the original frame (if possible).
This is my code. I want to get 5 strings from the user and espeak reads each of them when user interred it. But I get segmentation fault(core dumped) message.
#include <string.h>
#include <malloc.h>
#include <espeak/speak_lib.h>
int test()
{
espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;
char *path=NULL;
int Buflength = 500, Options=0;
void* user_data;
t_espeak_callback *SynthCallback;
espeak_PARAMETER Parm;
char Voice[] = {"English"};
int i=0;
char text[1000];
unsigned int Size,position=0, end_position=0, flags=espeakCHARS_AUTO, *unique_identifier;
output = AUDIO_OUTPUT_PLAYBACK;
espeak_Initialize(output, Buflength, path, Options );
espeak_SetVoiceByName(Voice);
const char *langNativeString = "en_US";
espeak_VOICE voice={0};
voice.languages = langNativeString;
voice.name = "US";
voice.variant = 2;
voice.gender = 1;
Size = strlen(text)+1;
for (i=0; i<5; i++)
{
scanf("%s ", &text);
printf("%s", text);
espeak_Synth( text, Size, position, position_type, end_position, flags,
unique_identifier, user_data );
espeak_Synchronize( );
fflush(stdout);
}
return 0;
}
int main(int argc, char* argv[] )
{
test();
return 0;
}
I tried some modification but none of them worked. I want the program works like this:
User input: hi
espeak says: hi
user input: one
espeak says: one
(for 5
inputs)
But when I try to interring more than 4 characters as input,it gives segmentation fault error!
The two main issues are:
you use strlen on an uninitialized array of chars;
the unique_identifier argument of espeak_Synth must be NULL or point to an unsigned int (see the source code) while now it is an unsigned pointer to random memory.
Move strlen after scanf, use NULL instead of unique_identifier and your code will suddenly work (kind of).
There are many other issues though: useless variables, uninitialized variables, no input sanitization and more. IMO a better approach would be to throw away the test function and rewrite it from scratch properly.
Addendum
This is how I'd rewrite the above code. It is still suboptimal (no input sanitization, no error checking) but IMO it is much cleaner.
#include <stdio.h>
#include <string.h>
#include <espeak/speak_lib.h>
static void say(const char *text)
{
static int initialized = 0;
if (! initialized) {
espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 0, NULL, 0);
espeak_SetVoiceByName("en");
initialized = 1;
}
espeak_Synth(text, strlen(text)+1,
0, POS_CHARACTER, 0,
espeakCHARS_UTF8, NULL, NULL);
espeak_Synchronize();
}
int main()
{
char text[1000];
int i;
for (i = 0; i < 5; ++i) {
scanf("%s", text);
say(text);
}
return 0;
}
I need to return a integer from callback, but I not succeeded.
I tried - but nothing.
I will be very glad if you will help me.
Here is my code:
using namespace std;
int main()
{
sqlite3* db;
char* zErrMsg;
sqlite3_open("try.db", &db);
//do something...
string query = "select * from myTable";
int *ptr;
sqlite3_exec(db, query.c_str(), callback, ptr, &zErrMsg);
cout << ptr << endl;
system ("pause");
return 0;
}
int callback(void* notUsed, int argc, char** argv, char** azCol)
{
/*
chane void* notUsed to int*
???how???
*/
return 0;
}
You have to pass an address of valid memory (e.g. on stack) or allocate it (heap) to the callback function, if you allocate you have to free it also.
The forth argument of the sqlite3_exec() function will be the first argument of the callback() function wich is of type void*. So if you pass the address of an int you have to interpret the void* as an int*.
I changed it so the memory (int someNumber) is in the callers function (main) and pass the address of it. In the callback function you need to cast the void* to the pointer type you expect, here int* and assign a value.
It should be:
...
int main()
{
...
int someNumber;
sqlite3_exec(db, query.c_str(), callback, &someNumber, &zErrMsg);
cout << someNumber << endl;
...
}
int callback(void* someNumberArg, int argc, char** argv, char** azCol)
{
int* someNumber = reinterpret_cast<int*> (someNumberArg);
*someNumber = 42;
return 0;
}
[Fore note: I have read the existing threads in StackOverflow. None seemed to be on my question]
I am looking into the Quake 2 MD2 format. I seem to be getting a bad allocation after new'ing a pointer-array. However, if I do some horrible pointer manipulation, everything is fine.
I guess the question is, why am I getting such an exception?
The first "MD2Model::Load" works. The one I posted after it gets the exception.
Basic structures :
struct MD2Header
{
int nIdentifier;
int nVersion;
int nSkinWidth;
int nSkinHeight;
int nFrameSize;
int nNumSkins;
int nNumVertices;
int nNumUV;
int nNumTriangles;
int nNumCmds;
int nNumFrames;
int nOffsetSkins;
int nOffsetUV;
int nOffSetTriangles;
int nOffsetFrames;
int nOffsetCmds;
int nOffsetEnd;
};
struct MD2Skin
{
char szName[64];
};
struct MD2TexCoord
{
short t;
short u;
};
struct MD2Triangle
{
short nVertex[3];
short tu[3];
};
struct MD2Vertex
{
float fVertex[3];
float fNormal[3];
};
struct MD2Frame
{
char szName[16];
MD2Vertex* pVerts;
};
And now, the function that reads the .md2 file:
bool MD2Model::Load( const char* pszName )
{
FILE* pFile = NULL;
fopen_s( &pFile, pszName, "rb" );
if( !pFile )
return false;
/* Read Header */
fread( &m_Header, sizeof(MD2Header), 1, pFile );
/* Allocate Pointers */
m_pSkins = new MD2Skin[m_Header.nNumSkins];
m_pTexCoords = new MD2TexCoord[m_Header.nNumUV];
m_pTriangles = new MD2Triangle[m_Header.nNumTriangles];
m_pFrames = new MD2Frame[m_Header.nNumFrames];
/* Read Skins */
fseek( pFile, m_Header.nOffsetSkins, SEEK_SET );
fread( m_pSkins, sizeof(MD2Skin), m_Header.nNumSkins, pFile );
/* Read Texture Coords */
fseek( pFile, m_Header.nOffsetUV, SEEK_SET );
fread( m_pTexCoords, sizeof(MD2TexCoord), m_Header.nNumUV, pFile );
/* Read Faces */
fseek( pFile, m_Header.nOffSetTriangles, SEEK_SET );
fread( m_pTriangles, sizeof(MD2Triangle), m_Header.nNumTriangles, pFile );
/* Read Animations */
struct stMD2Vertex
{
unsigned char nVertex[3];
unsigned char nLightNormalIndex;
};
struct stMD2Frame
{
float fScale[3];
float fTranslate[3];
char szName[16];
stMD2Vertex verts[1];
};
unsigned char pBuffer[30000];
stMD2Frame* pTmp = (stMD2Frame*) pBuffer;
fseek( pFile, m_Header.nOffsetFrames, SEEK_SET );
for( int i = 0; i < m_Header.nNumFrames; i++ )
{
fread( pTmp, 1, m_Header.nFrameSize, pFile );
m_pFrames[i].pVerts = new MD2Vertex[m_Header.nNumVertices];
strcpy_s( m_pFrames[i].szName, pTmp->szName );
for( int j = 0; j < m_Header.nNumVertices; j++ )
{
m_pFrames[i].pVerts[j].fVertex[0] = pTmp->verts[j].nVertex[0] *
pTmp->fScale[0] + pTmp->fTranslate[0];
m_pFrames[i].pVerts[j].fVertex[2] = -1 * (pTmp->verts[j].nVertex[1] *
pTmp->fScale[1] + pTmp->fTranslate[1]);
m_pFrames[i].pVerts[j].fVertex[1] = pTmp->verts[j].nVertex[2] *
pTmp->fScale[2] + pTmp->fTranslate[2];
}
}
return true;
}
Variables dumped during debugging:
nNumSkins 1 int
nNumVertices 583 int
nNumUV 582 int
nNumTriangles 422 int
nNumFrames 1 int
(I ended up removing my D3DXVECTOR3 structures to see, so it's kinda fugly..)
Alright. So, inside the 'for' loop, is where it blows up.
If I were to do it like this:
// unsigned char pBuffer[30000];
// stMD2Frame* pTmp = (stMD2Frame*) pBuffer;
fseek( pFile, m_Header.nOffsetFrames, SEEK_SET );
for( int i = 0; i < m_Header.nNumFrames; i++ )
{
stMD2Frame* pTmp = new stMD2Frame();
fread( pTmp, 1, m_Header.nFrameSize, pFile );
m_pFrames[i].pVerts = new MD2Vertex[m_Header.nNumVertices];
strcpy_s( m_pFrames[0].szName, pTmp->szName );
I get the bad_alloc exception during allocating the "m_pFrames[i].pVerts" statement.
Sometimes, I don't get it, but I do get the exception when I try to new my D3D class (I'm assuming I'll get it no matter what I new afterwards).
My MEM usage is extremely low, so could it be heap corruption?
I actually had to end up doing this during creating my VertexBuffer during animations as well.
Should I end up using vectors? I know they allocate more than needed, but it seems like the (most obvious)[best] way.
I would check the line:
strcpy_s( m_pFrames[0].szName, pTmp->szName );
If the string loaded from the file is not null-terminated then this copy could end up overwriting your stack space and causing corruption.
EDIT
Looking again, I think the problem is that you define stMD2Frame as
struct stMD2Frame
{
float fScale[3];
float fTranslate[3];
char szName[16];
stMD2Vertex verts[1];
};
but this only has space for one vertex.
When you read in the frame as
fread( pTmp, 1, m_Header.nFrameSize, pFile );
you will corrupt your memory.
I suggest a check that
sizeof(stMD2Frame)>=m_Header.nFrameSize
before reading the data.