标签: strcpy

strcpy ...想用strcpy_mine替换strncpy和null终止

线索在标题中,但基本上我继承了一些有800多个strcpy实例的代码.我想编写一个新函数,然后用strcpy_mine替换strcpy.

所以我想弄清楚strcpy_mine将包含哪些参数列表.

我试过了:

void strcpy_mine( char* pTarget, const char* const pCopyMe )
{
  const unsigned int lenAlwaysFour = sizeof(pCopyMe ); //:(
  strncpy( pTarget, pCopyMe, lenAlwaysFour );

  //add extra terminator in case of overrun
  pTarget[lenAlwaysFour] = 0;
}
Run Code Online (Sandbox Code Playgroud)

但sizeof总是4 pCopyMe是一个指针

我不想做的就是更换

strcpy (buf, pCopyMe);
Run Code Online (Sandbox Code Playgroud)

strncpy (buf, pCopyMe, sizeof(pCopyMe)); buf[sizeof(pCopyMe)] = 0;
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?(strcpy_l不可用)

干杯

c c++ strcpy

6
推荐指数
2
解决办法
1531
查看次数

Valgrind警告:我应该认真对待它

背景:我有一个模仿的小例程,fgets(character, 2, fp)除了它从字符串而不是流中获取字符.newBuff是动态分配的字符串,作为参数传递,字符声明为char character[2].

常规:

character[0] = newBuff[0];

character[1] = '\0';

strcpy(newBuff, newBuff+1);
Run Code Online (Sandbox Code Playgroud)

strcpy在从中读取每个字符时复制信息丢失.

问题:Valgrind确实警告我这个活动,"源和目标重叠在strcpy(0x419b818,0x419b819)".

我应该担心这个警告吗?

c valgrind overlap strcpy

6
推荐指数
2
解决办法
5412
查看次数

在C中使用strcpy和字符串数组

