C:struct-> char *:内容已更改:有人可以解释一下吗?

时间:2014-02-20 21:17:46

标签: c string pointers

为了给出一些上下文,我为我的计算机网络类分配了一个读取输入文件并打包内容的任务。所以我有函数来初始化与每个层相关的数据的指针。 我对作业的设计没有任何问题,而是我正在处理的一个非常模糊的错误......

我已经包含了相关的结构定义和导致错误的代码行... //******************所包含的区域是错误发生的地方。尽管引用相同的指针,但第一个printf的输出与第二个printf的输出有所不同......我甚至没有触摸指针,所以我很难计算为什么内容会突然变坏...

我还包括printf输出...

typedef struct
{
  char *data;
  int size;
} layer4_t;

typedef struct
{
  char *data;
  int nframes;
  int overflow;
  int size;
} layer3_t;

layer4_t *layer4_initForTransmit( const char *str )
{
  //
  // output variable initialization
  // 
  layer4_t *y = malloc( sizeof( layer4_t ) );
  if (!y) fprintf( stderr, "transmit: layer4: error: malloc failed\n");

  // compute size of the data buffer (+1 for null byte)
  y->size = strlen(str);
  // memory allocation of (layer4)y.data
  y->data = malloc( sizeof( y->size+1 )); // ******** INCRIMINATING... 
  // copy contents of str to buf and copy over null byte
  y->data[0] = '\0';
  strcat( y->data, str );

  if (DEBUG) printLayerData( y, 4 );

  return y;
}

layer3_t *layer3_initForTransmit( layer4_t *x )
{
  //
  // variable declarations
  //

  // 2. output variable
  layer3_t *y;

  // dummy ptrs
  char *ptr;     // 6 
  char *buf_ptr; // 1

  // 7. i: frame index, 
  // 3. n_frames: number of frames, 
  // 5. tx_size: size of transmission buffer, 
  // 1. buf_size: size of the layer4 data;
  // 8. offset: used to offset the buf_ptr
  int i, n_frames, tx_size, buf_size, offset = 0;

  //
  // variable assignments
  //

  // 1. assign dummy pointers
  buf_ptr = x->data;
  buf_size = x->size;

  // **********************
  printf( "%s\n", x->data );

  // 2. allocate space for output variable
  y = malloc( sizeof( layer3_t ));

  // 3. calculate number of frames
  y->nframes = n_frames = buf_size / MSS;   

  // 4. overflow = sizeof(layer4.data) mod MSS + layer3 header size
  y->overflow = buf_size % MSS + LAYER_3_HEAD_SIZE;

  // 5. calculate size of tx (transmission) buffer
  y->size = tx_size = LAYER_3_FRAME_SIZE * n_frames + y->overflow;

  // 6. allocate and confirm space for tx buffer
  y->data = ptr = malloc( tx_size+1 ); // +1 for null byte
  if (!ptr) fprintf( stderr, "transmit: layer3: error: malloc failed\n");

  printf( "%s\n", x->data );
  // **********************
...

output:

// first printf
this is a string of characters in the file sendfile.txt. willy wonka is a
fictional character in roald dahl's novel Charlie and the Chocolate Factory.
Willy Wonka is a manufacturer of premier brand chocolates and is planning to
retire soon. In anticipation of this, he begins a competition to find his
successor of the chocolate factory.
the way willy wonka intended on finding his successor was by throwing a contest
for 5 people to find a golden ticket inside one of his chocolate products.
?

// second printf
this is a string?/`3?

3 个答案:

答案 0 :(得分:1)

我认为你正在处理内存泄漏问题。 x->数据中的值可能会因您对y的操作而受到破坏。根据原始x(传入)和y的分配时间,它们可能在堆上彼此相邻。在y上进行每次操作后测试x->数据的值。

祝你好运!

答案 1 :(得分:1)

sizeof( y->size+1 )

应该是:

y->size+1

sizeof(any int)始终是相同的值(Windows上为4,大多数Linux系统上为4,其他Linux上可能不同),与sizeof(int)相同。您不希望为int分配足够的空间,您希望分配y->size+1个字节。

答案 2 :(得分:0)

“x”是在此函数之外已知的指针。也许有人碰这个指针?在另一个线程?

此外,还有一个小错误,您不检查第一个malloc调用的返回值:

y = malloc( sizeof( layer3_t ))