我已经研究了一段时间(在C中)并且无法解决这个问题.我有一个包含字符数组的缓冲区.我已经使用qsort对数组进行排序,现在它们都处于正确的顺序.我现在需要删除重复项(或者只打印出没有重复的列表).有一点需要注意:字符被分为N个字符组(用户给出的N).所以它不仅仅是将一个字母与另一个字母进行比较; 它将它们的组相互比较.
例如:如果输入是AADDBBEECCEE并且用户给出的N是2,则结果将是AABBCCDDEE(其中一个EE已被删除).
我知道我必须使用memcmp,但我对语法感到困惑.我尝试着:
i=0;
int result;
int k;
while(i<bufferSize-nValue){
result = memcmp(buffer[i], buffer[i+nValue], nValue);
if(result==0){
i=i+nValue;
}
else{
for(k=0; k<nValue; k++){
printf("%c",buffer[i]);
i++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
其中buffer是数组,nValue是N,bufferSize是数组中元素的总数.运行代码时我不断出现分段错误.
谢谢大家的帮助!
我正在使用memcmp()相同结构的两个变量进行比较(结构中有结合).变量在两个数组中,我正在运行一个循环,我在每次迭代中都这样做memcmp(&arr1[i], &arr2[i], sizeof(arrtype)).
调试时我发现memcmp返回-1,但是看两个变量及其值,我看到变量的值相等.这些数组在开始时使用memset归零.
memcmp返回-1而不是0?码:
typedef struct type1 {
int version;
union {
option1_t opt1;
option2_t opt2;
} union_t;
} type1_t;
typedef struct type0 {
type1_t member1;
type2_t member2;
type3_t member3;
type4_t member4;
type5_t member;
} type0_t;
type0_t arr1[SIZE];
type0_t arr2[SIZE];
memset(arr1, 0, SIZE * sizeof(type0_t));
memset(arr2, 0, SIZE * sizeof(type0_t));
/* doing irrelevant stuff... */
/* get values into arr1, arr2 ... */
/* comparing both arrays in for loop*/
value …Run Code Online (Sandbox Code Playgroud) 我刚刚调试了一个令人难以置信的令人讨厌的错误:在我自己的PC(Windows 7 x64,MinGw)上,我的C程序将使用memcmpwhen比较数组成员成功对数组进行排序.
我的函数使用冒泡排序算法,它的骨架看起来像这样:
void array_sort(ArrayMap *array, char direction) {
make sure direction is +1 or -1 only
define some variables
for(...) {
for(...) {
cmpres = memcmp(elm1, elm2, sizeOfElement);
if (cmpres!=0&&cmpres!=direction)
{
SWAP here
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在我的电脑上,memcmp已经返回-1,0而1在另一个电脑上,它又返回了-5,0并且5.通过与direction我的比较,导致排序完全错误.
但我想知道,返回值的绝对值(即大小)memcmp究竟是什么意思?
返回表示存储块内容之间关系的整数值:零值表示两个存储块的内容相等.的值大于零表示不在两个存储器块匹配的第一个字节在PTR1比PTR2更大的值,如同评价为无符号字符值; 小于零的值表示相反.
没有提到大小,他们只是通过大于零来确保+ -1的错误.
我使我的冒泡排序程序通用.我继续测试它,它运行良好,直到我在阵列中放置一个负数,我很惊讶它被推到最后,使它比正数更大.
显然memcmp是原因,那么为什么memcmp()负数大于正数呢?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void bubblesortA(void *_Buf, size_t bufSize, size_t bytes);
int main(void)
{
size_t n, bufsize = 5;
int buf[5] = { 5, 1, 2, -1, 10 };
bubblesortA(buf, 5, sizeof(int));
for (n = 0; n < bufsize; n++)
{
printf("%d ", buf[n]);
}
putchar('\n');
char str[] = "bzqacd";
size_t len = strlen(str);
bubblesortA(str, len, sizeof(char));
for (n = 0; n < len; n++)
{
printf("%c ", str[n]);
}
putchar('\n');
return …Run Code Online (Sandbox Code Playgroud) 使用memcmp我遇到了一个小问题.我有两个数组(长度= 3个字节),具有完全相同的数据.
如果我尝试将它们与memcmp进行比较,它会失败吗?!
if (memcmp(ucbuffer, ucnewbuffer, buffer.sDeviceData.sLenght)) {
cout << "val written, val ok!\n";
};
Run Code Online (Sandbox Code Playgroud)
ucbuffer,ucnewbuffer都是unsigned char*并且是使用分配的
calloc(buffer.sDeviceData.sLenght, sizeof(unsigned char);
Run Code Online (Sandbox Code Playgroud)
如果我手动比较两个数组,它们将导致完全相同.
你有什么主意吗?
祝你有个美好的夜晚.
我一直在使用memcmp函数来比较我的性能关键应用程序中的2个整数.除了使用相等的运算符之外我不得不使用它,因为我必须一般地处理其他数据类型.但是,我怀疑原始数据类型的memcpy性能,并将其更改为等于运算符.但是,性能的提高.
我刚做了一些简单的测试,如下所示.
使用memcmp
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
int main(int argc, char **argv)
{
int iValue1 = atoi(argv[1]);
int iValue2 = atoi(argv[2]);
struct timeval start;
gettimeofday(&start, NULL);
for (int i = 0; i < 2000000000; i++)
{
// if (iValue1 == iValue2)
if (memcmp(&iValue1, &iValue2, sizeof(int)) == 0)
{
cout << "Hello" << endl;
};
};
struct timeval end;
gettimeofday(&end, NULL);
cout << "Time taken : " << ((end.tv_sec * …Run Code Online (Sandbox Code Playgroud) 如果我发送memcmp两个指向整数的指针,那么它似乎将整数解释为字符。
例如:
int a = 5;
int b = 256;
int res = memcmp(&a,&b,sizeof(int));
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,它返回 1。
我想更好地理解这个函数,我想知道我是否使用了错误的函数,或者是否有一个类似的函数来比较内存块的保存int值。
struct Flat {
int a1;
int a2;
}
// a hierarchical struct which containing a struct attribute
struct NonFlat {
Flat b1;
int b2;
}
Flat f1, f2;
memcmp (&f1, &f2, sizeof f1)
Run Code Online (Sandbox Code Playgroud)
在我的编译器中,它起作用,意思是
f1.a1 == f2.a1,f1.a2 == f2.a2 <=> memcmp(f1,f2)== 0;
NonFlat n1, n2;
memcmp (&n1, &n2, sizeof n1) // does it also work for non-flat structs, considering the padding?
Run Code Online (Sandbox Code Playgroud)
我认为它也适用于NonFlat结构.但是,在我的编译器中,对于非平面结构,即使成员属性相等,memcmp的结果也表明它们是不同的.
我有下一个问题.我用int memcmp ( const void * ptr1, const void * ptr2, size_t num );
函数来比较两个包含整数的void指针.这对我很有用.
int firstValue = 5;
int secondValue = 3;
void* firstValueVoid;
void* secondValueVoid
firstValueVoid = new int(firstValue);
secondValueVoid = new int(secondValue);
int compare = memcmp(firstValueVoid, secondValueVoid, 4);
cout << compare << endl;
Run Code Online (Sandbox Code Playgroud)
但是,如果我想对字符串进行同样的操作,它总是表明第一个值小于第二个值.
string firstValue = "abc";
string secondValue = "a";
int testSize = firstValue.length();
void* firstValueVoid;
void* secondValueVoid
firstValueVoid = new string(firstValue);
secondValueVoid = new string(secondValue);
int compare = memcmp(firstValueVoid, secondValueVoid, testSize);
cout << …Run Code Online (Sandbox Code Playgroud)