我有一个字符数组定义如下:char *c[20];
我正在尝试:strcpy(c[k], "undefined);但它不起作用

我也试过把它定义为char c[20][70]没有运气.

编辑:我实际上知道它是一个字符数组的数组,我需要它.

c arrays char strcpy

6
推荐指数
1
解决办法
4万
查看次数

将strcpy用于mmap的文件时出现总线错误(核心转储)

我有一个简单的程序:

int main(void) {
   int fd;
   const char *text = "This is a test";

   fd = open("/tmp/msyncTest", (O_CREAT | O_TRUNC | O_RDWR), (S_IRWXU | S_IRWXG | S_IRWXO) );
   if ( fd < 0 ) {
           perror("open() error");
           return fd;
   }

    /* mmap the file. */
   void *address;
   off_t my_offset = 0;
   address = mmap(NULL, 4096, PROT_WRITE, MAP_SHARED, fd, my_offset);

   if ( address == MAP_FAILED ) {
           perror("mmap error. " );
           return -1;
   }

    /* Move some data into the file …
Run Code Online (Sandbox Code Playgroud)

c strcpy

6
推荐指数
1
解决办法
1902
查看次数

为什么strcpy不使用const指针作为dest?

有没有理由strcpy的签名是这样的:

char *strcpy(char *dest, const char *src);
Run Code Online (Sandbox Code Playgroud)

而不是这个?

char *strcpy(char *const dest, const char *src);
Run Code Online (Sandbox Code Playgroud)

据我所知,该函数永远不会改变指针.

我误解了应该使用的const指针吗?在我看来,当我写的函数接受一个不会被改变的指针(通过realloc等),然后我将它标记为一个const指针,这样可以确保调用者不会在它们上移动它们的指针.(如果它们有其他结构/等,引用那个过时的指针位置)

这是一个好的做法,还是会产生意想不到的后果?

c pointers const strcpy

6
推荐指数
1
解决办法
477
查看次数

Linux 上的 jvmti 代理致命错误:C [libc.so.6+0x7ae68] strcpy+0x18

我编写了一个 jvmti 代理来跟踪方法调用。我用 C 和 jvmti 和 jni 函数对其进行编码。我们的操作系统是Fedora 15,代理被编译成一个.so文件。当我用一个重要的 java 程序测试它时,它崩溃并给出以下错误消息:

A fatal error has been detected by the Java Runtime Environment:
  SIGSEGV (0xb) at pc=0x4e8e4e28, pid=24294, tid=3065949040.
  JRE version: 6.0_32-b05.
  Java VM: Java HotSpot (TM) Server VM (20.7-b02 mixed mode linux-x86).
  **Problematic frame:
    C [libc.so.6+0x7ae68] strcpy+0x18.**
Run Code Online (Sandbox Code Playgroud)

crash strcpy jvmti

5
推荐指数
1
解决办法
2746
查看次数

为什么C中的strcpy不安全?

我是初学者,我正在学习如何在C中复制字符串.

这是我刚遇到的一个问题:

每次我尝试使用"strcpy"命令从字符串1复制到字符串2时,Visual Studio 2013会给我一条错误/警告消息,说"strcpy"不安全,建议我改用strcpy_s.

你能解释一下为什么使用strcpy不安全吗?什么是strcpy的更安全的替代品?

这是我的代码:

#include<stdio.h>
#include<string.h>
main()
{
    char str1[] = "Copy a string.";
    char str2[15];
    char str3[15];
    int i;

    /* with strcpy() */
    strcpy(str2, str1);
    /* Without strcpy() */
    for (i = 0; str1[i]; i++)
        str3[i] = str1[i];
    str3[i] = '\0';
    /* Display str2 and str3 */
    printf("The content of str2: %s\n", str2);
    printf("The content of str3: %s\n", str3);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

PS我在Windows 7 64位终极版上使用Visual Studio 2013 64位终极版.感谢您的时间!

c string strcpy

5
推荐指数
1
解决办法
2万
查看次数

无法在 C 中使用 strcpy 从指针数组复制字符串?

我正在做一个练习,其中字符指针数组用作存储单词的一种方式。我不明白为什么我不能使用 'strcpy' 将单词 'hoi' 复制到主函数中数组的第二个元素。当我编译代码时,我在 CodeBlocks 中收到消息“程序已停止工作”。

函数“numberOfWordsInDict”和“printDict”工作正常。

提前致谢。

int numberOfWordsInDict(char **dict)
{
    int i, cnt = 0;
    for(i = 0; i < 10; i++)
    {
        if(dict[i] != NULL)
        {
            cnt++;
        }
    }
    return cnt;
}

void printDict(char **dict)
{
    int i = 0;
    printf("Dictionary:\n");
    if(numberOfWordsInDict(dict) == 0)
    {
        printf("The dictionary is empty.\n");
    } else
    {
        for(i = 0; i < 10; i++)
        {
            printf("- %s\n", dict[i]);
        }
    }
}

int main()
{
    char *dict[10] = {
            "aap", "bro ", …
Run Code Online (Sandbox Code Playgroud)

c string-literals strcpy undefined-behavior

5
推荐指数
1
解决办法
824
查看次数

为什么我不能strcpy?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main() {
   const char* hello = "Hello, World!";
   char *str = malloc(14 * sizeof(char));

   for (int i = 0; i < 14; i++) {
      strcpy(str[i],hello[i]);
   }
   str[14]='\0';

   printf("%s\n", str);

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译警告:

warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]   
note: expected 'char *' but argument is of type 'char'   
warning: passing argument 2 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]

str也是一个指针和你好,发生了什么事?

c pointers strcpy

5
推荐指数
1
解决办法
454
查看次数

-fno-builtin 到底在做什么?

所以我正在阅读Hacking the Art of Exploitation并且在书中,他们strcpy()在他们的 C 代码中使用了这个函数:

1   #include <stdio.h>
2   #include <string.h>
3   
4       int main() {
5           char str_a[20];
6   
7           strcpy(str_a, "Hello, world!\n");
8           printf(str_a);
9       }
Run Code Online (Sandbox Code Playgroud)

然后他们继续编译他们的源代码并使用gdb. 他在第 6 行、strcpy函数和第 8行设置了一个断点,但是在设置断点时strcpy会显示以下内容:

(gdb) break strcpy
Function "strcpy" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Run Code Online (Sandbox Code Playgroud)

我知道这是因为库还没有加载,所以它问他是否想把它作为一个挂起的断点。然后他运行程序并继续通过断点:

图片

一切对他来说都很好,但是当我尝试在我的计算机上重新创建它时,我得到以下信息:

frinto@kali:~/Documents/theclang/programs/helloworld$ gcc -m32 -g -o char_array char_array.c 
frinto@kali:~/Documents/theclang/programs/helloworld$ gdb -q char_array
Reading symbols from …
Run Code Online (Sandbox Code Playgroud)

c gcc gdb reverse-engineering strcpy

5
推荐指数
1
解决办法
3082
查看次数