简体   繁体   English

在同一进程中以及2个进程之间将字符串从一个指针复制到另一个指针

[英]Copy String from one pointer to another in same process and between 2 processes

So I was asked these 2 questions in my interview- 所以在面试中有人问我这两个问题-

1-what if I have a code snippet like this in C what will be the output 1-如果我在C中有这样的代码片段,输出将是什么

char *ptr1=malloc(10);
ptr1="abc";
char *ptr2 = malloc(20);
ptr2="defghi";
strcpy(ptr1,ptr2);
printf("%s",ptr1);

I tried this one on my terminal after getting home and it was giving this error 我回到家后在终端上尝试了这个,它给出了这个错误

[1]    7673 bus error  ./b.out

I would like to know how to copy the string in such a case from one pointer to another. 我想知道在这种情况下如何将字符串从一个指针复制到另一个指针。

2- We have 2 processes A and B 2-我们有2个过程A和B

This is the code in process A 这是过程A中的代码

char *ptr1=malloc(10);
ptr1="abc";
char *ptr2 = malloc(20);
ptr2="defghi";
strcpy(ptr1,ptr2);

now the address of ptr1 is passed to process B using interprocess communication and the code in process B is as follows (ptr1 is the address from process A) 现在,使用进程间通信将ptr1的地址传递给进程B,并且进程B中的代码如下(ptr1是来自进程A的地址)

char *ptr3=malloc(10);
ptr3=ptr1
printf("%s",ptr3);

What would be the output of this? 这将是什么输出? If the ans is garbage value or error then in what way can we make ptr3 point to the string pointed by ptr1. 如果ans是垃圾值或错误,那么我们可以通过什么方式使ptr3指向ptr1指向的字符串。

I would like to know how to copy the string in such a case from one pointer to another. 我想知道在这种情况下如何将字符串从一个指针复制到另一个指针。

You can't. 你不能 Pointers do not contain strings, or their characters. 指针不包含字符串或其字符。 They can, however, point to null-terminated arrays of characters, ie strings. 但是,它们可以指向以空值结尾的字符数组,即字符串。

Having that in mind, it is important to understand that here: 考虑到这一点,在这里了解这一点很重要:

char *ptr1=malloc(10);
ptr1="abc";

you allocate block of memory (or try to do), assign its address to variable ptr1 , and then immediately overwrite that pointer value with a pointer to the arrary represented by string literal "abc" . 您分配内存块(或尝试执行操作),将其地址分配给变量ptr1 ,然后立即用指针指针覆盖由字符串文字"abc"表示的指针的指针值。 The original pointer value is lost -- you leak that memory. 原始指针值丢失了–您泄漏了该内存。 In particular, the assignment absolutely does not copy the characters of the literal into the allocated space. 特别是,赋值绝对不会将文字的字符复制到分配的空间中。

Now be aware that attempting to modify any part of a string literal invokes undefined behavior, whether the modification is direct or indirect. 现在注意,无论修改是直接修改还是间接修改,尝试修改字符串文字的任何部分都会调用未定义的行为。 This: 这个:

strcpy(ptr1,ptr2);

constitutes an attempt to modify string literal "abc" indirectly via pointer ptr1 . 构成了通过指针ptr1间接修改字符串文字"abc"的尝试。 If the question was "what is the output" then it was a trick question. 如果问题是“什么是输出”,那么这是一个棘手的问题。 The program has undefined behavior -- it's output cannot be predicted, at least not from the C code alone. 该程序具有未定义的行为-无法预测其输出,至少不能仅从C代码中预测。


The IPC case is a different trick question. IPC案是一个不同的技巧问题。 Pointer values are meaningful only in the context of the process in which the pointer was obtained. 指针值仅在获取指针的过程的上下文中才有意义。 If you pass that pointer to a different process via some form of IPC, there is no reason to suppose that it is valid in the receiving process. 如果您通过某种形式的IPC将该指针传递给其他进程,则没有理由认为该指针在接收进程中有效。 Even if it happens to be valid, there's no telling what it points to. 即使碰巧是有效的,也没有告诉它指向什么。 If you want to send a string to another process then you must send the characters , not the pointer. 如果要将字符串发送到另一个进程,则必须发送字符 ,而不是指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM