为什么即使我不断更改它而不是2,它仍然说动态数组的容量是2?

时间:2019-07-09 06:09:30

标签: c malloc dynamic-memory-allocation sizeof

我想打印出动态数组的容量,但是无论如何更改,它总是说两个数组的容量均为2。

代码(tmp.c)

var request = require('request');
var qs = require('querystring')
var dataString = qs.stringify({
    path:
        ['["mylist",["id","length","name","requestId","trackIds"]]',
            '["mylist",{"from":0,"to":400},["availability","availabilityEndDateNear","delivery","interactiveBookmark","maturity","numSeasonsLabel","queue","releaseYear","runtime","seasonCount","summary","title","userRating","userRatingRequestId"]]',
            '["mylist",{"from":0,"to":400},"current",["hasAudioDescription","summary"]]',
            '["mylist",{"from":0,"to":400},"boxarts","_233x131","webp"]'],

})

var headers = {
    'Accept': '*/*',
    'Accept-Language': 'en-US,en;q=0.9,es;q=0.8',
    'Connection': 'keep-alive',
    'Content-Type': 'application/x-www-form-urlencoded',
    'DNT': '1',
    'Origin': 'https://www.netflix.com',
    'Postman-Token': '9aceb650-3446-4d39-8234-b503138fd065',
    'Referer': 'https://www.netflix.com/browse/my-list',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
    'X-Netflix.browserName': 'Chrome',
    'X-Netflix.browserVersion': '75',
    'X-Netflix.clientType': 'akira',
    'X-Netflix.esnPrefix': 'NFCDCH-02-',
    'X-Netflix.osFullName': 'Windows 10',
    'X-Netflix.osName': 'Windows',
    'X-Netflix.osVersion': '10.0',
    'X-Netflix.playerThroughput': '58194',
    'X-Netflix.uiVersion': 'v73fa49e3',
    'cache-control': 'no-cache',
    'Cookie': 'Cookie23werwetertert';

//var dataString = 'path=%5B%22mylist%22%2C%5B%22id%22%2C%22length%22%2C%22name%22%2C%22requestId%22%2C%22trackIds%22%5D%5D&path=%5B%22mylist%22%2C%7B%22from%22%3A0%2C%22to%22%3A400%7D%2C%5B%22availability%22%2C%22availabilityEndDateNear%22%2C%22delivery%22%2C%22interactiveBookmark%22%2C%22maturity%22%2C%22numSeasonsLabel%22%2C%22queue%22%2C%22releaseYear%22%2C%22runtime%22%2C%22seasonCount%22%2C%22summary%22%2C%22title%22%2C%22userRating%22%2C%22userRatingRequestId%22%5D%5D&path=%5B%22mylist%22%2C%7B%22from%22%3A0%2C%22to%22%3A400%7D%2C%22current%22%2C%5B%22hasAudioDescription%22%2C%22summary%22%5D%5D&path=%5B%22mylist%22%2C%7B%22from%22%3A0%2C%22to%22%3A400%7D%2C%22boxarts%22%2C%22_233x131%22%2C%22webp%22%5D&authURL=.%%3D';

var options = {
    json: true,
    url: 'https://www.netflix.com/api/shakti/v73fa49e3/pathEvaluator?drmSystem=widevine&isWatchlistEnabled=false&isVolatileBillboardsEnabled=true&falcor_server=0.1.0&withSize=true&materialize=true',
    method: 'POST',
    headers: headers,
    body: dataString
};

function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
    }
}

request(options, callback);

我在终端中执行的操作

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

int main(){

  int *ptr1 = (int *)malloc(sizeof(int) * 3);
  int capacity_ptr1 = sizeof(ptr1)/sizeof(int);
  printf("capacity of ptr1 is: %d\n", capacity_ptr1 );

  int *ptr2 = (int *)realloc(ptr1, sizeof(int) * 5);
  printf("capacity of ptr2 is: %ld\n", sizeof(ptr2)/sizeof(int) );



  return 0;
}

终端中的输出

gcc -std=c99 tmp.c -o tmp
./tmp

无论我为malloc()和realloc()输入什么容量参数,我都会得到此输出

2 个答案:

答案 0 :(得分:1)

取指针(不是数组)的大小(通常为64位),然后将其除以int的大小(通常为32位)。这就是为什么您得到2的原因。

答案 1 :(得分:0)

两件事:

  1. Arrays are not pointers, and vice-versa
  2. 无论如何,您都没有检查分配的内存大小。

详细地说,这里的问题是^\/flipbook\/portfolio\/ 不能产生指针所指向的有效内存的大小,它会返回指针本身的大小。指针的大小(或任何类型的指针)是恒定的。

对于分配器函数返回的指针,有一种直接的方法可以从指针本身获取请求的(和分配的)内存大小,您需要自己跟踪大小。

那是:Please see this discussion on why not to cast the return value of malloc() and family in C..