简体   繁体   中英

Struct pointer remains NULL even after assignment

I am trying to return a Tuple struct from a function, but it seems like it isn't updating the pointer to which it is being assigned.

For instance, if I look at the value of the Tuple just before it is being returned I find that it is allocated properly:

342        Tuple *t = create_tuple();
(gdb) 
345        memcpy(t->part1, cwd, i + 1);
(gdb) p t
$5 = (Tuple *) 0x605010
(gdb) n
346        memcpy(t->part2, &cwd[i + 1], cwd_len - i - 1);
(gdb) 
348        return t;

But after it is assigned it looks like the tuple pointer which I assign to it is still NULL:

291              if ((partitioned = split_prefix(cwd, strlen(cwd))) == NULL)
(gdb) p partitioned
$11 = (Tuple *) 0x0
(gdb) n
296              memcpy(name_field, partitioned->part1, strlen(partitioned- 
>part1));
(gdb) p partitioned
$12 = (Tuple *) 0x0

I thought that since I return the pointer that isn't NULL it should be ok, so I am confused at how I can a NULL pointer before and after the call. Maybe I initialized the pointer incorrectly?

This is the function where the struct pointer returns to:

// gets name field and returns TRUE if successful, otherwise FALSE
int get_name_field(Tuple *partitioned, char name_field[], char *fname, char *cwd, int cwd_len)
{
   // fname cannot be broken up
   if (strlen(fname) > 155)
   {
      perror(fname);
      return FALSE;
   }
   // there is a path (prefix)
   else if (cwd)
   {
      // fname field isn't long enough
      if (strlen(cwd) + 1 + strlen(fname) > 100)
      {
         // ***********assignment here**************
         if ((partitioned = split_prefix(cwd, strlen(cwd))) == NULL)
         {
            perror("partition failed");
            return FALSE;
         }
         memcpy(name_field, partitioned->part1, strlen(partitioned->part1));
      }
...

The function returning the Tuple pointer:

// return a the part of prefix that fits in name field, and the remainder
// if no delimiter is found, returns null
Tuple *split_prefix(char *cwd, int cwd_len)
{
   // find last '/' delimiter that is within NAME_LEN range 
   int found = 0;
   int i = cwd_len <= 100 ? cwd_len - 1: NAME_LEN;
   for (; i > -1; i--)
   {
      if (cwd[i] == '/')
      {
         found = TRUE;
         break;
      }
   }

   // no delimiter found 
   if (!found)
   {
      return NULL;
   }
   // too long to be stored in overflow
   else if (cwd_len - i > PREFIX_LEN)
   {
      return NULL;
   }

   // **********Tuple created here************
   Tuple *t = create_tuple();

   // make partitions
   memcpy(t->part1, cwd, i + 1);
   memcpy(t->part2, &cwd[i + 1], cwd_len - i - 1);

   return t;
}

Tuple's definition:

#define NAME_LEN 100
#define PREFIX_LEN 155

// holds partioned strings of prefix
typedef struct Tuple
{
   char part1[NAME_LEN];
   char part2[PREFIX_LEN];
} Tuple;

To create Tuple:

// create a tuple with default values
Tuple *create_tuple(void)
{
   Tuple *t = malloc(sizeof(Tuple));
   memset(t->part1, '\0', NAME_LEN);
   memset(t->part2, '\0', PREFIX_LEN);

   return t;
}

Edit: The function that calls get_name_field:

void test_get_name_field3(void)
{
   Tuple *partitioned = NULL;
   char name_field[NAME_LEN] = {'\0'};
   char *fname = "hellobutunfortunatelythiswon'tfit.txt";
   char *cwd = "user/bin/arafian/ratherlongdirectorynameandwillnot/fitin"
               "thenamefieldonlysoitwilloverflowtotheprefixfield";
   int cwd_len = strlen(cwd);

   get_name_field(partitioned, name_field, fname, cwd, cwd_len);
   char *result = "user/bin/arafian/ratherlongdirectorynameandwillnot/";
   checkit_string(name_field, result);
   free(partitioned);
}

If you want to return a pointer via an argument to a function, then that function must take a double pointer for an argument. Ie int get_name_field(Tuple **partition, ...) { and called like get_name_field(&partition, ...) .

This is the exact same problem as in the question I recently answered here https://stackoverflow.com/a/50261485/982257 and probably several others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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